dll调用学习----modbusRTU.dll
By
cuiqbo
at 2021-04-17 • 1人收藏 • 1910人看过
把几个功能都更新完成了有的方法写的不好,请大神指出
使用工具
先使用VSPD设备创建虚拟串口
modbus虚拟设备软件
虚拟设备配置表
可以设备配置表导入到虚拟设备
使用aardio调用modbusRTU.dll代码如下
import console; var dll = raw.loadDll($"\ModbusRTU.dll") RTUOpenCom= dll.api("RTUOpenCom","bool(int nPort, INT dwBaud = 9600, BYTE nDataBits = 8, BYTE parity = 0, BYTE stopBits = 0)"); RTUCloseCom= dll.api("RTUCloseCom","void()"); RTUReadCoils= dll.api("RTUReadCoils","WORD(BYTE nSlaveAddress, WORD nUnitAddress, WORD nUnitCount, string& pReturnBuf)"); RTUReadDiscreteInputs= dll.api("RTUReadDiscreteInputs","WORD(BYTE nSlaveAddress, WORD nUnitAddress, WORD nUnitCount, string& pReturnBuf)"); RTUReadHoldingRegisters= dll.api("RTUReadHoldingRegisters","WORD(BYTE nSlaveAddress, WORD nRegAddress, WORD nRegCount, string& pReturnBuf)"); RTUReadInputRegisters= dll.api("RTUReadInputRegisters","WORD(BYTE nSlaveAddress, WORD nRegAddress, WORD nRegCount, string& pReturnBuf)"); RTUWriteSingleCoil= dll.api("RTUWriteSingleCoil","WORD(BYTE nSlaveAddress, WORD nRegAddress, WORD nUnitValue)"); RTUWriteMultipleCoils= dll.api("RTUWriteMultipleCoils","WORD(BYTE nSlaveAddress, WORD nRegAddress, WORD nUnitCount, string& pWriteBuf)"); RTUWriteSingleRegister= dll.api("RTUWriteSingleRegister","WORD(BYTE nSlaveAddress, WORD nRegAddress, WORD nUnitValue)"); RTUWriteMultipleRegisters= dll.api("RTUWriteMultipleRegisters","WORD(BYTE nSlaveAddress, WORD nRegAddress, WORD nUnitCount, string& pWriteBuf)"); MesErr = function(s,a){ select(s) { case 0xffff { var sa ="" for(i=1;#a;2){ var now = string.slice(a,i,i+1); sa = sa ++ string.hex(now,"") ;} return sa;} case 0x8002, 0x4002 {return "寄存器地址错误"; } case 0x8001 {return "返回数据错误"; } case 0x2001 {return "modbus地址位错误:1~247"; } case 0x4007 {return "modbus设备不存在"; } case 0x4006 {return "写入数据出错"; } //case 0x4005 {return "写入数据出错"; } case 0x4004 {return "modbus设备不存在"; } case 0x4003 {return "写入数据出错"; } case 0x4001 {return "crc错误"; } else { return "ErrCode:"++s; } } } //a="0"; ReadCoils = function(SAddr,UAddr,Count){ var c = Count/8 > 1 ? (Count-Count%8)/8: 1; c = Count%8 > 0 ? c + 1 : c; var s,a = RTUReadCoils(SAddr,UAddr,Count,raw.buffer(c)) return MesErr(s,a); } ReadHoldingRegisters = function(SAddr,UAddr,Count){ var s,a = RTUReadHoldingRegisters(SAddr,UAddr,Count,raw.buffer(Count*2)) return MesErr(s,a); } ReadInputRegisters = function(SAddr,UAddr,Count){ var s,a = RTUReadInputRegisters(SAddr,UAddr,Count,raw.buffer(Count*2)) //console.dump(s,string.hex(a)) return MesErr(s,a); } /*打开串口 停止位 0代表1个停止位 { [0]=1;[1]=1.5;[2]=2} 写aardio函数要转换 */ openCom = function(Port,Baud,DataBits,parity,stopBits){ stopBit = {["1"] = 0; ["1.5"] = 1; ["2"] = 2; } return RTUOpenCom(Port,Baud,DataBits,parity,stopBit[tostring(stopBits)]) ; } closeCom = function(){ RTUCloseCom(); } ReadDiscreteInputs = function(SAddr,UAddr,Count){ var c = Count/8 > 1 ? (Count-Count%8)/8: 1; c = Count%8 > 0 ? c + 1 : c; var s,a = RTUReadDiscreteInputs(SAddr,UAddr,Count,raw.buffer(c)) return MesErr(s,a); } WriteSingleCoil =function(SAddr,UAddr,Value){ var val = Value > 0 ? 0xff00 : 0; return RTUWriteSingleCoil(SAddr,UAddr,val)==0xffff? true: false; } WriteSingleRegister =function(SAddr,UAddr,Value){ var val = string.format("%04x", Value) return RTUWriteSingleRegister(SAddr,UAddr,val)==0xffff? true: false; } WriteMultipleCoils =function(SAddr,UAddr,Count,Value){ var sendStr=""; var c = Count/8 > 1 ? (Count-Count%8)/8: 1; c = Count%8 > 0 ? c + 1 : c; Value := 0 ; Value = type(Value)==type.string ?Value : string.format("%02x",Value ); if(#Value != c*2) return 1; for(i=1;#Value;2){ var now = string.slice(Value,i,i+1); sendStr = sendStr ++ string.unhex(now," ") ;} return RTUWriteMultipleCoils(SAddr,UAddr,Count,raw.buffer(sendStr))==0xffff? true: false; } WriteMultipleRegisters =function(SAddr,UAddr,Count,Value){ var sendStr=""; Value := 0 ; Value = type(Value)==type.string ?Value : string.format("%04x",Value ); if(#Value != Count*4) return 1; for(i=1;#Value;2){ var now = string.slice(Value,i,i+1); sendStr = sendStr ++ string.unhex(now," ") ;} return RTUWriteMultipleRegisters(SAddr,UAddr,Count,raw.buffer(sendStr))==0xffff? true: false; } console.dump(openCom(15,9600,8,0,1)) var s=ReadInputRegisters(1,0,5); console.dump(s) console.dump( ReadCoils(1,0,16)) console.dump(WriteSingleCoil(1,0,1)) console.dump(WriteSingleRegister(1,0,4)) console.dump(WriteMultipleCoils(1,0,16,"da24")) console.dump(WriteMultipleRegisters(1,0,2,"da24")) closeCom(); console.pause("")
读取和写入功能 控制台显示及串口监控助手显示内容
raw库的一些功能还需要学习怎么用?
3 个回复 | 最后更新于 2021-04-17
//modbusRTU自定义通信库 namespace com.comm.modbus; modbusRTU = class { ctor( /*输入构造函数所需要的参数*/ ){ }; MesErr = function(s,a){ select(s) { case 0xffff { var sa ="" for(i=1;#a;2){var now = string.slice(a,i,i+1); sa = sa ++ string.hex(now,"") ;} return sa;} case 0x8002, 0x4002 {return "寄存器地址错误"; } case 0x8001 {return "返回数据错误"; } case 0x2001 {return "modbus地址位错误:1~247"; } case 0x4007 {return "modbus设备不存在"; } case 0x4006 {return "写入数据出错"; } //case 0x4005 {return "写入数据出错"; } case 0x4004 {return "modbus设备不存在"; } case 0x4003 {return "写入数据出错"; } case 0x4001 {return "crc错误"; } else {return "ErrCode:"++s; } } }; ReadCoils = function(SAddr,UAddr,Count){ var c = Count/8 > 1 ? (Count-Count%8)/8: 1; c = Count%8 > 0 ? c + 1 : c; var s,a = RTUReadCoils(SAddr,UAddr,Count,..raw.buffer(c)) return MesErr(s,a); }; ReadHoldingRegisters = function(SAddr,UAddr,Count){ var s,a = RTUReadHoldingRegisters(SAddr,UAddr,Count,..raw.buffer(Count*2)) return MesErr(s,a); }; ReadInputRegisters = function(SAddr,UAddr,Count){ var s,a = RTUReadInputRegisters(SAddr,UAddr,Count,..raw.buffer(Count*2)) return MesErr(s,a); }; openCom = function(Port,Baud,DataBits,parity,stopBits){ stopBit = {["1"] = 0; ["1.5"] = 1; ["2"] = 2; } return RTUOpenCom(Port,Baud,DataBits,parity,stopBit[tostring(stopBits)]) ; }; closeCom = function(){ RTUCloseCom(); }; ReadDiscreteInputs = function(SAddr,UAddr,Count){ var c = Count/8 > 1 ? (Count-Count%8)/8: 1; c = Count%8 > 0 ? c + 1 : c; var s,a = RTUReadDiscreteInputs(SAddr,UAddr,Count,..raw.buffer(c)) return MesErr(s,a); } WriteSingleCoil =function(SAddr,UAddr,Value){ var val = Value > 0 ? 0xff00 : 0; return RTUWriteSingleCoil(SAddr,UAddr,val)==0xffff? true: false; }; WriteSingleRegister =function(SAddr,UAddr,Value){ var val = string.format("%04x", Value) return RTUWriteSingleRegister(SAddr,UAddr,val)==0xffff? true: false; }; WriteMultipleCoils =function(SAddr,UAddr,Count,Value){ var sendStr=""; var c = Count/8 > 1 ? (Count-Count%8)/8: 1; c = Count%8 > 0 ? c + 1 : c; Value := 0 ; Value = type(Value)==type.string ?Value : string.format("%02x",Value ); if(#Value != c*2) return 1; for(i=1;#Value;2){var now = string.slice(Value,i,i+1); sendStr = sendStr ++ string.unhex(now," ") ;} return RTUWriteMultipleCoils(SAddr,UAddr,Count,..raw.buffer(sendStr))==0xffff? true: false; }; WriteMultipleRegisters =function(SAddr,UAddr,Count,Value){ var sendStr=""; Value := 0 ; Value = type(Value)==type.string ?Value : string.format("%04x",Value ); if(#Value != Count*4) return 1; for(i=1;#Value;2){var now = string.slice(Value,i,i+1); sendStr = sendStr ++ string.unhex(now," ") ;} return RTUWriteMultipleRegisters(SAddr,UAddr,Count,..raw.buffer(sendStr))==0xffff? true: false; }; } namespace modbusRTU{ var dll = ..raw.loadDll($"~\lib\com\comm\modbus\.res\ModbusRTU.dll") RTUOpenCom= dll.api("RTUOpenCom","bool(int nPort, INT dwBaud, BYTE nDataBits, BYTE parity, BYTE stopBits)"); RTUCloseCom= dll.api("RTUCloseCom","void()"); RTUReadCoils= dll.api("RTUReadCoils","WORD(BYTE nSlaveAddress, WORD nUnitAddress, WORD nUnitCount, string& pReturnBuf)"); RTUReadDiscreteInputs= dll.api("RTUReadDiscreteInputs","WORD(BYTE nSlaveAddress, WORD nUnitAddress, WORD nUnitCount, string& pReturnBuf)"); RTUReadHoldingRegisters= dll.api("RTUReadHoldingRegisters","WORD(BYTE nSlaveAddress, WORD nRegAddress, WORD nRegCount, string& pReturnBuf)"); RTUReadInputRegisters= dll.api("RTUReadInputRegisters","WORD(BYTE nSlaveAddress, WORD nRegAddress, WORD nRegCount, string& pReturnBuf)"); RTUWriteSingleCoil= dll.api("RTUWriteSingleCoil","WORD(BYTE nSlaveAddress, WORD nRegAddress, WORD nUnitValue)"); RTUWriteMultipleCoils= dll.api("RTUWriteMultipleCoils","WORD(BYTE nSlaveAddress, WORD nRegAddress, WORD nUnitCount, string& pWriteBuf)"); RTUWriteSingleRegister= dll.api("RTUWriteSingleRegister","WORD(BYTE nSlaveAddress, WORD nRegAddress, WORD nUnitValue)"); RTUWriteMultipleRegisters= dll.api("RTUWriteMultipleRegisters","WORD(BYTE nSlaveAddress, WORD nRegAddress, WORD nUnitCount, string& pWriteBuf)"); } /**intellisense(com.comm.modbus) modbusRTU() = !modbusRTU. end intellisense**/ /**intellisense(!modbusRTU) openCom(.(串口号,波特率,数据位,奇偶,停止位) = @__15,9600,8,0,1) /*打开串口*/ closeCom() = 关闭串口 ReadCoils(.(站点,起始地址,位数) = @1,0,1) /*读取线圈*/ ReadDiscreteInputs(.(站点,起始地址,位数) = @1,0,1)/*离散输入*/ ReadHoldingRegisters(.(站点,起始地址,位数) = @1,0,1)/*保持寄存器*/ ReadInputRegisters(.(站点,起始地址,位数) = @1,0,1)/*输入寄存器*/ WriteSingleCoil(.(站点,起始地址,写入值) = @1,0,1)/*写入单个线圈,写入0代表off,1代表on*/ WriteSingleRegister(.(站点,起始地址,写入值) = @1,0,1)/*写入单个寄存器值*/ WriteMultipleCoils(.(站点,起始地址,位数,写入值) = @1,0,8,"da")/*写入多个线圈,8个线圈为1个字节*/ WriteMultipleRegisters(.(站点,起始地址,位数,写入值) = @1,0,2,"da24") /*写入多个寄存器,1个寄存器2个字节*/ end intellisense**/
登录后方可回帖
谢谢分享.
声明dll的时候貌似不能带初始化参数吧