Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4137a84fa3 | ||
|
|
96b77d4181 | ||
|
|
8587ad9d3d | ||
|
|
3d4a97b4e9 | ||
|
|
3a3fd60a00 | ||
|
|
5c2a9973f0 | ||
|
|
36304e1fd0 | ||
|
|
580e2246bb | ||
|
|
e32c701bd6 | ||
|
|
bcf0fb7a5c | ||
|
|
e00e96bea7 | ||
|
|
80bf64b7d5 | ||
|
|
e9f3a8d350 | ||
|
|
615394a479 | ||
|
|
795488d01e | ||
|
|
88b833899d |
79
Datasheet.Designer.cs
generated
79
Datasheet.Designer.cs
generated
@@ -55,9 +55,16 @@
|
||||
this.label4 = new System.Windows.Forms.Label();
|
||||
this.sensorPanel = new System.Windows.Forms.FlowLayoutPanel();
|
||||
this.label9 = new System.Windows.Forms.Label();
|
||||
this.BrowseFirmware = new System.Windows.Forms.Button();
|
||||
this.label11 = new System.Windows.Forms.Label();
|
||||
this.firmwarePathLabel = new System.Windows.Forms.Label();
|
||||
this.WriteFirmware = new System.Windows.Forms.Button();
|
||||
this.groupBox3 = new System.Windows.Forms.GroupBox();
|
||||
this.firmwareProgressBar = new System.Windows.Forms.ProgressBar();
|
||||
this.groupBox1.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.nudModbusID)).BeginInit();
|
||||
this.groupBox2.SuspendLayout();
|
||||
this.groupBox3.SuspendLayout();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// groupBox1
|
||||
@@ -312,7 +319,7 @@
|
||||
this.sensorPanel.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
|
||||
this.sensorPanel.Location = new System.Drawing.Point(13, 168);
|
||||
this.sensorPanel.Name = "sensorPanel";
|
||||
this.sensorPanel.Size = new System.Drawing.Size(512, 248);
|
||||
this.sensorPanel.Size = new System.Drawing.Size(512, 262);
|
||||
this.sensorPanel.TabIndex = 4;
|
||||
this.sensorPanel.WrapContents = false;
|
||||
//
|
||||
@@ -325,11 +332,71 @@
|
||||
this.label9.TabIndex = 5;
|
||||
this.label9.Text = "Датчики";
|
||||
//
|
||||
// BrowseFirmware
|
||||
//
|
||||
this.BrowseFirmware.Location = new System.Drawing.Point(6, 38);
|
||||
this.BrowseFirmware.Name = "BrowseFirmware";
|
||||
this.BrowseFirmware.Size = new System.Drawing.Size(75, 23);
|
||||
this.BrowseFirmware.TabIndex = 14;
|
||||
this.BrowseFirmware.Text = "Обзор";
|
||||
this.BrowseFirmware.UseVisualStyleBackColor = true;
|
||||
this.BrowseFirmware.Click += new System.EventHandler(this.BrowseFirmware_Click);
|
||||
//
|
||||
// label11
|
||||
//
|
||||
this.label11.AutoSize = true;
|
||||
this.label11.Location = new System.Drawing.Point(4, 22);
|
||||
this.label11.Name = "label11";
|
||||
this.label11.Size = new System.Drawing.Size(34, 13);
|
||||
this.label11.TabIndex = 16;
|
||||
this.label11.Text = "Путь:";
|
||||
//
|
||||
// firmwarePathLabel
|
||||
//
|
||||
this.firmwarePathLabel.AutoSize = true;
|
||||
this.firmwarePathLabel.Location = new System.Drawing.Point(44, 22);
|
||||
this.firmwarePathLabel.Name = "firmwarePathLabel";
|
||||
this.firmwarePathLabel.Size = new System.Drawing.Size(126, 13);
|
||||
this.firmwarePathLabel.TabIndex = 17;
|
||||
this.firmwarePathLabel.Text = "C:\\Path\\To\\Firmware.bin";
|
||||
//
|
||||
// WriteFirmware
|
||||
//
|
||||
this.WriteFirmware.Location = new System.Drawing.Point(87, 38);
|
||||
this.WriteFirmware.Name = "WriteFirmware";
|
||||
this.WriteFirmware.Size = new System.Drawing.Size(75, 23);
|
||||
this.WriteFirmware.TabIndex = 18;
|
||||
this.WriteFirmware.Text = "Прошить";
|
||||
this.WriteFirmware.UseVisualStyleBackColor = true;
|
||||
this.WriteFirmware.Click += new System.EventHandler(this.WriteFirmware_Click);
|
||||
//
|
||||
// groupBox3
|
||||
//
|
||||
this.groupBox3.Controls.Add(this.firmwareProgressBar);
|
||||
this.groupBox3.Controls.Add(this.BrowseFirmware);
|
||||
this.groupBox3.Controls.Add(this.WriteFirmware);
|
||||
this.groupBox3.Controls.Add(this.label11);
|
||||
this.groupBox3.Controls.Add(this.firmwarePathLabel);
|
||||
this.groupBox3.Location = new System.Drawing.Point(13, 436);
|
||||
this.groupBox3.Name = "groupBox3";
|
||||
this.groupBox3.Size = new System.Drawing.Size(512, 72);
|
||||
this.groupBox3.TabIndex = 19;
|
||||
this.groupBox3.TabStop = false;
|
||||
this.groupBox3.Text = "Прошивка";
|
||||
//
|
||||
// firmwareProgressBar
|
||||
//
|
||||
this.firmwareProgressBar.Location = new System.Drawing.Point(176, 38);
|
||||
this.firmwareProgressBar.Name = "firmwareProgressBar";
|
||||
this.firmwareProgressBar.Size = new System.Drawing.Size(330, 23);
|
||||
this.firmwareProgressBar.TabIndex = 19;
|
||||
//
|
||||
// Datasheet
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(537, 428);
|
||||
this.ClientSize = new System.Drawing.Size(537, 511);
|
||||
this.Controls.Add(this.groupBox3);
|
||||
this.Controls.Add(this.label9);
|
||||
this.Controls.Add(this.sensorPanel);
|
||||
this.Controls.Add(this.groupBox2);
|
||||
@@ -344,6 +411,8 @@
|
||||
((System.ComponentModel.ISupportInitialize)(this.nudModbusID)).EndInit();
|
||||
this.groupBox2.ResumeLayout(false);
|
||||
this.groupBox2.PerformLayout();
|
||||
this.groupBox3.ResumeLayout(false);
|
||||
this.groupBox3.PerformLayout();
|
||||
this.ResumeLayout(false);
|
||||
this.PerformLayout();
|
||||
|
||||
@@ -377,5 +446,11 @@
|
||||
private System.Windows.Forms.Button buttonSetSpeed;
|
||||
private System.Windows.Forms.Label label8;
|
||||
private System.Windows.Forms.ComboBox cBoxSpeed;
|
||||
private System.Windows.Forms.Button BrowseFirmware;
|
||||
private System.Windows.Forms.Label label11;
|
||||
private System.Windows.Forms.Label firmwarePathLabel;
|
||||
private System.Windows.Forms.Button WriteFirmware;
|
||||
private System.Windows.Forms.GroupBox groupBox3;
|
||||
private System.Windows.Forms.ProgressBar firmwareProgressBar;
|
||||
}
|
||||
}
|
||||
465
Datasheet.cs
465
Datasheet.cs
@@ -1,9 +1,13 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.IO.Ports;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
@@ -14,11 +18,12 @@ namespace Gidrolock_Modbus_Scanner
|
||||
byte modbusID;
|
||||
Device device;
|
||||
|
||||
ModbusResponseEventArgs latestMessage;
|
||||
public static ModbusResponseEventArgs latestMessage;
|
||||
|
||||
SerialPort port = Modbus.port;
|
||||
bool isPolling = false;
|
||||
bool isAwaitingResponse = false;
|
||||
static bool isAwaitingResponse = false;
|
||||
static bool responseReceived = false;
|
||||
|
||||
|
||||
bool isValveClosed = false;
|
||||
@@ -27,13 +32,24 @@ namespace Gidrolock_Modbus_Scanner
|
||||
|
||||
List<WiredSensor> wiredSensors = new List<WiredSensor>();
|
||||
List<WirelessSensor> wirelessSensors;
|
||||
public static string firmwarePath;
|
||||
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
Thread fileThread = new Thread((ThreadStart)delegate
|
||||
{
|
||||
OpenFileDialog ofd = new OpenFileDialog();
|
||||
ofd.InitialDirectory = Application.StartupPath;
|
||||
ofd.RestoreDirectory = true;
|
||||
|
||||
if (ofd.ShowDialog() == DialogResult.OK)
|
||||
firmwarePath = ofd.FileName;
|
||||
});
|
||||
|
||||
public Datasheet(byte modbusID, Device device) : base()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
firmwareProgressBar.Minimum = 0;
|
||||
firmwareProgressBar.Maximum = 100;
|
||||
nudModbusID.Minimum = 1;
|
||||
nudModbusID.Maximum = 246;
|
||||
nudModbusID.Value = modbusID;
|
||||
@@ -63,7 +79,7 @@ namespace Gidrolock_Modbus_Scanner
|
||||
labelBattery.Text = "Нет";
|
||||
else labelBattery.Text = "???%";
|
||||
|
||||
Modbus.ResponseReceived += (sndr, msg) => { isAwaitingResponse = false; latestMessage = msg; };
|
||||
Modbus.ResponseReceived += (sndr, msg) => { responseReceived = true; latestMessage = msg; isAwaitingResponse = false; };
|
||||
|
||||
for (int i = 0; i < device.wiredSensors; i++)
|
||||
{
|
||||
@@ -99,94 +115,104 @@ namespace Gidrolock_Modbus_Scanner
|
||||
|
||||
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
|
||||
try
|
||||
{
|
||||
bool res = await PollEntry(device.valveStatus); // for some reason main thread doesn't go
|
||||
await Task.Run(() =>
|
||||
{
|
||||
|
||||
bool res = PollEntry(device.firmware);
|
||||
Console.WriteLine("Polling for alarm status, poll success: " + res);
|
||||
if (res)
|
||||
{
|
||||
this.Invoke(new MethodInvoker(delegate { labelFirmware.Text = App.ByteArrayToUnicode(latestMessage.Data); }));
|
||||
}
|
||||
|
||||
if (device.hasBattery)
|
||||
{
|
||||
res = PollEntry(device.batteryCharge);
|
||||
if (res)
|
||||
{
|
||||
this.Invoke(new MethodInvoker(delegate { labelBattery.Text = latestMessage.Data.Last().ToString(); }));
|
||||
}
|
||||
}
|
||||
|
||||
res = PollEntry(device.valveStatus);
|
||||
Console.WriteLine("Polling for valve status, poll success: " + res);
|
||||
if (res)
|
||||
{
|
||||
if (latestMessage.Data.Last() > 0)
|
||||
{
|
||||
isValveClosed = true;
|
||||
labelValve.Text = "Закрыт";
|
||||
buttonValve.Text = "Открыть";
|
||||
this.Invoke(new MethodInvoker(delegate { labelValve.Text = "Закрыт"; }));
|
||||
this.Invoke(new MethodInvoker(delegate { buttonValve.Text = "Открыть"; }));
|
||||
}
|
||||
else
|
||||
{
|
||||
isValveClosed = false;
|
||||
labelValve.Text = "Открыт";
|
||||
buttonValve.Text = "Закрыть";
|
||||
this.Invoke(new MethodInvoker(delegate { labelValve.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);
|
||||
if (res)
|
||||
{
|
||||
Console.WriteLine("Alarm data: " + Modbus.ByteArrayToString(latestMessage.Data));
|
||||
Console.WriteLine("Alarm data.last: " + latestMessage.Data.Last().ToString());
|
||||
if (latestMessage.Data.Last() > 0)
|
||||
{
|
||||
alarmStatus = true;
|
||||
buttonAlarm.Text = "Выключить";
|
||||
labelAlarm.Text = "Протечка!";
|
||||
this.Invoke(new MethodInvoker(delegate { buttonAlarm.Text = "Выключить"; }));
|
||||
this.Invoke(new MethodInvoker(delegate { labelAlarm.Text = "Протечка!"; }));
|
||||
}
|
||||
else
|
||||
{
|
||||
alarmStatus = false;
|
||||
buttonAlarm.Text = "Авария";
|
||||
labelAlarm.Text = "нет";
|
||||
this.Invoke(new MethodInvoker(delegate { buttonAlarm.Text = "Авария"; }));
|
||||
this.Invoke(new MethodInvoker(delegate { labelAlarm.Text = "нет"; }));
|
||||
}
|
||||
}
|
||||
|
||||
if (device.hasCleaningMode)
|
||||
{
|
||||
res = await PollEntry(device.cleaningMode);
|
||||
res = PollEntry(device.cleaningMode);
|
||||
if (res)
|
||||
{
|
||||
if (latestMessage.Data.Last() > 0)
|
||||
{
|
||||
cleaningStatus = true;
|
||||
buttonAlarm.Text = "Выключить";
|
||||
labelAlarm.Text = "вкл";
|
||||
this.Invoke(new MethodInvoker(delegate { buttonCleaning.Text = "Выключить"; }));
|
||||
this.Invoke(new MethodInvoker(delegate { labelCleaning.Text = "вкл"; }));
|
||||
}
|
||||
else
|
||||
{
|
||||
cleaningStatus = false;
|
||||
buttonAlarm.Text = "Включить";
|
||||
labelAlarm.Text = "выкл";
|
||||
this.Invoke(new MethodInvoker(delegate { buttonCleaning.Text = "Включить"; }));
|
||||
this.Invoke(new MethodInvoker(delegate { labelCleaning.Text = "выкл"; }));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (device.hasBattery)
|
||||
if (device.wiredLineBreak != null || device.wiredLineBreak.Count > 0)
|
||||
{
|
||||
res = await PollEntry(device.batteryCharge);
|
||||
for (int i = 0; i < device.wiredLineBreak.Count; i++)
|
||||
{
|
||||
res = PollEntry(device.wiredLineBreak[i]);
|
||||
if (res)
|
||||
{
|
||||
labelBattery.Text = latestMessage.Data.Last().ToString();
|
||||
}
|
||||
}
|
||||
if (device.hasCleaningMode)
|
||||
{
|
||||
res = await PollEntry(device.cleaningMode);
|
||||
if (res)
|
||||
{
|
||||
if (latestMessage.Data.Last() > 0)
|
||||
{
|
||||
cleaningStatus = true;
|
||||
buttonAlarm.Text = "Выключить";
|
||||
labelAlarm.Text = "вкл";
|
||||
}
|
||||
else
|
||||
{
|
||||
cleaningStatus = false;
|
||||
buttonAlarm.Text = "Включить";
|
||||
labelAlarm.Text = "выкл";
|
||||
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 = await PollEntry(device.sensorAlarm);
|
||||
res = PollEntry(device.sensorAlarm);
|
||||
if (res)
|
||||
{
|
||||
BitArray bArray = new BitArray(latestMessage.Data);
|
||||
@@ -195,11 +221,12 @@ namespace Gidrolock_Modbus_Scanner
|
||||
for (int i = 0; i < sensorPanel.Controls.Count; i++)
|
||||
{
|
||||
Sensor snsr = sensorPanel.Controls[i] as Sensor;
|
||||
snsr.labelLeak.Text = bools[i] ? "Протечка!" : "нет";
|
||||
snsr.Invoke(new MethodInvoker(delegate { snsr.labelLeak.Text = bools[i] ? "Протечка!" : "нет"; }));
|
||||
}
|
||||
}
|
||||
|
||||
res = await PollEntry(device.radioStatus);
|
||||
Console.WriteLine("Polling for radio status");
|
||||
res = PollEntry(device.radioStatus);
|
||||
if (res)
|
||||
{
|
||||
List<byte> values = new List<byte>(latestMessage.Data.Length / 2);
|
||||
@@ -226,96 +253,117 @@ namespace Gidrolock_Modbus_Scanner
|
||||
txt = "потеря";
|
||||
break;
|
||||
}
|
||||
snsr.labelStatus.Text = txt;
|
||||
snsr.Invoke(new MethodInvoker(delegate { snsr.labelStatus.Text = txt; }));
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
catch (Exception err) { MessageBox.Show(err.Message); }
|
||||
}
|
||||
|
||||
async Task<bool> PollEntry(Entry entry)
|
||||
bool PollEntry(Entry entry)
|
||||
{
|
||||
latestMessage = null;
|
||||
bool res = false;
|
||||
Modbus.ReadRegAsync(modbusID, (FunctionCode)entry.registerType, entry.address, entry.length);
|
||||
isAwaitingResponse = true;
|
||||
await Task.Delay(2000).ContinueWith(_ =>
|
||||
isPolling = true;
|
||||
|
||||
Modbus.ReadRegAsync(modbusID, (FunctionCode)entry.registerType, entry.address, entry.length);
|
||||
stopwatch.Restart();
|
||||
|
||||
while (isAwaitingResponse && latestMessage == null)
|
||||
{
|
||||
if (isAwaitingResponse)
|
||||
if (stopwatch.ElapsedMilliseconds > 5000)
|
||||
{
|
||||
MessageBox.Show("Превышено время ожидания ответа от устройства.");
|
||||
isAwaitingResponse = false;
|
||||
Console.WriteLine("Response timed out.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
while (isAwaitingResponse) { continue; }
|
||||
|
||||
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
|
||||
res = true;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// Задать новый Slave ID для устройства
|
||||
private async void buttonSetID_Click(object sender, EventArgs e)
|
||||
{
|
||||
byte newID = (byte)nudModbusID.Value; // should prevent assigning wrong ID if UpDown is fiddled with in the middle of request
|
||||
isAwaitingResponse = true;
|
||||
latestMessage = null;
|
||||
Modbus.WriteSingleAsync(FunctionCode.WriteRegister, modbusID, 128, newID);
|
||||
await Task.Delay(2000).ContinueWith(_ =>
|
||||
|
||||
stopwatch.Restart();
|
||||
while (isAwaitingResponse)
|
||||
{
|
||||
if (isAwaitingResponse)
|
||||
if (stopwatch.ElapsedMilliseconds > 10000)
|
||||
{
|
||||
MessageBox.Show("Превышено время ожидания ответа от устройства.");
|
||||
isAwaitingResponse = false;
|
||||
Console.WriteLine("Response timed out.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
while (isAwaitingResponse) { continue; }
|
||||
|
||||
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
|
||||
modbusID = newID;
|
||||
}
|
||||
|
||||
// Кран
|
||||
private async void buttonValve_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
ushort value = isValveClosed ? (ushort)0 : (ushort)0xFF00;
|
||||
Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.valveStatus.address, value);
|
||||
|
||||
await Task.Delay(2000).ContinueWith(_ =>
|
||||
{
|
||||
if (isAwaitingResponse)
|
||||
{
|
||||
MessageBox.Show("Превышено время ожидания ответа от устройства.");
|
||||
isAwaitingResponse = false;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
while (isAwaitingResponse) { continue; }
|
||||
|
||||
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
|
||||
if (SetEntry(device.valveStatus, value))
|
||||
{
|
||||
isValveClosed = !isValveClosed;
|
||||
labelValve.Text = isValveClosed ? "Закрыт" : "Открыт";
|
||||
buttonValve.Text = isValveClosed ? "Открыть" : "Закрыть";
|
||||
labelValve.Invoke(new MethodInvoker(delegate { labelValve.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;
|
||||
Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.alarmStatus.address, value);
|
||||
|
||||
await Task.Delay(2000).ContinueWith(_ =>
|
||||
{
|
||||
if (isAwaitingResponse)
|
||||
{
|
||||
MessageBox.Show("Превышено время ожидания ответа от устройства.");
|
||||
isAwaitingResponse = false;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
while (isAwaitingResponse) { continue; }
|
||||
|
||||
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
|
||||
if (SetEntry(device.alarmStatus, value))
|
||||
{
|
||||
alarmStatus = !alarmStatus;
|
||||
labelAlarm.Text = alarmStatus ? "Протечка!" : "Нет";
|
||||
@@ -323,23 +371,11 @@ 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;
|
||||
Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.cleaningMode.address, value);
|
||||
|
||||
await Task.Delay(2000).ContinueWith(_ =>
|
||||
{
|
||||
if (isAwaitingResponse)
|
||||
{
|
||||
MessageBox.Show("Превышено время ожидания ответа от устройства.");
|
||||
isAwaitingResponse = false;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
while (isAwaitingResponse) { continue; }
|
||||
|
||||
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
|
||||
if (SetEntry(device.cleaningMode, value))
|
||||
{
|
||||
cleaningStatus = !cleaningStatus;
|
||||
labelCleaning.Text = cleaningStatus ? "вкл" : "выкл";
|
||||
@@ -347,37 +383,213 @@ namespace Gidrolock_Modbus_Scanner
|
||||
}
|
||||
}
|
||||
|
||||
private async void buttonSetSpeed_Click(object sender, EventArgs e)
|
||||
// Задать скорость передачи данных для устройства
|
||||
private void buttonSetSpeed_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
string str = cBoxSpeed.Items[cBoxSpeed.SelectedIndex].ToString();
|
||||
str = str.Substring(0, str.Length - 2); //clip off two zeroes at the end
|
||||
ushort newSpeed = (ushort)Int16.Parse(str);
|
||||
//Console.WriteLine("Baudrate: " + newSpeed);
|
||||
|
||||
// send speed value to device
|
||||
// await for response
|
||||
Modbus.WriteSingleAsync(FunctionCode.WriteRegister, modbusID, device.baudRate.address, newSpeed);
|
||||
await Task.Delay(2000).ContinueWith(_ =>
|
||||
Console.WriteLine("new speed: " + newSpeed);
|
||||
if (SetEntry(device.baudRate, newSpeed))
|
||||
{
|
||||
if (isAwaitingResponse)
|
||||
{
|
||||
MessageBox.Show("Превышено время ожидания ответа от устройства.");
|
||||
isAwaitingResponse = false;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
while (isAwaitingResponse) { continue; }
|
||||
if (latestMessage.Status != ModbusStatus.Error)
|
||||
{
|
||||
port.Close();
|
||||
port.BaudRate = newSpeed;
|
||||
port.Open();
|
||||
//port.Close();
|
||||
port.BaudRate = newSpeed * 100;
|
||||
//port.Open();
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { MessageBox.Show(ex.Message); }
|
||||
}
|
||||
|
||||
// Выбор файла прошивки
|
||||
private void BrowseFirmware_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
fileThread.SetApartmentState(ApartmentState.STA);
|
||||
fileThread.Start();
|
||||
while (!fileThread.IsAlive) { Thread.Sleep(1); }
|
||||
Thread.Sleep(1);
|
||||
fileThread.Join();
|
||||
}
|
||||
catch (Exception err) { MessageBox.Show(err.Message); }
|
||||
|
||||
firmwarePathLabel.Invoke(new MethodInvoker(delegate { firmwarePathLabel.Text = firmwarePath; }));
|
||||
}
|
||||
|
||||
// Запись прошивки
|
||||
private async void WriteFirmware_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (firmwarePath is null || firmwarePath.Length == 0)
|
||||
{
|
||||
MessageBox.Show("Выберите файл прошивки.");
|
||||
return;
|
||||
}
|
||||
int cntr = 0;
|
||||
|
||||
FileStream fileStream = File.OpenRead(firmwarePath);
|
||||
long bytesLeft = fileStream.Length;
|
||||
long bytesTotal = fileStream.Length;
|
||||
int count = 64;
|
||||
byte[] buffer = new byte[count];
|
||||
byte[] bdma;
|
||||
short _flashAddr = 0;
|
||||
byte[] flashAddr = new byte[2];
|
||||
byte[] CRC;
|
||||
List<byte> message;
|
||||
bool firstMessageSent = false;
|
||||
long bytesWritten = 0;
|
||||
await Task.Run(() =>
|
||||
{
|
||||
while (bytesLeft > 0)
|
||||
{
|
||||
if (firstMessageSent && port.BaudRate != 9600) // after first message the device is sent into recovery mode which only supports 9600 bps
|
||||
{
|
||||
port.Close();
|
||||
port.BaudRate = 9600;
|
||||
port.Open();
|
||||
}
|
||||
|
||||
count = bytesLeft > 64 ? 64 : (int)bytesLeft;
|
||||
buffer = new byte[count];
|
||||
fileStream.Read(buffer, 0, count);
|
||||
|
||||
|
||||
bdma = new byte[2];
|
||||
bdma[0] = (byte)((bytesLeft & 0xFF_00) >> 8);
|
||||
bdma[1] = (byte)(bytesLeft & 0x00_FF);
|
||||
|
||||
flashAddr[0] = (byte)((_flashAddr & 0xFF_00) >> 8);
|
||||
flashAddr[1] = (byte)(_flashAddr & 0x00_FF);
|
||||
|
||||
message = new List<byte>();
|
||||
message.Add(modbusID); // device ID
|
||||
message.Add(0x10); // function code
|
||||
message.Add(0xFF); // register address
|
||||
message.Add(0xFF); // register address
|
||||
message.Add(0x00); // regCnt (?)
|
||||
message.Add(0x21); // regCnt (?)
|
||||
message.Add(0x42); // data bytecount
|
||||
|
||||
message.Add(flashAddr[0]);
|
||||
message.Add(flashAddr[1]);
|
||||
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < buffer.Length; i++)
|
||||
{
|
||||
message.Add(buffer[i]);
|
||||
}
|
||||
message.Add(0x00);
|
||||
message.Add(0x00);
|
||||
CRC = new byte[2];
|
||||
Modbus.GetCRC(message.ToArray(), ref CRC);
|
||||
message[message.Count - 2] = CRC[0];
|
||||
message[message.Count - 1] = CRC[1];
|
||||
|
||||
responseReceived = false;
|
||||
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (cntr > 3)
|
||||
{
|
||||
Console.WriteLine("Response timed out 4 times in a row, aborting. Check connection.");
|
||||
return;
|
||||
}
|
||||
isAwaitingResponse = true;
|
||||
latestMessage = null;
|
||||
Console.WriteLine("Outgoing firmware message: " + Modbus.ByteArrayToString(message.ToArray()));
|
||||
port.Write(message.ToArray(), 0, message.Count);
|
||||
stopwatch.Restart();
|
||||
|
||||
while (isAwaitingResponse)
|
||||
{
|
||||
if (stopwatch.ElapsedMilliseconds > port.ReadTimeout)
|
||||
{
|
||||
Console.WriteLine("Response timed out.");
|
||||
cntr++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (responseReceived)
|
||||
{
|
||||
if (latestMessage.Status == ModbusStatus.Error)
|
||||
Console.WriteLine("Response received: Error!");
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Response received: all good;");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
cntr = 0;
|
||||
bytesLeft -= count;
|
||||
bytesWritten += count;
|
||||
firmwareProgressBar.Invoke((MethodInvoker)delegate { firmwareProgressBar.Increment((int)(bytesWritten / bytesTotal) * 100); });
|
||||
|
||||
_flashAddr += (short)count;
|
||||
if (port.BaudRate != 9600)
|
||||
firstMessageSent = true;
|
||||
|
||||
if (bytesLeft <= 0)
|
||||
Console.WriteLine("Reached the end of firmware file.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "Firmware writing error");
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* Final Message */
|
||||
message = new List<byte>();
|
||||
message.Add(modbusID); // device ID
|
||||
message.Add(0x10); // function code
|
||||
message.Add(0xFF); // register address
|
||||
message.Add(0xFF); // register address
|
||||
message.Add(0x00); // regCnt (?)
|
||||
message.Add(0x21); // regCnt (?)
|
||||
message.Add(0x00); // data bytecount
|
||||
message.Add(0x00); // CRC
|
||||
message.Add(0x00); // CRC
|
||||
CRC = new byte[2];
|
||||
Modbus.GetCRC(message.ToArray(), ref CRC);
|
||||
message[message.Count - 2] = CRC[0];
|
||||
message[message.Count - 1] = CRC[1];
|
||||
while (true)
|
||||
{
|
||||
isAwaitingResponse = true;
|
||||
Console.WriteLine("Outgoing firmware message: " + Modbus.ByteArrayToString(message.ToArray()));
|
||||
port.Write(message.ToArray(), 0, message.Count);
|
||||
stopwatch.Restart();
|
||||
|
||||
while (isAwaitingResponse)
|
||||
{
|
||||
if (stopwatch.ElapsedMilliseconds > 1000)
|
||||
{
|
||||
Console.WriteLine("Response timed out.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (responseReceived)
|
||||
{
|
||||
if (latestMessage.Status == ModbusStatus.Error)
|
||||
Console.WriteLine("Response received: Error!");
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Response received: all good;");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
public class Sensor : FlowLayoutPanel
|
||||
{
|
||||
@@ -387,7 +599,6 @@ namespace Gidrolock_Modbus_Scanner
|
||||
}
|
||||
public class WiredSensor : Sensor
|
||||
{
|
||||
|
||||
public Label labelBreakFluff = new Label() { Width = 45, Height = 24 };
|
||||
public Label labelBreak = new Label() { Width = 55, Height = 24 }; // обрыв линии для WSP+
|
||||
|
||||
|
||||
86
Main.cs
86
Main.cs
@@ -3,6 +3,8 @@ using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using System.IO.Ports;
|
||||
using System.Reflection;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Gidrolock_Modbus_Scanner
|
||||
{
|
||||
@@ -20,6 +22,7 @@ namespace Gidrolock_Modbus_Scanner
|
||||
DateTime dateTime;
|
||||
|
||||
Datasheet datasheet;
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
#region Initialization
|
||||
public App()
|
||||
{
|
||||
@@ -45,7 +48,8 @@ namespace Gidrolock_Modbus_Scanner
|
||||
upDownModbusID.Minimum = 1;
|
||||
upDownModbusID.Maximum = 247;
|
||||
|
||||
models.Add("Standard", "STW485");
|
||||
models.Add("Standard Wi-Fi", "STW485");
|
||||
models.Add("Standard Radio", "STR485");
|
||||
models.Add("Premium Plus", "PRPLS1");
|
||||
models.Add("Inteli", "INTELI");
|
||||
models.Add("Premium", "BUP485");
|
||||
@@ -55,12 +59,6 @@ namespace Gidrolock_Modbus_Scanner
|
||||
System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location);
|
||||
string version = fvi.FileVersion;
|
||||
Console.WriteLine("Version: " + version);
|
||||
|
||||
Modbus.ResponseReceived += (sndr, msg) =>
|
||||
{
|
||||
latestMessage = msg;
|
||||
isAwaitingResponse = false;
|
||||
};
|
||||
}
|
||||
|
||||
void App_FormClosed(object sender, FormClosedEventArgs e)
|
||||
@@ -76,8 +74,10 @@ namespace Gidrolock_Modbus_Scanner
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
private async void ButtonConnect_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
if (cBoxPorts.SelectedItem.ToString() == "COM1")
|
||||
{
|
||||
DialogResult res = MessageBox.Show("Выбран серийный порт COM1, который обычно является портом PS/2 или RS-232, не подключенным к Modbus устройству. Продолжить?", "Внимание", MessageBoxButtons.OKCancel);
|
||||
@@ -122,18 +122,29 @@ namespace Gidrolock_Modbus_Scanner
|
||||
latestMessage = null;
|
||||
isAwaitingResponse = true;
|
||||
var send = Modbus.ReadRegAsync((byte)upDownModbusID.Value, FunctionCode.ReadInput, 200, 6);
|
||||
await Task.Delay(2000).ContinueWith(_ =>
|
||||
stopwatch.Restart();
|
||||
int counter = 0;
|
||||
while (isAwaitingResponse)
|
||||
{
|
||||
if (isAwaitingResponse)
|
||||
if (stopwatch.ElapsedMilliseconds > 1000)
|
||||
{
|
||||
AddLog("Истекло время ожидания ответа от устройства. Повторный запрос...");
|
||||
stopwatch.Restart();
|
||||
counter++;
|
||||
if (counter > 3)
|
||||
{
|
||||
AddLog("Устройство не отвечает. Проверьте соединение с устройством.");
|
||||
isAwaitingResponse = false;
|
||||
MessageBox.Show("Истекло время ожидания ответа от устройства.");
|
||||
}
|
||||
return;
|
||||
});
|
||||
|
||||
while (isAwaitingResponse) { continue; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (latestMessage is null)
|
||||
{
|
||||
Console.WriteLine("Latest message is null;");
|
||||
return;
|
||||
}
|
||||
if (latestMessage.Status == ModbusStatus.Error)
|
||||
return;
|
||||
|
||||
@@ -188,6 +199,7 @@ namespace Gidrolock_Modbus_Scanner
|
||||
|
||||
void OnResponseReceived(object sender, ModbusResponseEventArgs e)
|
||||
{
|
||||
latestMessage = e;
|
||||
isAwaitingResponse = false;
|
||||
AddLog("Получен ответ: " + Modbus.ByteArrayToString(e.Message));
|
||||
switch (e.Status)
|
||||
@@ -287,7 +299,7 @@ namespace Gidrolock_Modbus_Scanner
|
||||
Device d = new Device();
|
||||
switch (dt)
|
||||
{
|
||||
case DeviceType.Standard:
|
||||
case DeviceType.StandardWifi:
|
||||
d.name = "Standard Wi-Fi RS485";
|
||||
d.id = 30;
|
||||
d.modelName = "STW485";
|
||||
@@ -305,7 +317,37 @@ namespace Gidrolock_Modbus_Scanner
|
||||
d.wiredSensors = 2;
|
||||
d.hasScenarioSensor = true;
|
||||
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);
|
||||
|
||||
break;
|
||||
case DeviceType.StandardRadio:
|
||||
d.name = "Standard Radio RS485";
|
||||
d.id = 30;
|
||||
d.modelName = "STR485";
|
||||
d.firmware = new Entry(RegisterType.Input, 250, 6);
|
||||
d.baudRate = new Entry(RegisterType.Holding, 110);
|
||||
|
||||
d.valveStatus = new Entry(RegisterType.Coil, 1202);
|
||||
d.alarmStatus = new Entry(RegisterType.Coil, 1201);
|
||||
|
||||
d.hasCleaningMode = true;
|
||||
d.cleaningMode = new Entry(RegisterType.Coil, 3);
|
||||
|
||||
d.hasBattery = false;
|
||||
|
||||
d.wiredSensors = 2;
|
||||
d.hasScenarioSensor = false;
|
||||
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);
|
||||
|
||||
break;
|
||||
@@ -329,11 +371,21 @@ namespace Gidrolock_Modbus_Scanner
|
||||
d.cleaningMode = new Entry(RegisterType.Coil, 3);
|
||||
|
||||
d.hasBattery = true;
|
||||
d.batteryCharge = new Entry(RegisterType.Input, 1207);
|
||||
d.batteryCharge = new Entry(RegisterType.Input, 100);
|
||||
|
||||
d.wiredSensors = 7;
|
||||
d.hasScenarioSensor = true;
|
||||
d.sensorAlarm = new Entry(RegisterType.Discrete, 1343, 29);
|
||||
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);
|
||||
break;
|
||||
case DeviceType.Premium:
|
||||
@@ -355,4 +407,4 @@ public enum FunctionCode { ReadCoil = 1, ReadDiscrete = 2, ReadHolding = 3, Read
|
||||
//public enum SelectedPath { File, Folder };
|
||||
|
||||
|
||||
public enum DeviceType { Standard, PremiumPlus, Inteli, Premium };
|
||||
public enum DeviceType { StandardWifi, StandardRadio, PremiumPlus, Inteli, Premium };
|
||||
|
||||
111
Modbus.cs
111
Modbus.cs
@@ -5,7 +5,9 @@ using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using System.Collections.Generic;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Gidrolock_Modbus_Scanner
|
||||
{
|
||||
@@ -13,7 +15,6 @@ namespace Gidrolock_Modbus_Scanner
|
||||
{
|
||||
public static SerialPort port = new SerialPort();
|
||||
|
||||
|
||||
public static event EventHandler<ModbusResponseEventArgs> ResponseReceived = delegate { };
|
||||
|
||||
public static void Init()
|
||||
@@ -210,7 +211,7 @@ namespace Gidrolock_Modbus_Scanner
|
||||
|
||||
|
||||
#region CRC Computation
|
||||
static void GetCRC(byte[] message, ref byte[] CRC)
|
||||
public static void GetCRC(byte[] message, ref byte[] CRC)
|
||||
{
|
||||
//Function expects a modbus message of any length as well as a 2 byte CRC array in which to
|
||||
//return the CRC values:
|
||||
@@ -235,45 +236,115 @@ namespace Gidrolock_Modbus_Scanner
|
||||
CRC[1] = CRCHigh = (byte)((CRCFull >> 8) & 0xFF);
|
||||
CRC[0] = CRCLow = (byte)(CRCFull & 0xFF);
|
||||
}
|
||||
#endregion
|
||||
|
||||
static void PortDataReceived(object sender, EventArgs e)
|
||||
public static bool CheckResponse(byte[] response) // Проверка пакета на контрольную сумму
|
||||
{
|
||||
//Perform a basic CRC check:
|
||||
byte[] CRC = new byte[2];
|
||||
try
|
||||
{
|
||||
Thread.Sleep(50);
|
||||
byte[] message = new byte[port.BytesToRead];
|
||||
//Console.WriteLine("Bytes to read:" + port.BytesToRead);
|
||||
port.Read(message, 0, port.BytesToRead);
|
||||
Console.WriteLine("Incoming message: " + ByteArrayToString(message, false));
|
||||
if (message[1] <= 0x04) // read functions
|
||||
CRC[0] = response[response.Length - 2];
|
||||
CRC[1] = response[response.Length - 1];
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
//Console.WriteLine("It's a read message");
|
||||
ResponseReceived.Invoke(null, new ModbusResponseEventArgs(message, ModbusStatus.ReadSuccess));
|
||||
return false;
|
||||
}
|
||||
GetCRC(response, ref CRC);
|
||||
|
||||
if (CRC[0] == response[response.Length - 2] && CRC[1] == response[response.Length - 1])
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
static Stopwatch stopwatch = new Stopwatch();
|
||||
|
||||
static byte[] buffer = new byte[255];
|
||||
static int offset = 0;
|
||||
static int count = 0;
|
||||
static ModbusStatus responseStatus;
|
||||
static bool bytecountFound = false;
|
||||
static int expectedBytes = 0;
|
||||
static void PortDataReceived(object sender, EventArgs e)
|
||||
{
|
||||
//reset values on every event call;
|
||||
buffer = new byte[255];
|
||||
offset = 0;
|
||||
bytecountFound = false;
|
||||
expectedBytes = 0;
|
||||
|
||||
try
|
||||
{
|
||||
stopwatch.Restart();
|
||||
while (stopwatch.ElapsedMilliseconds < port.ReadTimeout)
|
||||
{
|
||||
if (bytecountFound && offset >= expectedBytes)
|
||||
break;
|
||||
if (port.BytesToRead > 0)
|
||||
{
|
||||
stopwatch.Restart();
|
||||
count = port.BytesToRead;
|
||||
port.Read(buffer, offset, count);
|
||||
offset += count;
|
||||
if (offset >= 1)
|
||||
{
|
||||
if (buffer[1] < 0x05)
|
||||
responseStatus = ModbusStatus.ReadSuccess;
|
||||
else if (buffer[1] < 0x10)
|
||||
{
|
||||
responseStatus = ModbusStatus.WriteSuccess;
|
||||
expectedBytes = 8;
|
||||
bytecountFound = true;
|
||||
}
|
||||
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");
|
||||
ResponseReceived.Invoke(null, new ModbusResponseEventArgs(message, ModbusStatus.WriteSuccess));
|
||||
expectedBytes = buffer[2] + 5;
|
||||
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");
|
||||
ResponseReceived.Invoke(null, new ModbusResponseEventArgs(message, ModbusStatus.Error));
|
||||
Console.WriteLine("Reached end of message");
|
||||
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)
|
||||
{
|
||||
MessageBox.Show(err.Message, "Modbus message reception error");
|
||||
}
|
||||
|
||||
port.DiscardInBuffer();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class ModbusResponseEventArgs : EventArgs
|
||||
{
|
||||
public byte[] Message { get; set; }
|
||||
|
||||
@@ -30,6 +30,6 @@ using System.Runtime.InteropServices;
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
[assembly: AssemblyVersion("0.9.0.0")]
|
||||
[assembly: AssemblyFileVersion("0.9.0.0")]
|
||||
[assembly: AssemblyVersion("0.9.5.0")]
|
||||
[assembly: AssemblyFileVersion("0.9.5.0")]
|
||||
[assembly: NeutralResourcesLanguage("en")]
|
||||
|
||||
@@ -6,5 +6,6 @@
|
||||
|
||||
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:
|
||||
- Standard Wi-Fi RS485
|
||||
- Premium Plus
|
||||
- Standard Radio RS485
|
||||
- Premium Plus Wi-Fi
|
||||
|
||||
|
||||
BIN
Screenshot_1.png
Normal file
BIN
Screenshot_1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 78 KiB |
83
Update.cs
Normal file
83
Update.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using Gidrolock_Modbus_Scanner;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Ports;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Gidrolock_Modbus_Configurator
|
||||
{
|
||||
public class Update
|
||||
{
|
||||
bool isAwaitingResponse = false;
|
||||
ModbusResponseEventArgs latestMessage;
|
||||
SerialPort port = Modbus.port;
|
||||
|
||||
public void UpdateFirmware(Stream fileStream)
|
||||
{
|
||||
Modbus.ResponseReceived += (sndr, msg) => { isAwaitingResponse = false; latestMessage = msg; };
|
||||
int offset = 0;
|
||||
byte[] buffer;
|
||||
byte[] preCRC;
|
||||
byte[] message;
|
||||
byte[] CRC = new byte[2];
|
||||
|
||||
long bytesLeft = fileStream.Length;
|
||||
|
||||
while (bytesLeft > 0)
|
||||
{
|
||||
int count = bytesLeft > 64 ? 64 : (int)bytesLeft;
|
||||
buffer = new byte[count];
|
||||
|
||||
fileStream.Read(buffer, offset, count);
|
||||
|
||||
preCRC = new byte[7 + buffer.Length];
|
||||
|
||||
preCRC[0] = 0x1E; // Modbus ID
|
||||
preCRC[1] = 0x10; // Function code
|
||||
preCRC[2] = 0xFF; // Address 01
|
||||
preCRC[3] = 0xFF; // Address 02
|
||||
preCRC[4] = 0x00; // Cnt01
|
||||
preCRC[5] = 0x21; // Cnt02
|
||||
preCRC[6] = 0x42; // byte count
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
preCRC[i+7] = buffer[i];
|
||||
}
|
||||
Modbus.GetCRC(preCRC, ref CRC);
|
||||
message = new byte[preCRC.Length + 2];
|
||||
|
||||
for (int i = 0; i < preCRC.Length; i++)
|
||||
{
|
||||
message[i] = preCRC[i];
|
||||
}
|
||||
message[message.Length - 2] = CRC[0];
|
||||
message[message.Length - 1] = CRC[1];
|
||||
|
||||
isAwaitingResponse = true;
|
||||
port.Write(message, 0, message.Length);
|
||||
|
||||
var delay = Task.Delay(2000).ContinueWith(_ =>
|
||||
{
|
||||
if (isAwaitingResponse)
|
||||
{
|
||||
MessageBox.Show("Превышено время ожидания ответа от устройства.");
|
||||
isAwaitingResponse = false;
|
||||
}
|
||||
});
|
||||
while (isAwaitingResponse) { continue; }
|
||||
|
||||
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
|
||||
{
|
||||
bytesLeft -= count;
|
||||
offset += count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user