| | |
| | | ParityNull Parity = 3 //无校验 |
| | | ) |
| | | |
| | | func (p Parity) String() string { |
| | | func (p Parity) ToString() string { |
| | | switch p { |
| | | case ParityEven: |
| | | return "E" |
| | |
| | | func newModbusRTUConnection(c *common.RTUConfig) (plc4go.PlcConnection, error) { |
| | | ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) |
| | | // 创建一个新的 PLC 连接 |
| | | connectionString := fmt.Sprintf("modbus-rtu:serial://%s?baudrate=%d&databits=%d&stopbits=%d&parity=%s", c.SerialName, c.BaudRate, c.DataBit, c.StopBit, c.Parity) |
| | | connectionString := fmt.Sprintf("modbus-rtu:serial://%s?baudrate=%d&databits=%d&stopbits=%d&parity=%s", c.SerialName, c.BaudRate, c.DataBit, c.StopBit, c.Parity.ToString()) |
| | | connectionRequestChanel := driverManager.GetConnection(connectionString) |
| | | // 等待连接响应,同时考虑上下文的超时 |
| | | select { |
| | |
| | | rtuHandler = modbus.NewRTUClientHandler(c.SerialName) |
| | | rtuHandler.BaudRate = c.BaudRate |
| | | rtuHandler.DataBits = c.DataBit |
| | | rtuHandler.Parity = c.Parity.String() |
| | | rtuHandler.Parity = c.Parity.ToString() |
| | | rtuHandler.StopBits = c.StopBit |
| | | rtuHandler.SlaveId = 1 |
| | | rtuHandler.Timeout = 5 * time.Second |