MT6070ih屏通过RS485与第三方设备通信,第三方设备连续不停发送数据,触摸屏只是单纯负责接收数据,该设备发送数据格式根据实际情况会有所不同,比如正常的情况下是发送20个字节,有起始符和结束符,异常情况下,发送8个字节,也有起始符和结束符,下面是之前的程序,数据的接收开始还是准的,但如果碰到异常情况下,就不正确了,把RS485通讯线断开重新连接,数据又可以正常显示,但随着通讯时间长了,接收到的数据又不准确了,触摸屏的通讯inport指令功能还是比较弱,
哪位网友有做过这方面类似的通讯,麻烦回复下,谢谢!
macro_command main()
unsigned char rcvbuffer,char1,data[8],result[4]
unsigned char str1[4],str2[32]
short rcvdata[25]
short receive_len,i,j,k,temp
float floatresult
bool flat=false
i=0
INPORT(rcvbuffer, "Free Protocol", 1, receive_len)
while (receive_len>=1)
//开始接收数据
if rcvbuffer==0x3C and not flat then
i=0
flat=true
FILL(rcvdata[0], 0, 25)
SetData(rcvdata[0], "Local HMI", LW, 0, 25)
end if
if flat then
rcvdata[i]=rcvbuffer
i=i+1
end if
//数据接收结束
if rcvbuffer==0x0B and flat then
flat=false
SetData(rcvdata[0], "Local HMI", LW, 0, i)
//数据处理
for j=5 to 12
if rcvdata[j]>=0x41 and rcvdata[j]<=0x46 then
rcvdata[j]=rcvdata[j]-55
else if rcvdata[j]>=0x30 and rcvdata[j]<=0x39 then
rcvdata[j]=rcvdata[j]-48
else
HEX2ASCII(rcvdata[j], str1[0], 4)
ASCII2HEX(str1[2], char1, 1)
rcvdata[j]=rcvdata[j]-char1*16
end if
next
temp=rcvdata[5]
rcvdata[5]=rcvdata[11]
rcvdata[11]=temp
temp=rcvdata[6]
rcvdata[6]=rcvdata[12]
rcvdata[12]=temp
temp=rcvdata[7]
rcvdata[7]=rcvdata[9]
rcvdata[9]=temp
temp=rcvdata[8]
rcvdata[8]=rcvdata[10]
rcvdata[10]=temp
for k=5 to 12
short a,b,c
a=4*(k-5)
b=a+3
c=k-5
HEX2ASCII(rcvdata[k], str2[a], 4)
ASCII2HEX(str2[b], data[c], 1)
next
result[0]=data[6]*16+data[7]
result[1]=data[4]*16+data[5]
result[2]=data[2]*16+data[3]
result[3]=data[0]*16+data[1]
SetData(result[0], "Local HMI", LW, 40, 4)
GetData(floatresult, "Local HMI", LW, 40, 1)
floatresult=floatresult-273
SetData(floatresult, "Local HMI", LW, 40, 1)
break
end if
INPORT(rcvbuffer, "Free Protocol", 1, receive_len)
wend
PURGE(1)
end macro_command |