public void WriteMultipleRegisters(int startingAddress, int[] values) { transactionIdentifierInternal++; if (tcpClient == null & !udpFlag) { throw new EasyModbusV2.Exceptions.ConnectionException("connection error"); } byte byteCount = (byte)(values.Length * 2); byte[] quantityOfOutputs = BitConverter.GetBytes((int)values.Length); this.length = BitConverter.GetBytes((int)(7 + values.Length * 2)); this.functionCode = 0x10; this.startingAddress = BitConverter.GetBytes(startingAddress); Byte[] data = new byte[7 + 2 + values.Length * 2]; data[0] = this.unitIdentifier; data[1] = this.functionCode; data[2] = this.startingAddress[1]; data[3] = this.startingAddress[0]; data[4] = quantityOfOutputs[1]; data[5] = quantityOfOutputs[0]; data[6] = byteCount; for (int i = 0; i < values.Length; i++) { byte[] singleRegisterValue = BitConverter.GetBytes((int)values[i]); data[7 + i * 2] = singleRegisterValue[1]; data[8 + i * 2] = singleRegisterValue[0]; } crc = BitConverter.GetBytes(calculateCRC(data, (ushort)(data.Length - 2), 0)); data[data.Length - 2] = crc[0]; data[data.Length - 1] = crc[1]; data = SendReceiveData(data); CheckResponseError(0x90, data[7], data[8]); #region check data on valid (check crc) if it is wrong, try to reread crc = BitConverter.GetBytes(CrcCalculator.calculateCRC(data, 6, 6)); if ((crc[0] != data[12] | crc[1] != data[13])) { Console.WriteLine("ошибка crc " + countRetries); if (NumberOfRetries <= countRetries) { countRetries = 0; throw new EasyModbusV2.Exceptions.CRCCheckFailedException("Response CRC check failed"); } else { countRetries++; WriteMultipleRegisters(startingAddress, values); } } #endregion }
public void WriteSingleRegister(int startingAddress, int value) { transactionIdentifierInternal++; if (tcpClient == null & !udpFlag) { throw new EasyModbusV2.Exceptions.ConnectionException("connection error"); } this.length = BitConverter.GetBytes((int)0x0006); this.functionCode = 0x06; this.startingAddress = BitConverter.GetBytes(startingAddress); byte[] registerValue = new byte[2]; registerValue = BitConverter.GetBytes((int)value); Byte[] data = new byte[] { this.unitIdentifier, this.functionCode, this.startingAddress[1], this.startingAddress[0], registerValue[1], registerValue[0], this.crc[0], this.crc[1] }; crc = BitConverter.GetBytes(calculateCRC(data, 6, 0)); data[6] = crc[0]; data[7] = crc[1]; data = SendReceiveData(data); CheckResponseError(0x86, data[7], data[8]); #region check data on valid (check crc) if it is wrong, try to reread crc = BitConverter.GetBytes(CrcCalculator.calculateCRC(data, 6, 6)); if ((crc[0] != data[12] | crc[1] != data[13])) { Console.WriteLine("ошибка crc " + countRetries); if (NumberOfRetries <= countRetries) { countRetries = 0; throw new EasyModbusV2.Exceptions.CRCCheckFailedException("Response CRC check failed"); } else { countRetries++; WriteSingleRegister(startingAddress, value); } } #endregion }
public bool[] ReadCoils(int startingAddress, int quantity) { transactionIdentifierInternal++; if (tcpClient == null & !udpFlag) { throw new EasyModbusV2.Exceptions.ConnectionException("connection error"); } CheckAddressAndQuantity(startingAddress, quantity); bool[] response; this.length = BitConverter.GetBytes((int)0x0006); this.functionCode = 0x01; this.startingAddress = BitConverter.GetBytes(startingAddress); this.quantity = BitConverter.GetBytes(quantity); Byte[] data = new byte[] { this.unitIdentifier, this.functionCode, this.startingAddress[1], this.startingAddress[0], this.quantity[1], this.quantity[0], this.crc[0], this.crc[1] }; crc = BitConverter.GetBytes(calculateCRC(data, 6, 0)); data[6] = crc[0]; data[7] = crc[1]; data = SendReceiveData(data); CheckResponseError(0x81, data[7], data[8]); crc = BitConverter.GetBytes(CrcCalculator.calculateCRC(data, (ushort)(data[8] + 3), 6)); #region check data on valid (check crc) if it is wrong, try to reread crc = BitConverter.GetBytes(CrcCalculator.calculateCRC(data, (ushort)(data[8] + 3), 6)); if ((crc[0] != data[data[8] + 9] | crc[1] != data[data[8] + 10])) { Console.WriteLine("ошибка crc " + countRetries); if (NumberOfRetries <= countRetries) { countRetries = 0; throw new EasyModbusV2.Exceptions.CRCCheckFailedException("Response CRC check failed"); } else { countRetries++; return(ReadDiscreteInputs(startingAddress, quantity)); } } #endregion return(ResponseToBit(data, quantity)); }
public int[] ReadWriteMultipleRegisters(int startingAddressRead, int quantityRead, int startingAddressWrite, int[] values) { transactionIdentifierInternal++; //check connection if (tcpClient == null & !udpFlag) { throw new EasyModbusV2.Exceptions.ConnectionException("connection error"); } ///check quantity CheckAddressAndQuantityForReadWrite(startingAddressRead, quantityRead, startingAddressWrite, values.Length); this.transactionIdentifier = BitConverter.GetBytes((uint)transactionIdentifierInternal); this.protocolIdentifier = BitConverter.GetBytes((int)0x0000); this.length = BitConverter.GetBytes((int)0 + values.Length * 2); this.functionCode = 0x17; byte[] startingAddressReadLocal = new byte[2]; byte[] quantityReadLocal = new byte[2]; byte[] startingAddressWriteLocal = new byte[2]; byte[] quantityWriteLocal = new byte[2]; byte writeByteCountLocal = 0; startingAddressReadLocal = BitConverter.GetBytes(startingAddressRead); quantityReadLocal = BitConverter.GetBytes(quantityRead); startingAddressWriteLocal = BitConverter.GetBytes(startingAddressWrite); quantityWriteLocal = BitConverter.GetBytes(values.Length); writeByteCountLocal = Convert.ToByte(values.Length * 2); Byte[] data = new byte[11 + 2 + values.Length * 2]; data[0] = this.unitIdentifier; data[1] = this.functionCode; data[2] = startingAddressReadLocal[1]; data[3] = startingAddressReadLocal[0]; data[4] = quantityReadLocal[1]; data[5] = quantityReadLocal[0]; data[6] = startingAddressWriteLocal[1]; data[7] = startingAddressWriteLocal[0]; data[8] = quantityWriteLocal[1]; data[9] = quantityWriteLocal[0]; data[10] = writeByteCountLocal; for (int i = 0; i < values.Length; i++) { byte[] singleRegisterValue = BitConverter.GetBytes((int)values[i]); data[11 + i * 2] = singleRegisterValue[1]; data[12 + i * 2] = singleRegisterValue[0]; } crc = BitConverter.GetBytes(calculateCRC(data, (ushort)(data.Length - 2), 0)); data[data.Length - 2] = crc[0]; data[data.Length - 1] = crc[1]; data = SendReceiveData(data); CheckResponseError(0x97, data[7], data[8]); #region check data on valid (check crc) if it is wrong, try to reread crc = BitConverter.GetBytes(CrcCalculator.calculateCRC(data, (ushort)(data[8] + 3), 6)); if ((crc[0] != data[data[8] + 9] | crc[1] != data[data[8] + 10])) { Console.WriteLine("ошибка crc " + countRetries); if (NumberOfRetries <= countRetries) { countRetries = 0; throw new EasyModbusV2.Exceptions.CRCCheckFailedException("Response CRC check failed"); } else { countRetries++; return(ReadWriteMultipleRegisters(startingAddressRead, quantityRead, startingAddressWrite, values)); } } #endregion return(ResponseToInt(data, values.Length)); }
public void WriteMultipleCoils(int startingAddress, bool[] values) { transactionIdentifierInternal++; if (tcpClient == null & !udpFlag) { throw new EasyModbusV2.Exceptions.ConnectionException("connection error"); } byte byteCount = (byte)((values.Length % 8 != 0 ? values.Length / 8 + 1 : (values.Length / 8))); byte[] quantityOfOutputs = BitConverter.GetBytes((int)values.Length); this.length = BitConverter.GetBytes((int)(7 + byteCount)); this.functionCode = 0x0F; this.startingAddress = BitConverter.GetBytes(startingAddress); Byte[] data = new byte[8 + 2 + (values.Length % 8 != 0 ? values.Length / 8 : (values.Length / 8) - 1)]; data[0] = this.unitIdentifier; data[1] = this.functionCode; data[2] = this.startingAddress[1]; data[3] = this.startingAddress[0]; data[4] = quantityOfOutputs[1]; data[5] = quantityOfOutputs[0]; data[6] = byteCount; byte singleCoilValue = 0; for (int i = 0; i < values.Length; i++) { if ((i % 8) == 0) { singleCoilValue = 0; } byte CoilValue; if (values[i] == true) { CoilValue = 1; } else { CoilValue = 0; } singleCoilValue = (byte)((int)CoilValue << (i % 8) | (int)singleCoilValue); data[7 + (i / 8)] = singleCoilValue; } crc = BitConverter.GetBytes(calculateCRC(data, (ushort)(data.Length - 2), 0)); data[data.Length - 2] = crc[0]; data[data.Length - 1] = crc[1]; data = SendReceiveData(data); CheckResponseError(0x8F, data[7], data[8]); #region check data on valid (check crc) if it is wrong, try to reread crc = BitConverter.GetBytes(CrcCalculator.calculateCRC(data, 6, 6)); if ((crc[0] != data[12] | crc[1] != data[13])) { Console.WriteLine("ошибка crc " + countRetries); if (NumberOfRetries <= countRetries) { countRetries = 0; throw new EasyModbusV2.Exceptions.CRCCheckFailedException("Response CRC check failed"); } else { countRetries++; WriteMultipleCoils(startingAddress, values); } } #endregion }
/// <summary> /// Read Input Registers from Master device (FC4). /// </summary> /// <param name="startingAddress">First input register to be read</param> /// <param name="quantity">Number of input registers to be read</param> /// <returns>Int Array which contains the input registers</returns> public int[] ReadInputRegisters(int startingAddress, int quantity) { transactionIdentifierInternal++; if (tcpClient == null & !udpFlag) { if (debug) { LogData.Instance.Message("ConnectionException Throwed", System.DateTime.Now); } throw new EasyModbusV2.Exceptions.ConnectionException("connection error"); } CheckAddressAndQuantity(startingAddress, quantity); int[] response; this.transactionIdentifier = BitConverter.GetBytes((uint)transactionIdentifierInternal); this.protocolIdentifier = BitConverter.GetBytes((int)0x0000); this.length = BitConverter.GetBytes((int)0x0006); this.functionCode = 0x04; this.startingAddress = BitConverter.GetBytes(startingAddress); this.quantity = BitConverter.GetBytes(quantity); Byte[] data = new byte[] { this.transactionIdentifier[1], this.transactionIdentifier[0], this.protocolIdentifier[1], this.protocolIdentifier[0], this.length[1], this.length[0], this.unitIdentifier, this.functionCode, this.startingAddress[1], this.startingAddress[0], this.quantity[1], this.quantity[0], this.crc[0], this.crc[1] }; crc = BitConverter.GetBytes(CrcCalculator.calculateCRC(data, 6, 0)); data[12] = crc[0]; data[13] = crc[1]; if (tcpClient.Client.Connected | udpFlag) { if (udpFlag) { UdpClient udpClient = new UdpClient(); IPEndPoint endPoint = new IPEndPoint(System.Net.IPAddress.Parse(ipAddress), port); udpClient.Send(data, data.Length - 2, endPoint); portOut = ((IPEndPoint)udpClient.Client.LocalEndPoint).Port; udpClient.Client.ReceiveTimeout = 5000; endPoint = new IPEndPoint(System.Net.IPAddress.Parse(ipAddress), portOut); data = udpClient.Receive(ref endPoint); } else { stream.Write(data, 0, data.Length - 2); if (debug) { byte [] debugData = new byte[data.Length - 2]; Array.Copy(data, 0, debugData, 0, data.Length - 2); if (debug) { LogData.Instance.Message("Send ModbusTCP-Data: " + BitConverter.ToString(debugData), System.DateTime.Now); } } if (SendDataChanged != null) { sendData = new byte[data.Length - 2]; Array.Copy(data, 0, sendData, 0, data.Length - 2); SendDataChanged(this); } data = new Byte[2100]; int NumberOfBytes = stream.Read(data, 0, data.Length); if (ReceiveDataChanged != null) { receiveData = new byte[NumberOfBytes]; Array.Copy(data, 0, receiveData, 0, NumberOfBytes); if (debug) { LogData.Instance.Message("Receive ModbusTCP-Data: " + BitConverter.ToString(receiveData), System.DateTime.Now); } ReceiveDataChanged(this); } } } CheckResponseError(0x84, data[7], data[8]); return(ResponseToInt(data, quantity)); }