// MODBUS读保持寄存器 iAddress 开始地址(0开始),iLength 寄存器数量 //主站请求:01 03 00 00 00 06 70 08 //地址 1字节 //功能码 1字节 0x03 //起始寄存器地址 2字节 0x0000~0x0005 //寄存器数量 2字节 0x01~0x06 //CRC校验 2字节 public byte[] ReadKeepReg(int iDevAdd, long iAddress, int iLength) { byte[] ResByte = null; iMWordStartAddr = iAddress; iMWordLen = iLength; if (comBusying == true) { Thread.Sleep(30); } byte[] SendCommand = new byte[8]; CurrentAddr = iDevAdd - 1; SendCommand[0] = (byte)iDevAdd; SendCommand[1] = 0x03; SendCommand[2] = (byte)((iAddress - iAddress % 256) / 256); SendCommand[3] = (byte)(iAddress % 256); SendCommand[4] = (byte)((iLength - iLength % 256) / 256); SendCommand[5] = (byte)(iLength % 256); MmUtils.Crc16(SendCommand, 6); SendCommand[6] = MmUtils.ucCRCLo; SendCommand[7] = MmUtils.ucCRCHi; try { //发送指令 SyncWtire(SendCommand, 0, 8); } catch { return(ResByte); } mReceiveByteCount = 0; strUpData = ""; Thread.Sleep(100); ResByte = MmUtils.HexStringToByteArray(this.strUpData); return(ResByte); }
//MODBUS预置双字寄存器 iAddress 开始地址(0开始) //主站请求:01 06 00 00 00 10 70 08 //地址 1字节 //功能码 1字节 0x10 //起始寄存器地址 2字节 0x0000~0x0005 //寄存器数量 2字节 0x01~0x06 //CRC校验 2字节 public byte[] PreSetFloatKeepReg(int iDevAdd, long iAddress, float SetValue) { byte[] ResByte = null; int i; byte[] bSetValue = new byte[4]; bSetValue = BitConverter.GetBytes(SetValue); //bSetValue = &SetValue; iMWordStartAddr = iAddress; //iMWordLen = 0; if (comBusying == true) { Thread.Sleep(250); } byte[] SendCommand = new byte[13]; CurrentAddr = iDevAdd - 1; SendCommand[0] = (byte)iDevAdd; SendCommand[1] = 0x10; SendCommand[2] = (byte)((iAddress - iAddress % 256) / 256); SendCommand[3] = (byte)(iAddress % 256); SendCommand[4] = 0x00; SendCommand[5] = 0x02; SendCommand[6] = 0x04; SendCommand[7] = bSetValue[3]; SendCommand[8] = bSetValue[2]; SendCommand[9] = bSetValue[1]; SendCommand[10] = bSetValue[0]; MmUtils.Crc16(SendCommand, 11); SendCommand[11] = ucCRCLo; SendCommand[12] = ucCRCHi; strUpData = ""; try { //发送指令。 SyncWtire(SendCommand, 0, 13); Thread.Sleep(200); } catch (Exception) { return(ResByte); } mReceiveByteCount = 0; strDownData = ""; for (i = 0; i < 13; i++) { strDownData = strDownData + " " + SendCommand[i].ToString("X2"); } comBusying = true; //设置串口忙标志 bCommWell = false; //设置本次通讯标志 ResByte = MmUtils.HexStringToByteArray(this.strUpData); return(ResByte); }
//MODBUS读输出状态 iAddress 开始地址(0开始),iLength 寄存器数量 //主站请求:01 01 00 00 00 07 70 08 //地址 1字节 //功能码 1字节 0x01 //起始寄存器地址 2字节 0x0000~0x0005 //寄存器数量 2字节 0x01~0x06 //CRC校验 2字节 public byte[] ReadOutputStatus(int iDevAdd, long iAddress, int iLength) { byte[] ResByte = null; //一个字节代表8个位状态 //一个字节代表8个位状态 if (iLength % 8 == 0) { iMBitLen = iLength / 8; } else { iMBitLen = iLength / 8 + 1; } iMBitStartAddr = iAddress; if (comBusying == true) { Thread.Sleep(250); } byte[] SendCommand = new byte[8]; CurrentAddr = iDevAdd - 1; SendCommand[0] = (byte)iDevAdd; SendCommand[1] = 0x01; SendCommand[2] = (byte)((iAddress - iAddress % 256) / 256); SendCommand[3] = (byte)(iAddress % 256); SendCommand[4] = (byte)((iLength - iLength % 256) / 256); SendCommand[5] = (byte)(iLength % 256); MmUtils.Crc16(SendCommand, 6); SendCommand[6] = ucCRCLo; SendCommand[7] = ucCRCHi; try { //发送指令。 SyncWtire(SendCommand, 0, 8); } catch (Exception) { return(ResByte); } mReceiveByteCount = 0; strUpData = ""; //strDownData = ""; //for (i = 0; i < 8; i++) //{ // strDownData = strDownData + " " + SendCommand[i].ToString("X2"); //} Thread.Sleep(300); ResByte = MmUtils.HexStringToByteArray(this.strUpData); return(ResByte); }
//MODBUS预置单字寄存器 iAddress 开始地址(0开始),iHiValue 数据 //主站请求:01 06 00 00 00 06 70 08 //地址 1字节 //功能码 1字节 0x06 //起始寄存器地址 2字节 0x0000~0x0005 //寄存器数量 2字节 0x01~0x06 //CRC校验 2字节 public byte[] PreSetKeepReg(int iDevAdd, long iAddress, UInt16 SetValue) { int i; iMWordStartAddr = iAddress; byte[] ResByte = null; //iMWordLen = 0; if (comBusying == true) { Thread.Sleep(250); } byte[] SendCommand = new byte[8]; CurrentAddr = iDevAdd - 1; SendCommand[0] = (byte)iDevAdd; SendCommand[1] = 0x06; SendCommand[2] = (byte)((iAddress - iAddress % 256) / 256); SendCommand[3] = (byte)(iAddress % 256); SendCommand[4] = (byte)((SetValue - SetValue % 256) / 256);; SendCommand[5] = (byte)(SetValue % 256);; MmUtils.Crc16(SendCommand, 6); SendCommand[6] = ucCRCLo; SendCommand[7] = ucCRCHi; strUpData1 = ""; //发送指令。 try { SyncWtire(SendCommand, 0, 8); Thread.Sleep(100); } catch (Exception ex) { return(ResByte); } mReceiveByteCount = 0; strDownData = ""; for (i = 0; i < 8; i++) { strDownData = strDownData + " " + SendCommand[i].ToString("X2"); } comBusying = true; //设置串口忙标志 bCommWell = false; //设置本次通讯标志 ResByte = MmUtils.HexStringToByteArray(this.strUpData); return(ResByte); }
//MODBUS复位单线圈 iAddress 开始地址(0开始) //主站请求:01 05 00 00 00 00 70 08 //地址 1字节 //功能码 1字节 0x05 //起始寄存器地址 2字节 0x0000~0x0005 //寄存器数量 2字节 0x01~0x06 //CRC校验 2字节 public byte[] ForceOff(int iDevAdd, long iAddress) { // bool Success = true; byte[] ResByte = null; iMWordStartAddr = iAddress; //iMWordLen = 0; if (comBusying == true) { Thread.Sleep(250); } byte[] SendCommand = new byte[8]; CurrentAddr = iDevAdd - 1; SendCommand[0] = (byte)iDevAdd; SendCommand[1] = 0x05; SendCommand[2] = (byte)((iAddress - iAddress % 256) / 256); SendCommand[3] = (byte)(iAddress % 256); SendCommand[4] = 0x00; SendCommand[5] = 0x00; MmUtils.Crc16(SendCommand, 6); SendCommand[6] = ucCRCLo; SendCommand[7] = ucCRCHi; strUpData = ""; try { //发送指令。 SyncWtire(SendCommand, 0, 8); Thread.Sleep(100); } catch (Exception) { return(ResByte); } mReceiveByteCount = 0; strUpData = ""; //strDownData = ""; //for (i = 0; i < 8; i++) //{ // strDownData = strDownData + " " + SendCommand[i].ToString("X2"); //} comBusying = true; //设置串口忙标志 bCommWell = false; //设置本次通讯标志 ResByte = MmUtils.HexStringToByteArray(this.strUpData); return(ResByte); }
// MODBUS强制单线圈 iAddress 开始地址(0开始) //主站请求:01 05 00 00 FF 00 70 08 //地址 1字节 //功能码 1字节 0x05 //起始寄存器地址 2字节 0x0000~0x0005 //寄存器数量 2字节 0x01~0x06 //CRC校验 2字节 public byte[] ForceOn(int iDevAdd, long iAddress) { // bool Success = true; byte[] ResByte = null; iMWordStartAddr = iAddress; //iMWordLen = 0; if (comBusying == true) { Thread.Sleep(250); } byte[] SendCommand = new byte[8]; CurrentAddr = iDevAdd - 1; SendCommand[0] = (byte)iDevAdd; SendCommand[1] = 0x05; SendCommand[2] = (byte)((iAddress - iAddress % 256) / 256); SendCommand[3] = (byte)(iAddress % 256); SendCommand[4] = 0xff; SendCommand[5] = 0x00; MmUtils.Crc16(SendCommand, 6); SendCommand[6] = ucCRCLo; SendCommand[7] = ucCRCHi; strUpData = ""; try { //发送指令。 SyncWtire(SendCommand, 0, 8); Thread.Sleep(100); } catch { return(ResByte); } mReceiveByteCount = 0; ResByte = MmUtils.HexStringToByteArray(this.strUpData); return(ResByte); // return Success; }