Compare commits
7 Commits
08a89aa3bb
...
v1.0.3
| Author | SHA1 | Date | |
|---|---|---|---|
| fb90bbf207 | |||
| 343292b9b2 | |||
| a635b8af4e | |||
| 7ba768b8c2 | |||
| b45b8e1d8d | |||
| 1df66eccd1 | |||
| 34b8d0fedd |
@@ -332,8 +332,8 @@ func (rt *rtuTransport) readRTUFrame() (res *pdu, err error) {
|
|||||||
crc.add(rxbuf[0:crcEndIndex]) // 校验范围从 rxbuf[0] 到数据域结束
|
crc.add(rxbuf[0:crcEndIndex]) // 校验范围从 rxbuf[0] 到数据域结束
|
||||||
|
|
||||||
// 比较接收到的 CRC
|
// 比较接收到的 CRC
|
||||||
// sentHigh := rxbuf[crcEndIndex] // C_high (例如 0x8B)
|
sentHigh := rxbuf[crcEndIndex] // C_high (例如 0x8B)
|
||||||
// sentLow := rxbuf[crcEndIndex+1] // C_low (例如 0xB0)
|
sentLow := rxbuf[crcEndIndex+1] // C_low (例如 0xB0)
|
||||||
|
|
||||||
// 由于 isEqual 期望 (low, high),我们需要将 sentLow 传给 low
|
// 由于 isEqual 期望 (low, high),我们需要将 sentLow 传给 low
|
||||||
|
|
||||||
@@ -344,11 +344,11 @@ func (rt *rtuTransport) readRTUFrame() (res *pdu, err error) {
|
|||||||
|
|
||||||
// fmt.Println("len: ", len(rxbuf[crcEndIndex-10:crcEndIndex]))
|
// fmt.Println("len: ", len(rxbuf[crcEndIndex-10:crcEndIndex]))
|
||||||
|
|
||||||
// if !crc.isEqual(sentLow, sentHigh) {
|
if !crc.isEqual(sentHigh, sentLow) {
|
||||||
// err = ErrBadCRC
|
err = ErrBadCRC
|
||||||
// fmt.Println("crc: ", sentLow, sentHigh, "byte needed: ", bytesNeeded)
|
// fmt.Println("crc: ", sentLow, sentHigh, "byte needed: ", bytesNeeded)
|
||||||
// return
|
return
|
||||||
// }
|
}
|
||||||
|
|
||||||
// 8. 构造 PDU
|
// 8. 构造 PDU
|
||||||
// Payload 包含:自定义数据 (4 bytes) + Data (N bytes)
|
// Payload 包含:自定义数据 (4 bytes) + Data (N bytes)
|
||||||
@@ -465,24 +465,29 @@ func (rt *rtuTransport) readRTUFrameWithRes() (res *pdu, err error) {
|
|||||||
// 标准modbus响应已读取完成,现在读取自定义数据
|
// 标准modbus响应已读取完成,现在读取自定义数据
|
||||||
// 设置5秒超时来读取自定义数据
|
// 设置5秒超时来读取自定义数据
|
||||||
customDataTimeout := 5 * time.Second
|
customDataTimeout := 5 * time.Second
|
||||||
err = rt.link.SetDeadline(time.Now().Add(customDataTimeout))
|
deadline := time.Now().Add(customDataTimeout)
|
||||||
|
err = rt.link.SetDeadline(deadline)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// fmt.Println("---: ", rxbuf[:10])
|
|
||||||
|
|
||||||
// a := make([]byte, 100)
|
|
||||||
// n, err := io.ReadFull(rt.link, a)
|
|
||||||
// fmt.Println("n: ", n, "err: ", err, "a: ", a)
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
|
|
||||||
// 使用临时缓冲区循环读取自定义数据
|
// 使用临时缓冲区循环读取自定义数据
|
||||||
tempBuf := make([]byte, 256) // 每次读取最多256字节
|
tempBuf := make([]byte, 256) // 每次读取最多256字节
|
||||||
totalRead := 0
|
totalRead := 0
|
||||||
startPos := 3 + bytesNeeded
|
startPos := 3 + bytesNeeded
|
||||||
|
var lastErr error // 记录最后一次非超时错误
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
// 检查是否已经超时
|
||||||
|
if time.Now().After(deadline) {
|
||||||
|
// 超时时间到,退出循环
|
||||||
|
// 如果有之前记录的错误,使用它;否则err保持为nil(超时是正常的)
|
||||||
|
if lastErr != nil {
|
||||||
|
err = lastErr
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
// 检查缓冲区是否还有空间
|
// 检查缓冲区是否还有空间
|
||||||
if startPos+totalRead+len(tempBuf) > len(rxbuf) {
|
if startPos+totalRead+len(tempBuf) > len(rxbuf) {
|
||||||
// 如果缓冲区不够,扩展它
|
// 如果缓冲区不够,扩展它
|
||||||
@@ -499,31 +504,23 @@ func (rt *rtuTransport) readRTUFrameWithRes() (res *pdu, err error) {
|
|||||||
totalRead += n
|
totalRead += n
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果遇到超时错误,说明没有更多数据了
|
// 如果遇到超时错误,说明超时时间到了,退出循环
|
||||||
if readErr != nil {
|
if readErr != nil {
|
||||||
if os.IsTimeout(readErr) {
|
if os.IsTimeout(readErr) {
|
||||||
// 超时是正常的,说明自定义数据读取完成
|
// 超时时间到,退出循环
|
||||||
|
// 如果有之前记录的错误,使用它;否则err保持为nil(超时是正常的)
|
||||||
|
if lastErr != nil {
|
||||||
|
err = lastErr
|
||||||
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// 其他错误需要检查是否是EOF(数据读取完成)
|
// 记录非超时错误,但继续等待直到超时
|
||||||
if readErr == io.EOF {
|
lastErr = readErr
|
||||||
break
|
// 继续循环,等待超时
|
||||||
}
|
|
||||||
// 对于其他错误,如果已经读取了一些数据,继续处理
|
|
||||||
if totalRead == 0 {
|
|
||||||
err = readErr
|
|
||||||
return
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果读取的字节数少于请求的,说明没有更多数据了
|
|
||||||
if n < len(tempBuf) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 返回标准响应的payload(地址+值,4字节)+ 自定义数据
|
|
||||||
// 标准响应的payload在 rxbuf[2:3+bytesNeeded-2] 位置
|
// 标准响应的payload在 rxbuf[2:3+bytesNeeded-2] 位置
|
||||||
standardPayloadStart := 2
|
standardPayloadStart := 2
|
||||||
standardPayloadEnd := 3 + bytesNeeded - 2
|
standardPayloadEnd := 3 + bytesNeeded - 2
|
||||||
@@ -538,9 +535,7 @@ func (rt *rtuTransport) readRTUFrameWithRes() (res *pdu, err error) {
|
|||||||
res = &pdu{
|
res = &pdu{
|
||||||
unitId: rxbuf[0],
|
unitId: rxbuf[0],
|
||||||
functionCode: rxbuf[1],
|
functionCode: rxbuf[1],
|
||||||
// payload包含标准响应的payload + 自定义数据
|
payload: completePayload,
|
||||||
payload: completePayload,
|
|
||||||
// payload: a,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|||||||
54
serial.go
54
serial.go
@@ -1,6 +1,6 @@
|
|||||||
package modbus
|
package modbus
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/goburrow/serial"
|
"github.com/goburrow/serial"
|
||||||
@@ -10,43 +10,46 @@ import (
|
|||||||
// 1) satisfy the rtuLink interface and
|
// 1) satisfy the rtuLink interface and
|
||||||
// 2) add Read() deadline/timeout support.
|
// 2) add Read() deadline/timeout support.
|
||||||
type serialPortWrapper struct {
|
type serialPortWrapper struct {
|
||||||
conf *serialPortConfig
|
conf *serialPortConfig
|
||||||
port serial.Port
|
port serial.Port
|
||||||
deadline time.Time
|
deadline time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type serialPortConfig struct {
|
type serialPortConfig struct {
|
||||||
Device string
|
Device string
|
||||||
Speed uint
|
Speed uint
|
||||||
DataBits uint
|
DataBits uint
|
||||||
Parity uint
|
Parity uint
|
||||||
StopBits uint
|
StopBits uint
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSerialPortWrapper(conf *serialPortConfig) (spw *serialPortWrapper) {
|
func newSerialPortWrapper(conf *serialPortConfig) (spw *serialPortWrapper) {
|
||||||
spw = &serialPortWrapper{
|
spw = &serialPortWrapper{
|
||||||
conf: conf,
|
conf: conf,
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (spw *serialPortWrapper) Open() (err error) {
|
func (spw *serialPortWrapper) Open() (err error) {
|
||||||
var parity string
|
var parity string
|
||||||
|
|
||||||
switch spw.conf.Parity {
|
switch spw.conf.Parity {
|
||||||
case PARITY_NONE: parity = "N"
|
case PARITY_NONE:
|
||||||
case PARITY_EVEN: parity = "E"
|
parity = "N"
|
||||||
case PARITY_ODD: parity = "O"
|
case PARITY_EVEN:
|
||||||
|
parity = "E"
|
||||||
|
case PARITY_ODD:
|
||||||
|
parity = "O"
|
||||||
}
|
}
|
||||||
|
|
||||||
spw.port, err = serial.Open(&serial.Config{
|
spw.port, err = serial.Open(&serial.Config{
|
||||||
Address: spw.conf.Device,
|
Address: spw.conf.Device,
|
||||||
BaudRate: int(spw.conf.Speed),
|
BaudRate: int(spw.conf.Speed),
|
||||||
DataBits: int(spw.conf.DataBits),
|
DataBits: int(spw.conf.DataBits),
|
||||||
Parity: parity,
|
Parity: parity,
|
||||||
StopBits: int(spw.conf.StopBits),
|
StopBits: int(spw.conf.StopBits),
|
||||||
Timeout: 10 * time.Millisecond,
|
Timeout: 100 * time.Millisecond,
|
||||||
})
|
})
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -64,11 +67,12 @@ func (spw *serialPortWrapper) Close() (err error) {
|
|||||||
// attempting to read from the serial port.
|
// attempting to read from the serial port.
|
||||||
// If Read() is called before the deadline, a read attempt to the serial port
|
// If Read() is called before the deadline, a read attempt to the serial port
|
||||||
// is made. At this point, one of two things can happen:
|
// is made. At this point, one of two things can happen:
|
||||||
// - the serial port's receive buffer has one or more bytes and port.Read()
|
// - the serial port's receive buffer has one or more bytes and port.Read()
|
||||||
// returns immediately (partial or full read),
|
// returns immediately (partial or full read),
|
||||||
// - the serial port's receive buffer is empty: port.Read() blocks for
|
// - the serial port's receive buffer is empty: port.Read() blocks for
|
||||||
// up to 10ms and returns serial.ErrTimeout. The serial timeout error is
|
// up to 10ms and returns serial.ErrTimeout. The serial timeout error is
|
||||||
// masked and Read() returns with no data.
|
// masked and Read() returns with no data.
|
||||||
|
//
|
||||||
// As the higher-level methods use io.ReadFull(), Read() will be called
|
// As the higher-level methods use io.ReadFull(), Read() will be called
|
||||||
// as many times as necessary until either enough bytes have been read or an
|
// as many times as necessary until either enough bytes have been read or an
|
||||||
// error is returned (ErrRequestTimedOut or any other i/o error).
|
// error is returned (ErrRequestTimedOut or any other i/o error).
|
||||||
|
|||||||
Reference in New Issue
Block a user