pcsms_r9rnbwYw 发表于 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, response
short address, checksum
short read_no, return_value, read_data, i

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

command = 0x01//station no
command = 0x03//read holding registers

address = 0
HIBYTE(address, command)
LOBYTE(address, command)

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

CRC(command, checksum, 6)

LOBYTE(checksum, command)
HIBYTE(checksum, command)

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

DELAY(100)                //延迟100ms

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

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

if (return_value > 0 and response == 0x03) then
read_data = response + (response *256)//4x_1
read_data = response + (response *256)//4x_2
read_data = response + (response *256)//4x_1
read_data = response + (response *256)//4x_2
read_data = response + (response *256)//4x_1
read_data = response + (response *256)//4x_2
read_data = response + (response *256)//4x_1
read_data = response + (response *256)//4x_2
read_data = response + (response *256)//4x_1
read_data = response + (response *256)//4x_2

SetData(read_data, "Local HMI", LW, 40, 10)
end if

end macro_command

我为了验证数据在传输过程中是不是有变化,用另一个485的设备,接收到的数据,和MODBUS SLAVE上的数据是一样的。
部分数据正确,部分数据不正确,这个就有点无从下手了。有碰到过的朋友吗?请指点一下,谢谢谢谢

pcsms_GzNtyrj5 发表于 2022-7-20 15:39:32

我也是把常查询宏指令下载到屏幕里去,但是调试助手根本收不到查询的命令,硬件连接都没问题,请问一下您是怎么设置的呢
页: [1]
查看完整版本: 宏指令与单片机进行通讯