Skip to content

Commit

Permalink
dev: fix read frequency
Browse files Browse the repository at this point in the history
  • Loading branch information
wwhai committed Apr 15, 2023
1 parent 2bc33f0 commit 6dfe8c0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
14 changes: 8 additions & 6 deletions device/custom_protocol_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,11 @@ func (mdev *CustomProtocolDevice) Start(cctx typex.CCTX) error {
time.Sleep(time.Duration(p.AutoRequestGap) * time.Millisecond)
result := [100]byte{} // 全局buf, 默认是100字节, 应该能覆盖绝大多数报文了
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
if _, err2 := utils.ReadAtLeast(ctx, cancel, mdev.serialPort, result[:p.BufferSize],
if _, err2 := utils.ReadAtLeast(ctx, mdev.serialPort, result[:p.BufferSize],
p.BufferSize); err2 != nil {
glogger.GLogger.Error("serialPort.ReadAtLeast error: ", err2)
mdev.errorCount++
cancel()
continue
}
if core.GlobalConfig.AppDebugMode {
Expand Down Expand Up @@ -318,12 +319,14 @@ func (mdev *CustomProtocolDevice) OnRead(cmd []byte, data []byte) (int, error) {
result := [100]byte{} // 全局buf, 默认是100字节, 应该能覆盖绝大多数报文了

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
if _, err2 := utils.ReadAtLeast(ctx, cancel, mdev.serialPort, result[:p.BufferSize],
if _, err2 := utils.ReadAtLeast(ctx, mdev.serialPort, result[:p.BufferSize],
p.BufferSize); err2 != nil {
glogger.GLogger.Error("serialPort.ReadAtLeast error: ", err2)
mdev.errorCount++
cancel()
return 0, err2
}
cancel()

if core.GlobalConfig.AppDebugMode {
log.Println("[AppDebugMode] Write data:", p.ProtocolArg.In)
Expand Down Expand Up @@ -407,17 +410,16 @@ func (mdev *CustomProtocolDevice) OnWrite(cmd []byte, data []byte) (int, error)
return 0, err1
}

// 协议等待响应时间毫秒
time.Sleep(time.Duration(p.AutoRequestGap) * time.Millisecond)
result := [100]byte{} // 全局buf, 默认是100字节, 应该能覆盖绝大多数报文了

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
if _, err2 := utils.ReadAtLeast(ctx, cancel, mdev.serialPort, result[:p.BufferSize],
if _, err2 := utils.ReadAtLeast(ctx, mdev.serialPort, result[:p.BufferSize],
p.BufferSize); err2 != nil {
glogger.GLogger.Error("serialPort.ReadAtLeast error: ", err2)
mdev.errorCount++
cancel()
return 0, err2
}
cancel()
if core.GlobalConfig.AppDebugMode {
log.Println("[AppDebugMode] Write data:", p.ProtocolArg.In)
log.Println("[AppDebugMode] Read data:", result[:p.BufferSize])
Expand Down
2 changes: 1 addition & 1 deletion test/apps/read_multi_device1.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ function Main(arg)
print("UdpData => ", udpDataJson)
local err4 = applib:DataToUdp(udpServerUUID, udpDataJson)
print('DataToUdp success? =>', err4 == nil)
applib:Sleep(1000)
end
applib:Sleep(1000)
::END::
end
end
4 changes: 1 addition & 3 deletions utils/io_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ var errTimeout = errors.New("read Timeout")
*
*/

func ReadAtLeast(ctx context.Context, cancel context.CancelFunc,
r io.Reader, buf []byte, min int) (n int, err error) {
defer cancel()
func ReadAtLeast(ctx context.Context, r io.Reader, buf []byte, min int) (n int, err error) {
if len(buf) < min {
n = 0
err = errShortBuffer
Expand Down

0 comments on commit 6dfe8c0

Please sign in to comment.