Beispiel #1
0
func NewQueryEngine(
	rtuDevice string,
	interval int,
	verbose bool,
	channel ReadingChannel,
) *QueryEngine {
	// Modbus RTU/ASCII
	mbhandler := modbus.NewRTUClientHandler(rtuDevice)
	mbhandler.BaudRate = 9600
	mbhandler.DataBits = 8
	mbhandler.Parity = "N"
	mbhandler.StopBits = 1
	mbhandler.SlaveId = 1
	mbhandler.Timeout = 1000 * time.Millisecond
	if verbose {
		mbhandler.Logger = log.New(os.Stdout, "RTUClientHandler: ", log.LstdFlags)
		log.Printf("Connecting to RTU via %s\r\n", rtuDevice)
	}

	err := mbhandler.Connect()
	if err != nil {
		log.Fatal("Failed to connect: ", err)
	}

	mbclient := modbus.NewClient(mbhandler)

	return &QueryEngine{client: mbclient, interval: interval,
		handler: *mbhandler, datastream: channel}
}
Beispiel #2
0
func main() {

	// Modbus RTU/ASCII
	handler := modbus.NewRTUClientHandler("/dev/ttyUSB0") // I use FT232 based handmade converter
	handler.BaudRate = 19200
	handler.DataBits = 8
	handler.Parity = "O" // Odd parity
	handler.StopBits = 1
	handler.SlaveId = 1
	handler.Timeout = 2 * time.Second

	err := handler.Connect()
	chk(err)
	defer handler.Close()

	client := modbus.NewClient(handler)
	for i := uint16(0); i < 7; i++ {
		results, err := client.WriteSingleCoil(64512+i, 0xFF00)
		chk(err)
		fmt.Println(results)
		results1, err := client.WriteSingleCoil(64512+i, 0)
		chk(err)
		fmt.Println(results1)
	}
}
Beispiel #3
0
// ReadParam reads parameter p's value from the motor.
func (m *Motor) ReadParam(p *Parameter) error {
	m.c.SlaveId = byte(p.Index[0])
	o, err := modbus.NewClient(m.c).ReadHoldingRegisters(p.MBReg(), nregs)
	if err != nil {
		return err
	}
	copy(p.Data[:], o)
	return err
}
Beispiel #4
0
// WriteParam writes parameter p's value to the motor.
func (m *Motor) WriteParam(p Parameter) error {
	m.c.SlaveId = byte(p.Index[0])
	o, err := modbus.NewClient(m.c).WriteMultipleRegisters(p.MBReg(), nregs, p.Data[:])
	if err != nil {
		return err
	}
	if o[1] != nregs {
		return fmt.Errorf(
			"m702: invalid write at Pr-%v. expected %d, got %d",
			p, nregs, o[1],
		)
	}
	return err
}
Beispiel #5
0
func TestASCIIClientAdvancedUsage(t *testing.T) {
	handler := modbus.NewASCIIClientHandler(asciiDevice)
	handler.BaudRate = 19200
	handler.DataBits = 8
	handler.Parity = "E"
	handler.StopBits = 1
	handler.SlaveId = 12
	handler.Logger = log.New(os.Stdout, "ascii: ", log.LstdFlags)
	err := handler.Connect()
	if err != nil {
		t.Fatal(err)
	}
	defer handler.Close()

	client := modbus.NewClient(handler)
	results, err := client.ReadDiscreteInputs(15, 2)
	if err != nil || results == nil {
		t.Fatal(err, results)
	}
	results, err = client.ReadWriteMultipleRegisters(0, 2, 2, 2, []byte{1, 2, 3, 4})
	if err != nil || results == nil {
		t.Fatal(err, results)
	}
}
Beispiel #6
0
func TestASCIIClient(t *testing.T) {
	// Diagslave does not support broadcast id.
	handler := modbus.NewASCIIClientHandler(asciiDevice)
	handler.SlaveId = 17
	ClientTestAll(t, modbus.NewClient(handler))
}