Skip to content

Fixed reading send data as a received data in ReadCoils method #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 54 additions & 53 deletions EasyModbus/ModbusClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1272,68 +1272,69 @@ public bool[] ReadCoils(int startingAddress, int quantity)
if (debug) StoreLogData.Instance.Store("Receive ModbusTCP-Data: " + BitConverter.ToString(receiveData), System.DateTime.Now);
ReceiveDataChanged(this);
}
}
}
if (data[7] == 0x81 & data[8] == 0x01)
{
if (debug) StoreLogData.Instance.Store("FunctionCodeNotSupportedException Throwed", System.DateTime.Now);
throw new EasyModbus.Exceptions.FunctionCodeNotSupportedException("Function code not supported by master");
}
if (data[7] == 0x81 & data[8] == 0x02)
{
if (debug) StoreLogData.Instance.Store("StartingAddressInvalidException Throwed", System.DateTime.Now);
throw new EasyModbus.Exceptions.StartingAddressInvalidException("Starting address invalid or starting address + quantity invalid");
}
if (data[7] == 0x81 & data[8] == 0x03)
{
if (debug) StoreLogData.Instance.Store("QuantityInvalidException Throwed", System.DateTime.Now);
throw new EasyModbus.Exceptions.QuantityInvalidException("quantity invalid");
}
if (data[7] == 0x81 & data[8] == 0x04)
{
if (debug) StoreLogData.Instance.Store("ModbusException Throwed", System.DateTime.Now);
throw new EasyModbus.Exceptions.ModbusException("error reading");
}
if (serialport != null)
{
crc = BitConverter.GetBytes(calculateCRC(data, (ushort)(data[8]+3), 6));
if ((crc[0] != data[data[8]+9] | crc[1] != data[data[8]+10]) & dataReceived)
{
if (debug) StoreLogData.Instance.Store("CRCCheckFailedException Throwed", System.DateTime.Now);
if (NumberOfRetries <= countRetries)
if (data[7] == 0x81 & data[8] == 0x01)
{
countRetries = 0;
throw new EasyModbus.Exceptions.CRCCheckFailedException("Response CRC check failed");
if (debug) StoreLogData.Instance.Store("FunctionCodeNotSupportedException Throwed", System.DateTime.Now);
throw new EasyModbus.Exceptions.FunctionCodeNotSupportedException("Function code not supported by master");
}
else
if (data[7] == 0x81 & data[8] == 0x02)
{
countRetries++;
return ReadCoils(startingAddress, quantity);
if (debug) StoreLogData.Instance.Store("StartingAddressInvalidException Throwed", System.DateTime.Now);
throw new EasyModbus.Exceptions.StartingAddressInvalidException("Starting address invalid or starting address + quantity invalid");
}
}
else if (!dataReceived)
{
if (debug) StoreLogData.Instance.Store("TimeoutException Throwed", System.DateTime.Now);
if (NumberOfRetries <= countRetries)
if (data[7] == 0x81 & data[8] == 0x03)
{
countRetries = 0;
throw new TimeoutException("No Response from Modbus Slave");
if (debug) StoreLogData.Instance.Store("QuantityInvalidException Throwed", System.DateTime.Now);
throw new EasyModbus.Exceptions.QuantityInvalidException("quantity invalid");
}
else
if (data[7] == 0x81 & data[8] == 0x04)
{
countRetries++;
return ReadCoils(startingAddress, quantity);
if (debug) StoreLogData.Instance.Store("ModbusException Throwed", System.DateTime.Now);
throw new EasyModbus.Exceptions.ModbusException("error reading");
}
if (serialport != null)
{
crc = BitConverter.GetBytes(calculateCRC(data, (ushort)(data[8] + 3), 6));
if ((crc[0] != data[data[8] + 9] | crc[1] != data[data[8] + 10]) & dataReceived)
{
if (debug) StoreLogData.Instance.Store("CRCCheckFailedException Throwed", System.DateTime.Now);
if (NumberOfRetries <= countRetries)
{
countRetries = 0;
throw new EasyModbus.Exceptions.CRCCheckFailedException("Response CRC check failed");
}
else
{
countRetries++;
return ReadCoils(startingAddress, quantity);
}
}
else if (!dataReceived)
{
if (debug) StoreLogData.Instance.Store("TimeoutException Throwed", System.DateTime.Now);
if (NumberOfRetries <= countRetries)
{
countRetries = 0;
throw new TimeoutException("No Response from Modbus Slave");
}
else
{
countRetries++;
return ReadCoils(startingAddress, quantity);
}
}
}
response = new bool[quantity];
for (int i = 0; i < quantity; i++)
{
int intData = data[9 + i / 8];
int mask = Convert.ToInt32(Math.Pow(2, (i % 8)));
response[i] = Convert.ToBoolean((intData & mask) / mask);
}
return (response);
}
}
response = new bool[quantity];
for (int i = 0; i < quantity; i++)
{
int intData = data[9+i/8];
int mask = Convert.ToInt32(Math.Pow(2, (i%8)));
response[i] = Convert.ToBoolean((intData & mask)/mask);
}
return (response);
}
return null;
}

/// <summary>
Expand Down