威纶通官方论坛

宏指令与单片机进行通讯

[复制链接]
发表于 2021-10-25 13:25:19 | 显示全部楼层 |阅读模式
最近在做一个项目,使用TK6071ip和单片机之间,使用自由口协议进行通讯,但是遇到一个很奇怪的问题。
HMI作为主站发送数据请求,使用一个MODBUS SLAVE作为从机,HMI发出的数据为:
01 03 00 00 00 0A C5 CD,意思是读取10个保持寄存器的数据
MODBUS SLAVE有数据返回:
01 03 14 00 0B 08 AE 07 D0 11 11 11 11 11 11 11 11 11 11 00 00 00 00 E6 8C
第一个数据00 0B,在HMI上显示正确
第二个数据08 AE,在HMI上显示成了1968,对应的16进制为07 AE
第三个数据07 D0,在HMI上显示成了1744,对应的16进制为06 D0
后面的7个数据都正常。

我使用的宏指令为:
//  Read Holding Registers
macro_command main()

char command[32], response[32]
short address, checksum
short read_no, return_value, read_data[10], i

FILL(command[0], 0, 32)                        //  init
FILL(response[0], 0, 32)

command[0] = 0x01//  station no
command[1] = 0x03//  read holding registers

address = 0
HIBYTE(address, command[2])
LOBYTE(address, command[3])

read_no = 10//  read 4x_1, 4x_2
HIBYTE(read_no, command[4])
LOBYTE(read_no, command[5])

CRC(command[0], checksum, 6)

LOBYTE(checksum, command[6])
HIBYTE(checksum, command[7])

OUTPORT(command[0], "MODBUS RTU Device", 8)                                                //  send command

DELAY(100)                //延迟100ms

INPORT(response[0], "MODBUS RTU Device", 25, return_value)                //  read response

//SetData(return_value, "Local HMI", LW, 0, 1)//  return_value == 0 -> error
//SetData(response[0], "Local HMI", LW, 10, 24)//  send response to LW  

if (return_value > 0 and response[1] == 0x03) then
  read_data[0] = response[4] + (response[3] *256)//  4x_1
  read_data[1] = response[6] + (response[5] *256)//  4x_2
  read_data[2] = response[8] + (response[7] *256)//  4x_1
  read_data[3] = response[10] + (response[9] *256)//  4x_2
  read_data[4] = response[12] + (response[11] *256)//  4x_1
  read_data[5] = response[14] + (response[13] *256)//  4x_2
  read_data[6] = response[16] + (response[15] *256)//  4x_1
  read_data[7] = response[18] + (response[17] *256)//  4x_2
  read_data[8] = response[20] + (response[19] *256)//  4x_1
  read_data[9] = response[22] + (response[21] *256)//  4x_2
  
  SetData(read_data[0], "Local HMI", LW, 40, 10)  
end if

end macro_command

我为了验证数据在传输过程中是不是有变化,用另一个485的设备,接收到的数据,和MODBUS SLAVE上的数据是一样的。
部分数据正确,部分数据不正确,这个就有点无从下手了。有碰到过的朋友吗?请指点一下,谢谢谢谢
发表于 2022-7-20 15:39:32 | 显示全部楼层
我也是把常查询宏指令下载到屏幕里去,但是调试助手根本收不到查询的命令,硬件连接都没问题,请问一下您是怎么设置的呢
您需要登录后才可以发帖 登录 | 注册

本版积分规则

回复帖子

Archiver|小黑屋|威纶通官网 ( 粤ICP备06054553号 )

GMT+8, 2024-4-20 22:04

Powered by Discuz! X3.4

© 2001-2023 Comsenz Inc.

快速回复 返回顶部 返回列表