added data byte counter for incoming port data
This commit is contained in:
27
Datasheet.cs
27
Datasheet.cs
@@ -26,7 +26,7 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
static bool responseReceived = false;
|
static bool responseReceived = false;
|
||||||
|
|
||||||
|
|
||||||
bool isValveClosed = false;
|
bool isValveClosed = false;
|
||||||
bool alarmStatus = false;
|
bool alarmStatus = false;
|
||||||
bool cleaningStatus = false;
|
bool cleaningStatus = false;
|
||||||
|
|
||||||
@@ -257,18 +257,18 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
catch (Exception err) { MessageBox.Show(err.Message); }
|
catch (Exception err) { MessageBox.Show(err.Message); }
|
||||||
}
|
}
|
||||||
|
|
||||||
//Опрос всех записей
|
|
||||||
async Task<bool> PollEntry(Entry entry)
|
async Task<bool> PollEntry(Entry entry)
|
||||||
{
|
{
|
||||||
|
latestMessage = null;
|
||||||
bool res = false;
|
bool res = false;
|
||||||
isAwaitingResponse = true;
|
isAwaitingResponse = true;
|
||||||
Modbus.ReadRegAsync(modbusID, (FunctionCode)entry.registerType, entry.address, entry.length);
|
|
||||||
|
|
||||||
|
Modbus.ReadRegAsync(modbusID, (FunctionCode)entry.registerType, entry.address, entry.length);
|
||||||
stopwatch.Restart();
|
stopwatch.Restart();
|
||||||
|
|
||||||
while (isAwaitingResponse)
|
while (isAwaitingResponse && latestMessage == null)
|
||||||
{
|
{
|
||||||
if (stopwatch.ElapsedMilliseconds > 1000)
|
if (stopwatch.ElapsedMilliseconds > 5000)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Response timed out.");
|
Console.WriteLine("Response timed out.");
|
||||||
break;
|
break;
|
||||||
@@ -287,12 +287,13 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
{
|
{
|
||||||
byte newID = (byte)nudModbusID.Value; // should prevent assigning wrong ID if UpDown is fiddled with in the middle of request
|
byte newID = (byte)nudModbusID.Value; // should prevent assigning wrong ID if UpDown is fiddled with in the middle of request
|
||||||
isAwaitingResponse = true;
|
isAwaitingResponse = true;
|
||||||
|
latestMessage = null;
|
||||||
Modbus.WriteSingleAsync(FunctionCode.WriteRegister, modbusID, 128, newID);
|
Modbus.WriteSingleAsync(FunctionCode.WriteRegister, modbusID, 128, newID);
|
||||||
|
|
||||||
stopwatch.Restart();
|
stopwatch.Restart();
|
||||||
while (isAwaitingResponse)
|
while (isAwaitingResponse)
|
||||||
{
|
{
|
||||||
if (stopwatch.ElapsedMilliseconds > 1000)
|
if (stopwatch.ElapsedMilliseconds > port.ReadTimeout)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Response timed out.");
|
Console.WriteLine("Response timed out.");
|
||||||
break;
|
break;
|
||||||
@@ -308,12 +309,13 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
{
|
{
|
||||||
ushort value = isValveClosed ? (ushort)0 : (ushort)0xFF00;
|
ushort value = isValveClosed ? (ushort)0 : (ushort)0xFF00;
|
||||||
isAwaitingResponse = true;
|
isAwaitingResponse = true;
|
||||||
|
latestMessage = null;
|
||||||
Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.valveStatus.address, value);
|
Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.valveStatus.address, value);
|
||||||
|
|
||||||
stopwatch.Restart();
|
stopwatch.Restart();
|
||||||
while (isAwaitingResponse)
|
while (isAwaitingResponse)
|
||||||
{
|
{
|
||||||
if (stopwatch.ElapsedMilliseconds > 1000)
|
if (stopwatch.ElapsedMilliseconds > port.ReadTimeout)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Response timed out.");
|
Console.WriteLine("Response timed out.");
|
||||||
break;
|
break;
|
||||||
@@ -333,12 +335,13 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
{
|
{
|
||||||
ushort value = alarmStatus ? (ushort)0 : (ushort)0xFF00;
|
ushort value = alarmStatus ? (ushort)0 : (ushort)0xFF00;
|
||||||
isAwaitingResponse = true;
|
isAwaitingResponse = true;
|
||||||
|
latestMessage = null;
|
||||||
Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.alarmStatus.address, value);
|
Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.alarmStatus.address, value);
|
||||||
|
|
||||||
stopwatch.Restart();
|
stopwatch.Restart();
|
||||||
while (isAwaitingResponse)
|
while (isAwaitingResponse)
|
||||||
{
|
{
|
||||||
if (stopwatch.ElapsedMilliseconds > 1000)
|
if (stopwatch.ElapsedMilliseconds > port.ReadTimeout)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Response timed out.");
|
Console.WriteLine("Response timed out.");
|
||||||
break;
|
break;
|
||||||
@@ -358,6 +361,7 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
{
|
{
|
||||||
ushort value = cleaningStatus ? (ushort)0 : (ushort)0xFF00;
|
ushort value = cleaningStatus ? (ushort)0 : (ushort)0xFF00;
|
||||||
isAwaitingResponse = true;
|
isAwaitingResponse = true;
|
||||||
|
latestMessage = null;
|
||||||
Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.cleaningMode.address, value);
|
Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.cleaningMode.address, value);
|
||||||
|
|
||||||
stopwatch.Restart();
|
stopwatch.Restart();
|
||||||
@@ -391,12 +395,14 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
// send speed value to device
|
// send speed value to device
|
||||||
// await for response
|
// await for response
|
||||||
isAwaitingResponse = true;
|
isAwaitingResponse = true;
|
||||||
|
latestMessage = null;
|
||||||
Modbus.WriteSingleAsync(FunctionCode.WriteRegister, modbusID, device.baudRate.address, newSpeed);
|
Modbus.WriteSingleAsync(FunctionCode.WriteRegister, modbusID, device.baudRate.address, newSpeed);
|
||||||
|
|
||||||
stopwatch.Restart();
|
stopwatch.Restart();
|
||||||
|
|
||||||
while (isAwaitingResponse)
|
while (isAwaitingResponse)
|
||||||
{
|
{
|
||||||
if (stopwatch.ElapsedMilliseconds > 1000)
|
if (stopwatch.ElapsedMilliseconds > port.ReadTimeout)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Response timed out.");
|
Console.WriteLine("Response timed out.");
|
||||||
break;
|
break;
|
||||||
@@ -510,13 +516,14 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
isAwaitingResponse = true;
|
isAwaitingResponse = true;
|
||||||
|
latestMessage = null;
|
||||||
Console.WriteLine("Outgoing firmware message: " + Modbus.ByteArrayToString(message.ToArray()));
|
Console.WriteLine("Outgoing firmware message: " + Modbus.ByteArrayToString(message.ToArray()));
|
||||||
port.Write(message.ToArray(), 0, message.Count);
|
port.Write(message.ToArray(), 0, message.Count);
|
||||||
stopwatch.Restart();
|
stopwatch.Restart();
|
||||||
|
|
||||||
while (isAwaitingResponse)
|
while (isAwaitingResponse)
|
||||||
{
|
{
|
||||||
if (stopwatch.ElapsedMilliseconds > 1000)
|
if (stopwatch.ElapsedMilliseconds > port.ReadTimeout)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Response timed out.");
|
Console.WriteLine("Response timed out.");
|
||||||
cntr++;
|
cntr++;
|
||||||
|
|||||||
3
Main.cs
3
Main.cs
@@ -129,11 +129,12 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
if (stopwatch.ElapsedMilliseconds > 1000)
|
if (stopwatch.ElapsedMilliseconds > 1000)
|
||||||
{
|
{
|
||||||
AddLog("Истекло время ожидания ответа от устройства. Повторный запрос...");
|
AddLog("Истекло время ожидания ответа от устройства. Повторный запрос...");
|
||||||
isAwaitingResponse = false;
|
stopwatch.Restart();
|
||||||
counter++;
|
counter++;
|
||||||
if (counter > 3)
|
if (counter > 3)
|
||||||
{
|
{
|
||||||
AddLog("Устройство не отвечает. Проверьте соединение с устройством.");
|
AddLog("Устройство не отвечает. Проверьте соединение с устройством.");
|
||||||
|
isAwaitingResponse = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
62
Modbus.cs
62
Modbus.cs
@@ -274,59 +274,65 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
static byte[] buffer = new byte[255];
|
static byte[] buffer = new byte[255];
|
||||||
static int offset = 0;
|
static int offset = 0;
|
||||||
static int count = 0;
|
static int count = 0;
|
||||||
|
static bool bytecountFound = false;
|
||||||
|
static int expectedBytes = 0;
|
||||||
static void PortDataReceived(object sender, EventArgs e)
|
static void PortDataReceived(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
|
|
||||||
//reset values on every event call;
|
//reset values on every event call;
|
||||||
buffer = new byte[255];
|
buffer = new byte[255];
|
||||||
offset = 0;
|
offset = 0;
|
||||||
|
bytecountFound = false;
|
||||||
|
expectedBytes = 0;
|
||||||
|
|
||||||
|
Console.WriteLine("Port data received");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
stopwatch.Restart();
|
stopwatch.Restart();
|
||||||
while (stopwatch.ElapsedMilliseconds < 50)
|
while (stopwatch.ElapsedMilliseconds < port.ReadTimeout)
|
||||||
{
|
{
|
||||||
|
if (bytecountFound && offset >= expectedBytes + 5)
|
||||||
|
break;
|
||||||
if (port.BytesToRead > 0)
|
if (port.BytesToRead > 0)
|
||||||
{
|
{
|
||||||
stopwatch.Restart();
|
stopwatch.Restart();
|
||||||
count = port.BytesToRead;
|
count = port.BytesToRead;
|
||||||
port.Read(buffer, offset, port.BytesToRead);
|
port.Read(buffer, offset, count);
|
||||||
offset += count;
|
offset += count;
|
||||||
|
if (!bytecountFound && offset >= 2)
|
||||||
|
{
|
||||||
|
expectedBytes = buffer[2];
|
||||||
|
Console.WriteLine("Found data byte count: " + expectedBytes);
|
||||||
|
bytecountFound = true;
|
||||||
|
}
|
||||||
|
if (bytecountFound && offset >= expectedBytes + 5) // reached end of message
|
||||||
|
{
|
||||||
|
Console.WriteLine("Reached end of message");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
stopwatch.Restart();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Console.WriteLine("Buffer: " + ByteArrayToString(buffer, false));
|
||||||
// assume that the message ended
|
// assume that the message ended
|
||||||
|
Console.WriteLine("Message reception ended");
|
||||||
|
byte[] message = new byte[expectedBytes + 5];
|
||||||
|
for (int i = 0; i < expectedBytes + 5; i++)
|
||||||
|
message[i] = buffer[i];
|
||||||
|
|
||||||
List<byte> message = new List<byte>();
|
Console.WriteLine("Incoming message: " + ByteArrayToString(message, false));
|
||||||
for (int i = 0; i < offset; i++)
|
|
||||||
{
|
|
||||||
message.Add(buffer[i]);
|
|
||||||
}
|
|
||||||
if (message.Count == 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
Console.WriteLine("Incoming message: " + ByteArrayToString(message.ToArray(), false));
|
if (!CheckResponse(message))
|
||||||
/*
|
|
||||||
if (!CheckResponse(message.ToArray()))
|
|
||||||
{
|
|
||||||
Console.WriteLine("Bad CRC or not a modbus message!");
|
Console.WriteLine("Bad CRC or not a modbus message!");
|
||||||
}
|
|
||||||
*/
|
|
||||||
if (message[1] <= 0x04) // read functions
|
if (message[1] <= 0x04) // read functions
|
||||||
{
|
ResponseReceived.Invoke(null, new ModbusResponseEventArgs(message, ModbusStatus.ReadSuccess));
|
||||||
//Console.WriteLine("It's a read message");
|
|
||||||
ResponseReceived.Invoke(null, new ModbusResponseEventArgs(message.ToArray(), ModbusStatus.ReadSuccess));
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (message[1] <= 0x10) // write functions
|
if (message[1] <= 0x10) // write functions
|
||||||
{
|
ResponseReceived.Invoke(null, new ModbusResponseEventArgs(message, ModbusStatus.WriteSuccess));
|
||||||
//Console.WriteLine("It's a write message");
|
|
||||||
ResponseReceived.Invoke(null, new ModbusResponseEventArgs(message.ToArray(), ModbusStatus.WriteSuccess));
|
|
||||||
}
|
|
||||||
else // error codes
|
else // error codes
|
||||||
{
|
ResponseReceived.Invoke(null, new ModbusResponseEventArgs(message, ModbusStatus.Error));
|
||||||
//Console.WriteLine("It's an error");
|
|
||||||
ResponseReceived.Invoke(null, new ModbusResponseEventArgs(message.ToArray(), ModbusStatus.Error));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception err)
|
catch (Exception err)
|
||||||
|
|||||||
Reference in New Issue
Block a user