// Close restores the port to how it was before initialisation, closes the port and // frees resources. func Close(s *Conf) { C.sp_flush(s.Port, SP_BUF_BOTH) C.sp_free_config(s.newConfig) C.sp_set_config(s.Port, s.oldConfig) C.sp_free_config(s.oldConfig) C.sp_close(s.Port) }
// SetParams sets the common port options: baudrate, bits and stopbits. func SetParams(s *Conf, baud int, bits int, stopbits int) (int32, error) { var result int32 = SP_OK C.sp_set_config_baudrate(s.newConfig, C.int(baud)) C.sp_set_config_bits(s.newConfig, C.int(bits)) C.sp_set_config_parity(s.newConfig, SP_PARITY_NONE) C.sp_set_config_stopbits(s.newConfig, C.int(stopbits)) result = C.sp_set_config(s.Port, s.newConfig) return result, checkResult(result) }
// Apply port options. func (p *Port) Apply(o *Options) (err error) { // get port config var conf *C.struct_sp_port_config if err = errmsg(C.sp_new_config(&conf)); err != nil { return } defer C.sp_free_config(conf) // set bit rate if o.BitRate != 0 { err = errmsg(C.sp_set_config_baudrate(conf, C.int(o.BitRate))) if err != nil { return } } // set data bits if o.DataBits != 0 { err = errmsg(C.sp_set_config_bits(conf, C.int(o.DataBits))) if err != nil { return } } // set stop bits if o.StopBits != 0 { err = errmsg(C.sp_set_config_stopbits(conf, C.int(o.StopBits))) if err != nil { return } } // set parity if o.Parity != 0 { cparity := parity2c(o.Parity) if err = errmsg(C.sp_set_config_parity(conf, cparity)); err != nil { return } } // set flow control if o.FlowControl != 0 { cfc, err := flow2c(o.FlowControl) if err != nil { return err } if err = errmsg(C.sp_set_config_flowcontrol(conf, cfc)); err != nil { return err } } // set RTS if o.RTS != 0 { crts := rts2c(o.RTS) if err = errmsg(C.sp_set_config_rts(conf, crts)); err != nil { return } } // set CTS if o.CTS != 0 { ccts := cts2c(o.CTS) if err = errmsg(C.sp_set_config_cts(conf, ccts)); err != nil { return } } // set DTR if o.DTR != 0 { cdtr := dtr2c(o.DTR) if err = errmsg(C.sp_set_config_dtr(conf, cdtr)); err != nil { return } } // set DSR if o.DSR != 0 { cdsr := dsr2c(o.DSR) if err = errmsg(C.sp_set_config_dsr(conf, cdsr)); err != nil { return } } // apply config if err = errmsg(C.sp_set_config(p.p, conf)); err != nil { return } // update local config return p.getConf() }