Exemple #1
0
// 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)
}
// Finalizer callback for garbage collection.
func (p *Port) free() {
	p.Info.free()
	if p.c != nil {
		C.sp_free_config(p.c)
	}
	p.c = nil
}
// 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()
}