MCGS 使用脚本字符串计算自由口通信控制32路继电器测试
使用脚本进行字符串计算
帧头="BBBF"
帧尾="FF025557"
和校验=!right(!I2Hex(BYTE1+BYTE2+BYTE3+BYTE4),2)
发送数据=帧头+!I2Hex(BYTE1)+!I2Hex(BYTE2)+!I2Hex(BYTE3)+!I2Hex(BYTE4)+和校验+帧尾
'串口接收数据
Receivehex=!ReadSerial(CommNo)
'串口未正确打开则退出
IF CommError1<>0 OR CommError2<>0 OR CommError3<>0 OR CommError4<>0 THEN
EXIT
ENDIF
通过脚本设置串口参数
'保存串口参数
CommNo.SaveDataInitValue( )
BaudRate.SaveDataInitValue( )
DataBit.SaveDataInitValue( )
StopBit.SaveDataInitValue( )
ParityBit.SaveDataInitValue( )
'刷新到磁盘上
!FlushDataInitValueToDisk( )
'设定波特率
CommError1=!SetSerialBaud(CommNo,BaudRate)
IF CommError1<>0 THEN
EXIT
ENDIF
'设定数据位
CommError2=!SetSerialDataBit(CommNo,DataBit)
IF CommError2<>0 THEN
EXIT
ENDIF
'设定停止位
CommError3=!SetSerialStopBit(CommNo,StopBit)
IF CommError3<>0 THEN
EXIT
ENDIF
'设定校验位
CommError4=!SetSerialParityBit(CommNo,ParityBit)
IF CommError4<>0 THEN
EXIT
ENDIF
可以进行非标准modbus rtu协议通信控制
不需要添加串口驱动
32路继电器控制发送数据格式如下
BB BF 00 00 00 01 01 FF 02 55 57 //k1
BB BF 00 00 00 02 02 FF 02 55 57 //k2
BB BF 00 00 00 04 04 FF 02 55 57 //k3
BB BF 00 00 00 08 08 FF 02 55 57 //k4
BB BF 00 00 00 10 10 FF 02 55 57 //k5
BB BF 00 00 00 20 20 FF 02 55 57 //K6
BB BF 00 00 00 40 40 FF 02 55 57 //K7
BB BF 00 00 00 80 80 FF 02 55 57 //K8
BB BF 00 00 01 00 01 FF 02 55 57 //K9
BB BF 00 00 02 00 02 FF 02 55 57 //K10
BB BF 00 00 04 00 04 FF 02 55 57 //K11
BB BF 00 00 08 00 08 FF 02 55 57 //K12
BB BF 00 00 10 00 10 FF 02 55 57 //K13
BB BF 00 00 20 00 20 FF 02 55 57 //K14
BB BF 00 00 40 00 40 FF 02 55 57 //K15
BB BF 00 00 80 00 80 FF 02 55 57 //K16
BB BF 00 01 00 00 01 FF 02 55 57 //K17
BB BF 00 02 00 00 02 FF 02 55 57 //K18
BB BF 00 04 00 00 04 FF 02 55 57 //K19
BB BF 00 08 00 00 08 FF 02 55 57 //K20
BB BF 00 10 00 00 10 FF 02 55 57 //K21
BB BF 00 20 00 00 20 FF 02 55 57 //K22
BB BF 00 40 00 00 40 FF 02 55 57 //K23
BB BF 00 80 00 00 80 FF 02 55 57 //K24
BB BF 01 00 00 00 01 FF 02 55 57 //K25
BB BF 02 00 00 00 02 FF 02 55 57 //K26
BB BF 04 00 00 00 04 FF 02 55 57 //K27
BB BF 08 00 00 00 08 FF 02 55 57 //K28
BB BF 10 00 00 00 10 FF 02 55 57 //K29
BB BF 20 00 00 00 20 FF 02 55 57 //K30
BB BF 40 00 00 00 40 FF 02 55 57 //K31
BB BF 80 00 00 00 40 FF 02 55 57 //K32
下发数据
'发送16进制 字符串数据
SENDHEX=!WriteSerial(CommNo,!Hex2I("BB"))AND!WriteSerial(CommNo,!Hex2I("BF"))AND!WriteSerial(CommNo,BYTE1)AND!WriteSerial(CommNo,BYTE2)AND!WriteSerial(CommNo,BYTE3)AND!WriteSerial(CommNo,BYTE4)AND!WriteSerial(CommNo,!Hex2I(和校验))AND!WriteSerial(CommNo,!Hex2I("FF"))AND!WriteSerial(CommNo,!Hex2I("02"))AND!WriteSerial(CommNo,!Hex2I("55"))AND!WriteSerial(CommNo,!Hex2I("57"))
每次设置一个继电器动作发送数据通过虚拟串口监测的数据
任意组合设置继电器动作通过虚拟串口监测的数据
楼主最近还看过