Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8d3773368e | ||
|
|
4137a84fa3 | ||
|
|
96b77d4181 | ||
|
|
8587ad9d3d | ||
|
|
3d4a97b4e9 | ||
|
|
3a3fd60a00 | ||
|
|
5c2a9973f0 |
260
Datasheet.cs
260
Datasheet.cs
@@ -18,7 +18,7 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
byte modbusID;
|
byte modbusID;
|
||||||
Device device;
|
Device device;
|
||||||
|
|
||||||
ModbusResponseEventArgs latestMessage;
|
public static ModbusResponseEventArgs latestMessage;
|
||||||
|
|
||||||
SerialPort port = Modbus.port;
|
SerialPort port = Modbus.port;
|
||||||
bool isPolling = false;
|
bool isPolling = false;
|
||||||
@@ -34,9 +34,7 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
List<WirelessSensor> wirelessSensors;
|
List<WirelessSensor> wirelessSensors;
|
||||||
public static string firmwarePath;
|
public static string firmwarePath;
|
||||||
|
|
||||||
static int timeout = 1000;
|
|
||||||
Stopwatch stopwatch = new Stopwatch();
|
Stopwatch stopwatch = new Stopwatch();
|
||||||
|
|
||||||
Thread fileThread = new Thread((ThreadStart)delegate
|
Thread fileThread = new Thread((ThreadStart)delegate
|
||||||
{
|
{
|
||||||
OpenFileDialog ofd = new OpenFileDialog();
|
OpenFileDialog ofd = new OpenFileDialog();
|
||||||
@@ -85,10 +83,23 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
|
|
||||||
for (int i = 0; i < device.wiredSensors; i++)
|
for (int i = 0; i < device.wiredSensors; i++)
|
||||||
{
|
{
|
||||||
WiredSensor ws = new WiredSensor(i) { Width = 495, Height = 24 };
|
WiredSensor ws = new WiredSensor(i,device) { Width = 495, Height = 24 };
|
||||||
sensorPanel.Controls.Add(ws);
|
sensorPanel.Controls.Add(ws);
|
||||||
ws.Visible = true;
|
ws.Visible = true;
|
||||||
|
|
||||||
|
if (device.modelName == "PRPLS1")
|
||||||
|
{
|
||||||
|
ws.wspPlusEntry = device.wspPlusMode[i];
|
||||||
|
ws.wspPlusCheckbox.CheckedChanged += (s, e) =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ushort value = ws.wspPlusCheckbox.Checked ? (ushort)0xFF00 : (ushort)0x0000;
|
||||||
|
SetEntry(ws.wspPlusEntry, value);
|
||||||
|
}
|
||||||
|
catch (Exception err) { MessageBox.Show(err.Message, "WSP+ toggle error"); }
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (device.hasScenarioSensor)
|
if (device.hasScenarioSensor)
|
||||||
{
|
{
|
||||||
@@ -117,44 +128,49 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
|
|
||||||
private async void buttonPoll_Click(object sender, EventArgs e)
|
private async void buttonPoll_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
|
if (isPolling)
|
||||||
|
return;
|
||||||
// hardcoded for now, probably easier to keep it like this in the future
|
// hardcoded for now, probably easier to keep it like this in the future
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
bool res = await PollEntry(device.firmware);
|
await Task.Run(() =>
|
||||||
|
{
|
||||||
|
|
||||||
|
bool res = PollEntry(device.firmware);
|
||||||
Console.WriteLine("Polling for alarm status, poll success: " + res);
|
Console.WriteLine("Polling for alarm status, poll success: " + res);
|
||||||
if (res)
|
if (res)
|
||||||
{
|
{
|
||||||
labelFirmware.Text = App.ByteArrayToUnicode(latestMessage.Data);
|
this.Invoke(new MethodInvoker(delegate { labelFirmware.Text = App.ByteArrayToUnicode(latestMessage.Data); }));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (device.hasBattery)
|
if (device.hasBattery)
|
||||||
{
|
{
|
||||||
res = await PollEntry(device.batteryCharge);
|
res = PollEntry(device.batteryCharge);
|
||||||
if (res)
|
if (res)
|
||||||
{
|
{
|
||||||
labelBattery.Text = latestMessage.Data.Last().ToString();
|
this.Invoke(new MethodInvoker(delegate { labelBattery.Text = latestMessage.Data.Last().ToString(); }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
res = await PollEntry(device.valveStatus);
|
res = PollEntry(device.valveStatus);
|
||||||
Console.WriteLine("Polling for valve status, poll success: " + res);
|
Console.WriteLine("Polling for valve status, poll success: " + res);
|
||||||
if (res)
|
if (res)
|
||||||
{
|
{
|
||||||
if (latestMessage.Data.Last() > 0)
|
if (latestMessage.Data.Last() > 0)
|
||||||
{
|
{
|
||||||
isValveClosed = true;
|
isValveClosed = true;
|
||||||
labelValve.Text = "Закрыт";
|
this.Invoke(new MethodInvoker(delegate { labelValve.Text = "Закрыт"; }));
|
||||||
buttonValve.Text = "Открыть";
|
this.Invoke(new MethodInvoker(delegate { buttonValve.Text = "Открыть"; }));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
isValveClosed = false;
|
isValveClosed = false;
|
||||||
labelValve.Text = "Открыт";
|
this.Invoke(new MethodInvoker(delegate { labelValve.Text = "Открыт"; }));
|
||||||
buttonValve.Text = "Закрыть";
|
this.Invoke(new MethodInvoker(delegate { buttonValve.Text = "Закрыть"; }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
res = await PollEntry(device.alarmStatus);
|
res = PollEntry(device.alarmStatus);
|
||||||
Console.WriteLine("Polling for alarm status, poll success: " + res);
|
Console.WriteLine("Polling for alarm status, poll success: " + res);
|
||||||
if (res)
|
if (res)
|
||||||
{
|
{
|
||||||
@@ -163,38 +179,67 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
if (latestMessage.Data.Last() > 0)
|
if (latestMessage.Data.Last() > 0)
|
||||||
{
|
{
|
||||||
alarmStatus = true;
|
alarmStatus = true;
|
||||||
buttonAlarm.Text = "Выключить";
|
this.Invoke(new MethodInvoker(delegate { buttonAlarm.Text = "Выключить"; }));
|
||||||
labelAlarm.Text = "Протечка!";
|
this.Invoke(new MethodInvoker(delegate { labelAlarm.Text = "Протечка!"; }));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
alarmStatus = false;
|
alarmStatus = false;
|
||||||
buttonAlarm.Text = "Авария";
|
this.Invoke(new MethodInvoker(delegate { buttonAlarm.Text = "Авария"; }));
|
||||||
labelAlarm.Text = "нет";
|
this.Invoke(new MethodInvoker(delegate { labelAlarm.Text = "нет"; }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (device.hasCleaningMode)
|
if (device.hasCleaningMode)
|
||||||
{
|
{
|
||||||
res = await PollEntry(device.cleaningMode);
|
res = PollEntry(device.cleaningMode);
|
||||||
if (res)
|
if (res)
|
||||||
{
|
{
|
||||||
if (latestMessage.Data.Last() > 0)
|
if (latestMessage.Data.Last() > 0)
|
||||||
{
|
{
|
||||||
cleaningStatus = true;
|
cleaningStatus = true;
|
||||||
buttonCleaning.Text = "Выключить";
|
this.Invoke(new MethodInvoker(delegate { buttonCleaning.Text = "Выключить"; }));
|
||||||
labelCleaning.Text = "вкл";
|
this.Invoke(new MethodInvoker(delegate { labelCleaning.Text = "вкл"; }));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cleaningStatus = false;
|
cleaningStatus = false;
|
||||||
buttonCleaning.Text = "Включить";
|
this.Invoke(new MethodInvoker(delegate { buttonCleaning.Text = "Включить"; }));
|
||||||
labelCleaning.Text = "выкл";
|
this.Invoke(new MethodInvoker(delegate { labelCleaning.Text = "выкл"; }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
res = await PollEntry(device.sensorAlarm);
|
if (device.wspPlusMode != null || device.wspPlusMode.Count > 0)
|
||||||
|
{
|
||||||
|
for(int i = 0; i < device.wspPlusMode.Count; i++)
|
||||||
|
{
|
||||||
|
res = PollEntry(device.wspPlusMode[i]);
|
||||||
|
if (res)
|
||||||
|
{
|
||||||
|
bool value = latestMessage.Data[0] > 0x00 ? true : false;
|
||||||
|
WiredSensor snsr = sensorPanel.Controls[i] as WiredSensor;
|
||||||
|
snsr.Invoke(new MethodInvoker(delegate { snsr.wspPlusCheckbox.Checked = value; }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (device.wiredLineBreak != null || device.wiredLineBreak.Count > 0)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < device.wiredLineBreak.Count; i++)
|
||||||
|
{
|
||||||
|
res = PollEntry(device.wiredLineBreak[i]);
|
||||||
|
if (res)
|
||||||
|
{
|
||||||
|
bool value = latestMessage.Data[0] > 0x00 ? true : false;
|
||||||
|
WiredSensor snsr = sensorPanel.Controls[i] as WiredSensor;
|
||||||
|
snsr.Invoke(new MethodInvoker(delegate { snsr.labelBreak.Text = value ? "Обрыв!" : "ОК"; }));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
res = PollEntry(device.sensorAlarm);
|
||||||
if (res)
|
if (res)
|
||||||
{
|
{
|
||||||
BitArray bArray = new BitArray(latestMessage.Data);
|
BitArray bArray = new BitArray(latestMessage.Data);
|
||||||
@@ -203,12 +248,12 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
for (int i = 0; i < sensorPanel.Controls.Count; i++)
|
for (int i = 0; i < sensorPanel.Controls.Count; i++)
|
||||||
{
|
{
|
||||||
Sensor snsr = sensorPanel.Controls[i] as Sensor;
|
Sensor snsr = sensorPanel.Controls[i] as Sensor;
|
||||||
snsr.labelLeak.Text = bools[i] ? "Протечка!" : "нет";
|
snsr.Invoke(new MethodInvoker(delegate { snsr.labelLeak.Text = bools[i] ? "Протечка!" : "нет"; }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine("Polling for radio status");
|
Console.WriteLine("Polling for radio status");
|
||||||
res = await PollEntry(device.radioStatus);
|
res = PollEntry(device.radioStatus);
|
||||||
if (res)
|
if (res)
|
||||||
{
|
{
|
||||||
List<byte> values = new List<byte>(latestMessage.Data.Length / 2);
|
List<byte> values = new List<byte>(latestMessage.Data.Length / 2);
|
||||||
@@ -235,26 +280,27 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
txt = "потеря";
|
txt = "потеря";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
snsr.labelStatus.Text = txt;
|
snsr.Invoke(new MethodInvoker(delegate { snsr.labelStatus.Text = txt; }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
catch (Exception err) { MessageBox.Show(err.Message); }
|
catch (Exception err) { MessageBox.Show(err.Message); }
|
||||||
}
|
}
|
||||||
|
|
||||||
//Опрос всех записей
|
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);
|
isPolling = true;
|
||||||
|
|
||||||
|
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;
|
||||||
@@ -265,6 +311,34 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
res = true;
|
res = true;
|
||||||
|
|
||||||
Console.WriteLine("Poll attempt finished");
|
Console.WriteLine("Poll attempt finished");
|
||||||
|
isPolling = false;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
bool SetEntry(Entry entry, ushort value)
|
||||||
|
{
|
||||||
|
latestMessage = null;
|
||||||
|
bool res = false;
|
||||||
|
isAwaitingResponse = true;
|
||||||
|
isPolling = true;
|
||||||
|
FunctionCode fc = FunctionCode.WriteCoil;
|
||||||
|
if (entry.registerType == RegisterType.Holding)
|
||||||
|
fc = FunctionCode.WriteRegister;
|
||||||
|
|
||||||
|
Modbus.WriteSingleAsync(fc, modbusID, entry.address, value);
|
||||||
|
stopwatch.Restart();
|
||||||
|
|
||||||
|
while (isAwaitingResponse && latestMessage == null)
|
||||||
|
{
|
||||||
|
if (stopwatch.ElapsedMilliseconds > port.ReadTimeout)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Response timed out.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (latestMessage != null && latestMessage.Status == ModbusStatus.WriteSuccess)
|
||||||
|
res = true;
|
||||||
|
isPolling = false;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,12 +347,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 > 10000)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Response timed out.");
|
Console.WriteLine("Response timed out.");
|
||||||
break;
|
break;
|
||||||
@@ -291,47 +366,31 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
|
|
||||||
// Кран
|
// Кран
|
||||||
private async void buttonValve_Click(object sender, EventArgs e)
|
private async void buttonValve_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await Task.Run(() =>
|
||||||
{
|
{
|
||||||
ushort value = isValveClosed ? (ushort)0 : (ushort)0xFF00;
|
ushort value = isValveClosed ? (ushort)0 : (ushort)0xFF00;
|
||||||
isAwaitingResponse = true;
|
if (SetEntry(device.valveStatus, value))
|
||||||
Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.valveStatus.address, value);
|
|
||||||
|
|
||||||
stopwatch.Restart();
|
|
||||||
while (isAwaitingResponse)
|
|
||||||
{
|
|
||||||
if (stopwatch.ElapsedMilliseconds > 1000)
|
|
||||||
{
|
|
||||||
Console.WriteLine("Response timed out.");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
|
|
||||||
{
|
{
|
||||||
isValveClosed = !isValveClosed;
|
isValveClosed = !isValveClosed;
|
||||||
labelValve.Text = isValveClosed ? "Закрыт" : "Открыт";
|
labelValve.Invoke(new MethodInvoker(delegate { labelValve.Text = isValveClosed ? "Закрыт" : "Открыт"; }));
|
||||||
buttonValve.Text = isValveClosed ? "Открыть" : "Закрыть";
|
buttonValve.Invoke(new MethodInvoker(delegate { buttonValve.Text = isValveClosed ? "Открыть" : "Закрыть"; }));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception err)
|
||||||
|
{
|
||||||
|
MessageBox.Show(err.Message, "Valve Set Error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Авария
|
// Авария
|
||||||
private async void buttonAlarm_Click(object sender, EventArgs e)
|
private void buttonAlarm_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
ushort value = alarmStatus ? (ushort)0 : (ushort)0xFF00;
|
ushort value = alarmStatus ? (ushort)0 : (ushort)0xFF00;
|
||||||
isAwaitingResponse = true;
|
if (SetEntry(device.alarmStatus, value))
|
||||||
Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.alarmStatus.address, value);
|
|
||||||
|
|
||||||
stopwatch.Restart();
|
|
||||||
while (isAwaitingResponse)
|
|
||||||
{
|
|
||||||
if (stopwatch.ElapsedMilliseconds > 1000)
|
|
||||||
{
|
|
||||||
Console.WriteLine("Response timed out.");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
|
|
||||||
{
|
{
|
||||||
alarmStatus = !alarmStatus;
|
alarmStatus = !alarmStatus;
|
||||||
labelAlarm.Text = alarmStatus ? "Протечка!" : "Нет";
|
labelAlarm.Text = alarmStatus ? "Протечка!" : "Нет";
|
||||||
@@ -340,23 +399,10 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Режим уборки
|
// Режим уборки
|
||||||
private async void buttonCleaning_Click(object sender, EventArgs e)
|
private void buttonCleaning_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
ushort value = cleaningStatus ? (ushort)0 : (ushort)0xFF00;
|
ushort value = cleaningStatus ? (ushort)0 : (ushort)0xFF00;
|
||||||
isAwaitingResponse = true;
|
if (SetEntry(device.cleaningMode, value))
|
||||||
Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.cleaningMode.address, value);
|
|
||||||
|
|
||||||
stopwatch.Restart();
|
|
||||||
while (isAwaitingResponse)
|
|
||||||
{
|
|
||||||
if (stopwatch.ElapsedMilliseconds > 1000)
|
|
||||||
{
|
|
||||||
Console.WriteLine("Response timed out.");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
|
|
||||||
{
|
{
|
||||||
cleaningStatus = !cleaningStatus;
|
cleaningStatus = !cleaningStatus;
|
||||||
labelCleaning.Text = cleaningStatus ? "вкл" : "выкл";
|
labelCleaning.Text = cleaningStatus ? "вкл" : "выкл";
|
||||||
@@ -365,35 +411,19 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Задать скорость передачи данных для устройства
|
// Задать скорость передачи данных для устройства
|
||||||
private async void buttonSetSpeed_Click(object sender, EventArgs e)
|
private void buttonSetSpeed_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string str = cBoxSpeed.Items[cBoxSpeed.SelectedIndex].ToString();
|
string str = cBoxSpeed.Items[cBoxSpeed.SelectedIndex].ToString();
|
||||||
str = str.Substring(0, str.Length - 2); //clip off two zeroes at the end
|
str = str.Substring(0, str.Length - 2); //clip off two zeroes at the end
|
||||||
ushort newSpeed = (ushort)Int16.Parse(str);
|
ushort newSpeed = (ushort)Int16.Parse(str);
|
||||||
//Console.WriteLine("Baudrate: " + newSpeed);
|
Console.WriteLine("new speed: " + newSpeed);
|
||||||
|
if (SetEntry(device.baudRate, newSpeed))
|
||||||
// send speed value to device
|
|
||||||
// await for response
|
|
||||||
isAwaitingResponse = true;
|
|
||||||
Modbus.WriteSingleAsync(FunctionCode.WriteRegister, modbusID, device.baudRate.address, newSpeed);
|
|
||||||
|
|
||||||
stopwatch.Restart();
|
|
||||||
while (isAwaitingResponse)
|
|
||||||
{
|
{
|
||||||
if (stopwatch.ElapsedMilliseconds > 1000)
|
//port.Close();
|
||||||
{
|
port.BaudRate = newSpeed * 100;
|
||||||
Console.WriteLine("Response timed out.");
|
//port.Open();
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
|
|
||||||
{
|
|
||||||
port.Close();
|
|
||||||
port.BaudRate = newSpeed;
|
|
||||||
port.Open();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex) { MessageBox.Show(ex.Message); }
|
catch (Exception ex) { MessageBox.Show(ex.Message); }
|
||||||
@@ -496,13 +526,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++;
|
||||||
@@ -587,6 +618,8 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region Sensor Classes
|
||||||
public class Sensor : FlowLayoutPanel
|
public class Sensor : FlowLayoutPanel
|
||||||
{
|
{
|
||||||
public Label labelName = new Label() { Width = 60, Height = 24 };
|
public Label labelName = new Label() { Width = 60, Height = 24 };
|
||||||
@@ -598,9 +631,10 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
public Label labelBreakFluff = new Label() { Width = 45, Height = 24 };
|
public Label labelBreakFluff = new Label() { Width = 45, Height = 24 };
|
||||||
public Label labelBreak = new Label() { Width = 55, Height = 24 }; // обрыв линии для WSP+
|
public Label labelBreak = new Label() { Width = 55, Height = 24 }; // обрыв линии для WSP+
|
||||||
|
|
||||||
//public Label labelWSPPlusFluff = new Label() { Width = 45, Height = 24 };
|
public Label labelWSPPlusFluff;
|
||||||
//public CheckBox wspPlusCheckbox = new CheckBox() { Width = 20, Height = 14 };
|
public CheckBox wspPlusCheckbox;
|
||||||
public WiredSensor(int count)
|
public Entry wspPlusEntry; // for WSP+ control
|
||||||
|
public WiredSensor(int count, Device device)
|
||||||
{
|
{
|
||||||
this.Margin = Padding.Empty;
|
this.Margin = Padding.Empty;
|
||||||
this.Padding = new Padding(0, 5, 0, 0);
|
this.Padding = new Padding(0, 5, 0, 0);
|
||||||
@@ -616,9 +650,6 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
this.Controls.Add(labelLeakFluff);
|
this.Controls.Add(labelLeakFluff);
|
||||||
this.Controls.Add(labelLeak);
|
this.Controls.Add(labelLeak);
|
||||||
|
|
||||||
//this.Controls.Add(labelWSPPlusFluff);
|
|
||||||
//this.Controls.Add(wspPlusCheckbox);
|
|
||||||
|
|
||||||
labelName.Text = "WSP " + (count + 1);
|
labelName.Text = "WSP " + (count + 1);
|
||||||
|
|
||||||
labelLeakFluff.Text = "Протечка:";
|
labelLeakFluff.Text = "Протечка:";
|
||||||
@@ -626,9 +657,15 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
|
|
||||||
labelBreakFluff.Text = "Обрыв:";
|
labelBreakFluff.Text = "Обрыв:";
|
||||||
labelBreak.Text = "неизвестно";
|
labelBreak.Text = "неизвестно";
|
||||||
|
if (device.wspPlusMode != null || device.wspPlusMode.Count > 0)
|
||||||
|
{
|
||||||
|
labelWSPPlusFluff = new Label() { Width = 45, Height = 24, Text = "WSP+:" };
|
||||||
|
wspPlusCheckbox = new CheckBox() { Width = 20, Height = 14, Margin = Padding.Empty };
|
||||||
|
|
||||||
|
this.Controls.Add(labelWSPPlusFluff);
|
||||||
|
this.Controls.Add(wspPlusCheckbox);
|
||||||
|
}
|
||||||
|
|
||||||
//labelWSPPlusFluff.Text = "WSP+:";
|
|
||||||
//wspPlusCheckbox.Margin = Padding.Empty;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -683,3 +720,4 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
35
Main.cs
35
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -316,7 +317,11 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
d.wiredSensors = 2;
|
d.wiredSensors = 2;
|
||||||
d.hasScenarioSensor = true;
|
d.hasScenarioSensor = true;
|
||||||
d.sensorAlarm = new Entry(RegisterType.Discrete, 1343, 24);
|
d.sensorAlarm = new Entry(RegisterType.Discrete, 1343, 24);
|
||||||
|
d.wiredLineBreak = new List<Entry>()
|
||||||
|
{
|
||||||
|
new Entry(RegisterType.Discrete, 1205),
|
||||||
|
new Entry(RegisterType.Discrete, 1207)
|
||||||
|
};
|
||||||
d.radioStatus = new Entry(RegisterType.Input, 1215, 21);
|
d.radioStatus = new Entry(RegisterType.Input, 1215, 21);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -338,7 +343,11 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
d.wiredSensors = 2;
|
d.wiredSensors = 2;
|
||||||
d.hasScenarioSensor = false;
|
d.hasScenarioSensor = false;
|
||||||
d.sensorAlarm = new Entry(RegisterType.Discrete, 1343, 23);
|
d.sensorAlarm = new Entry(RegisterType.Discrete, 1343, 23);
|
||||||
|
d.wiredLineBreak = new List<Entry>()
|
||||||
|
{
|
||||||
|
new Entry(RegisterType.Discrete, 1205),
|
||||||
|
new Entry(RegisterType.Discrete, 1207)
|
||||||
|
};
|
||||||
d.radioStatus = new Entry(RegisterType.Input, 1215, 21);
|
d.radioStatus = new Entry(RegisterType.Input, 1215, 21);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -367,6 +376,26 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
d.wiredSensors = 7;
|
d.wiredSensors = 7;
|
||||||
d.hasScenarioSensor = true;
|
d.hasScenarioSensor = true;
|
||||||
d.sensorAlarm = new Entry(RegisterType.Discrete, 1343, 29);
|
d.sensorAlarm = new Entry(RegisterType.Discrete, 1343, 29);
|
||||||
|
d.wspPlusMode = new List<Entry>()
|
||||||
|
{
|
||||||
|
new Entry(RegisterType.Coil, 1041),
|
||||||
|
new Entry(RegisterType.Coil, 1042),
|
||||||
|
new Entry(RegisterType.Coil, 1043),
|
||||||
|
new Entry(RegisterType.Coil, 1044),
|
||||||
|
new Entry(RegisterType.Coil, 1045),
|
||||||
|
new Entry(RegisterType.Coil, 1046),
|
||||||
|
new Entry(RegisterType.Coil, 1047),
|
||||||
|
};
|
||||||
|
d.wiredLineBreak = new List<Entry>()
|
||||||
|
{
|
||||||
|
new Entry(RegisterType.Discrete, 1025),
|
||||||
|
new Entry(RegisterType.Discrete, 1026),
|
||||||
|
new Entry(RegisterType.Discrete, 1027),
|
||||||
|
new Entry(RegisterType.Discrete, 1028),
|
||||||
|
new Entry(RegisterType.Discrete, 1029),
|
||||||
|
new Entry(RegisterType.Discrete, 1030),
|
||||||
|
new Entry(RegisterType.Discrete, 1031),
|
||||||
|
};
|
||||||
d.radioStatus = new Entry(RegisterType.Input, 1215, 21);
|
d.radioStatus = new Entry(RegisterType.Input, 1215, 21);
|
||||||
break;
|
break;
|
||||||
case DeviceType.Premium:
|
case DeviceType.Premium:
|
||||||
|
|||||||
103
Modbus.cs
103
Modbus.cs
@@ -257,84 +257,84 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
/*
|
|
||||||
static byte[] buffer = new byte[255];
|
|
||||||
static int offset = 0;
|
|
||||||
static Thread timer = new Thread(new ThreadStart(() =>
|
|
||||||
{
|
|
||||||
Thread.Sleep(50);
|
|
||||||
offset = 0;
|
|
||||||
buffer = new byte[255];
|
|
||||||
port.DiscardInBuffer();
|
|
||||||
}));
|
|
||||||
*/
|
|
||||||
|
|
||||||
static Stopwatch stopwatch = new Stopwatch();
|
static Stopwatch stopwatch = new Stopwatch();
|
||||||
|
|
||||||
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 ModbusStatus responseStatus;
|
||||||
|
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;
|
||||||
|
buffer = new byte[255];
|
||||||
|
offset = 0;
|
||||||
|
bytecountFound = false;
|
||||||
|
expectedBytes = 0;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
stopwatch.Restart();
|
stopwatch.Restart();
|
||||||
while (stopwatch.ElapsedMilliseconds < 20)
|
while (stopwatch.ElapsedMilliseconds < port.ReadTimeout)
|
||||||
{
|
{
|
||||||
|
if (bytecountFound && offset >= expectedBytes)
|
||||||
|
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 (offset >= 1)
|
||||||
}
|
|
||||||
// assume that the message ended
|
|
||||||
offset = 0;
|
|
||||||
List <byte> message = new List <byte>();
|
|
||||||
int endOfMessage = buffer.Length - 1;
|
|
||||||
for (int i = buffer.Length-1; i >= 0; i--)
|
|
||||||
{
|
{
|
||||||
if (buffer[i] != 0x00)
|
if (buffer[1] < 0x05)
|
||||||
|
responseStatus = ModbusStatus.ReadSuccess;
|
||||||
|
else if (buffer[1] < 0x10)
|
||||||
{
|
{
|
||||||
endOfMessage = i;
|
responseStatus = ModbusStatus.WriteSuccess;
|
||||||
break;
|
expectedBytes = 8;
|
||||||
}
|
bytecountFound = true;
|
||||||
}
|
|
||||||
for (int i = 0; i < endOfMessage + 1; i++)
|
|
||||||
{
|
|
||||||
message.Add(buffer[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (message.Count == 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
Console.WriteLine("Incoming message: " + ByteArrayToString(message.ToArray(), false));
|
|
||||||
/*
|
|
||||||
if (!CheckResponse(message.ToArray()))
|
|
||||||
{
|
|
||||||
Console.WriteLine("Bad CRC or not a modbus message!");
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
if (message[1] <= 0x04) // read functions
|
|
||||||
{
|
|
||||||
//Console.WriteLine("It's a read message");
|
|
||||||
ResponseReceived.Invoke(null, new ModbusResponseEventArgs(message.ToArray(), ModbusStatus.ReadSuccess));
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (message[1] <= 0x10) // write functions
|
responseStatus = ModbusStatus.Error;
|
||||||
|
expectedBytes = 5;
|
||||||
|
bytecountFound = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (responseStatus == ModbusStatus.ReadSuccess && !bytecountFound && offset >= 2)
|
||||||
{
|
{
|
||||||
//Console.WriteLine("It's a write message");
|
expectedBytes = buffer[2] + 5;
|
||||||
ResponseReceived.Invoke(null, new ModbusResponseEventArgs(message.ToArray(), ModbusStatus.WriteSuccess));
|
Console.WriteLine("Found data byte count: " + expectedBytes);
|
||||||
|
bytecountFound = true;
|
||||||
}
|
}
|
||||||
else // error codes
|
if (bytecountFound && offset >= expectedBytes) // reached end of message
|
||||||
{
|
{
|
||||||
//Console.WriteLine("It's an error");
|
Console.WriteLine("Reached end of message");
|
||||||
ResponseReceived.Invoke(null, new ModbusResponseEventArgs(message.ToArray(), ModbusStatus.Error));
|
break;
|
||||||
|
}
|
||||||
|
stopwatch.Restart();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Console.WriteLine("Buffer: " + ByteArrayToString(buffer, false));
|
||||||
|
// assume that the message ended
|
||||||
|
Console.WriteLine("Message reception ended");
|
||||||
|
byte[] message = new byte[expectedBytes];
|
||||||
|
for (int i = 0; i < expectedBytes; i++)
|
||||||
|
message[i] = buffer[i];
|
||||||
|
|
||||||
|
Console.WriteLine("Incoming message: " + ByteArrayToString(message, false));
|
||||||
|
|
||||||
|
if (!CheckResponse(message))
|
||||||
|
Console.WriteLine("Bad CRC or not a modbus message!");
|
||||||
|
|
||||||
|
ResponseReceived.Invoke(null, new ModbusResponseEventArgs(message, responseStatus));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception err)
|
catch (Exception err)
|
||||||
{
|
{
|
||||||
@@ -345,7 +345,6 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ModbusResponseEventArgs : EventArgs
|
public class ModbusResponseEventArgs : EventArgs
|
||||||
{
|
{
|
||||||
public byte[] Message { get; set; }
|
public byte[] Message { get; set; }
|
||||||
@@ -365,7 +364,7 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
}
|
}
|
||||||
//Console.WriteLine("Read data: " + Modbus.ByteArrayToString(Data, false));
|
//Console.WriteLine("Read data: " + Modbus.ByteArrayToString(Data, false));
|
||||||
}
|
}
|
||||||
else Data = new byte[1] {0x0F};
|
else Data = new byte[1] { 0x0F };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
1
Model.cs
1
Model.cs
@@ -25,6 +25,7 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
public bool hasScenarioSensor;
|
public bool hasScenarioSensor;
|
||||||
public Entry sensorAlarm;
|
public Entry sensorAlarm;
|
||||||
|
|
||||||
|
public List<Entry> wspPlusMode; // Premium Plus only for now
|
||||||
public List<Entry> wiredLineBreak;
|
public List<Entry> wiredLineBreak;
|
||||||
|
|
||||||
public Entry radioStatus;
|
public Entry radioStatus;
|
||||||
|
|||||||
@@ -30,6 +30,6 @@ using System.Runtime.InteropServices;
|
|||||||
// Build Number
|
// Build Number
|
||||||
// Revision
|
// Revision
|
||||||
//
|
//
|
||||||
[assembly: AssemblyVersion("0.9.3.0")]
|
[assembly: AssemblyVersion("1.1.0.0")]
|
||||||
[assembly: AssemblyFileVersion("0.9.3.0")]
|
[assembly: AssemblyFileVersion("1.1.0.0")]
|
||||||
[assembly: NeutralResourcesLanguage("en")]
|
[assembly: NeutralResourcesLanguage("en")]
|
||||||
|
|||||||
Reference in New Issue
Block a user