public int WriteMultipleRegister(int id, int startAddress, byte[] values) { int len = values.Length; if (len % 2 > 0) { len++; } byte[] data = new byte[len + 9]; data[0] = (byte)id; // Slave id high byte Slave address data[1] = Modbus.fctWriteMultipleRegister; // Function code function code byte[] _adr = BitConverter.GetBytes((short)startAddress); data[2] = _adr[1]; // Start address Start address high eight data[3] = _adr[0]; // Start address The lower eight bits of the start address byte[] _length = BitConverter.GetBytes((short)(len >> 1)); data[4] = _length[1]; // Number of data to read The upper eight bits of the number of registers data[5] = _length[0]; // Number of data to read The lower eight registers data[6] = (byte)len; //Bytes Array.Copy(values, 0, data, 7, len); //Add change data to data byte[] arr = Utility.CalculateCrc(data, len + 7); data[len + 7] = arr[0]; //The lower eight bits of CRC check data[len + 8] = arr[1]; //The high eight bits of CRC check lock (_async) { _serialPort.Write(data, 0, data.Length); int numBytesRead = 0; var frameBytes = new byte[5]; while (numBytesRead != frameBytes.Length) { numBytesRead += _serialPort.Read(frameBytes, numBytesRead, frameBytes.Length - numBytesRead); } var slave = frameBytes[0]; var code = frameBytes[1]; if (code == 0x85)//Errors only need to read 5 bytes { var errorcode = frameBytes[2]; if (OnError != null) { OnError(this, new IOErrorEventArgs(Modbus.GetErrorString(errorcode))); } Thread.Sleep(10); return(-1); } else//8 bytes is required { numBytesRead = 0; while (numBytesRead < 3) { numBytesRead += _serialPort.Read(frameBytes, numBytesRead, 3 - numBytesRead); } Thread.Sleep(10); return(0); } } }
public int WriteMultipleRegister(int id, int startAddress, byte[] values) { int len = values.Length; if (len % 2 > 0) { len++; } byte[] data = new byte[len + 9]; data[0] = (byte)id; // Slave id high byte 从站地址 data[1] = Modbus.fctWriteMultipleRegister; // Function code 功能码 byte[] _adr = BitConverter.GetBytes((short)startAddress); data[2] = _adr[1]; // Start address 开始地址高八位 data[3] = _adr[0]; // Start address 开始地址低八位 byte[] _length = BitConverter.GetBytes((short)(len >> 1)); data[4] = _length[1]; // Number of data to read 寄存器数量高八位 data[5] = _length[0]; // Number of data to read 寄存器数量低八位 data[6] = (byte)len; //字节数 Array.Copy(values, 0, data, 7, len); //把变更数据加入data中 byte[] arr = Utility.CalculateCrc(data, len + 7); data[len + 7] = arr[0]; //crc校验的低八位 data[len + 8] = arr[1]; //CRC校验的高八位 lock (_async) { _serialPort.Write(data, 0, data.Length); int numBytesRead = 0; var frameBytes = new byte[5]; while (numBytesRead != frameBytes.Length) { numBytesRead += _serialPort.Read(frameBytes, numBytesRead, frameBytes.Length - numBytesRead); } var slave = frameBytes[0]; var code = frameBytes[1]; if (code == 0x85)//错误则只需读5字节 { var errorcode = frameBytes[2]; if (OnError != null) { OnError(this, new IOErrorEventArgs(Modbus.GetErrorString(errorcode))); } Thread.Sleep(10); return(-1); } else//正确需8字节 { numBytesRead = 0; while (numBytesRead < 3) { numBytesRead += _serialPort.Read(frameBytes, numBytesRead, 3 - numBytesRead); } Thread.Sleep(10); return(0); } } }
internal void CallException(int id, byte function, byte exception) { if (tcpSynCl == null) { return; } if (OnError != null) { OnError(this, new IOErrorEventArgs(Modbus.GetErrorString(exception))); } }
public int WriteSingleCoils(int id, int startAddress, bool OnOff) { byte[] data = new byte[8]; data[0] = (byte)id; // Slave id high byte data[1] = Modbus.fctWriteSingleCoil; // Function code byte[] _adr = BitConverter.GetBytes((short)startAddress); data[2] = _adr[1]; // Start address data[3] = _adr[0]; // Start address if (OnOff) { data[4] = 0xFF; } byte[] arr = Utility.CalculateCrc(data, 6); data[6] = arr[0]; data[7] = arr[1]; lock (_async) { _serialPort.Write(data, 0, data.Length); int numBytesRead = 0; var frameBytes = new byte[5]; while (numBytesRead != frameBytes.Length) { numBytesRead += _serialPort.Read(frameBytes, numBytesRead, frameBytes.Length - numBytesRead); } var slave = frameBytes[0]; var code = frameBytes[1]; if (code == 0x85)//错误则只需读5字节 { var errorcode = frameBytes[2]; if (OnError != null) { OnError(this, new IOErrorEventArgs(Modbus.GetErrorString(errorcode))); } Thread.Sleep(10); return(-1); } else//正确需8字节 { numBytesRead = 0; while (numBytesRead < 3) { numBytesRead += _serialPort.Read(frameBytes, numBytesRead, 3 - numBytesRead); } Thread.Sleep(10); return(0); } } }
internal void CallException(int id, byte function, byte exception) { if (tcpSynCl == null) { return; } //主动断开连接准备重连 if (exception == Modbus.excExceptionConnectionLost) { tcpSynCl.Close(); } if (OnError != null) { OnError(this, new IOErrorEventArgs(Modbus.GetErrorString(exception))); } }
public int WriteSingleRegister(int id, int startAddress, byte[] values) { byte[] data = new byte[8]; data[0] = (byte)id; // Slave id high byte Slave address High eight data[1] = Modbus.fctWriteSingleRegister; // Function code function code byte[] _adr = BitConverter.GetBytes((short)startAddress); data[2] = _adr[1]; // Start address Start address high eight data[3] = _adr[0]; // Start address Start address high eight data[4] = values[1]; //Change the high bit of the data data[5] = values[0]; //Change the low bit of data byte[] arr = Utility.CalculateCrc(data, 6); data[6] = arr[0]; //The lower eight bits of the CRC check code data[7] = arr[1]; //The upper eight bits of the CRC check code lock (_async) { _serialPort.Write(data, 0, data.Length); int numBytesRead = 0; var frameBytes = new byte[5]; while (numBytesRead != frameBytes.Length) { numBytesRead += _serialPort.Read(frameBytes, numBytesRead, frameBytes.Length - numBytesRead); } var slave = frameBytes[0]; var code = frameBytes[1]; if (code == 0x85)//Errors only need to read 5 bytes { var errorcode = frameBytes[2]; if (OnError != null) { OnError(this, new IOErrorEventArgs(Modbus.GetErrorString(errorcode))); } Thread.Sleep(10); return(-1); } else//8 bytes is required { numBytesRead = 0; while (numBytesRead < 3) { numBytesRead += _serialPort.Read(frameBytes, numBytesRead, 3 - numBytesRead); } Thread.Sleep(10); return(0); } } }
public int WriteSingleRegister(int id, int startAddress, byte[] values) { byte[] data = new byte[8]; data[0] = (byte)id; // Slave id high byte 从站地址高八位 data[1] = Modbus.fctWriteSingleRegister; // Function code 功能码 byte[] _adr = BitConverter.GetBytes((short)startAddress); data[2] = _adr[1]; // Start address 开始地址高八位 data[3] = _adr[0]; // Start address 开始地址高八位 data[4] = values[1]; //变更数据的高位 data[5] = values[0]; //变更数据的低位 byte[] arr = Utility.CalculateCrc(data, 6); data[6] = arr[0]; //CRC校验码低八位 data[7] = arr[1]; //CRC校验码高八位 lock (_async) { _serialPort.Write(data, 0, data.Length); int numBytesRead = 0; var frameBytes = new byte[5]; while (numBytesRead != frameBytes.Length) { numBytesRead += _serialPort.Read(frameBytes, numBytesRead, frameBytes.Length - numBytesRead); } var slave = frameBytes[0]; var code = frameBytes[1]; if (code == 0x85)//错误则只需读5字节 { var errorcode = frameBytes[2]; if (OnError != null) { OnError(this, new IOErrorEventArgs(Modbus.GetErrorString(errorcode))); } return(-1); } else//正确需8字节 { numBytesRead = 0; while (numBytesRead < 3) { numBytesRead += _serialPort.Read(frameBytes, numBytesRead, 3 - numBytesRead); } return(0); } } }