6 Commits

Author SHA1 Message Date
nikzori
36304e1fd0 fixed port buffer offset and count not being set to 0 2025-03-19 15:27:37 +03:00
nikzori
580e2246bb premium plus registers updated 2025-03-11 11:39:29 +03:00
nikzori
e32c701bd6 reworked serial port data processing, added firmware writing 2025-03-07 16:49:07 +03:00
nikzori
bcf0fb7a5c switched to Stopwatch for timers for more reliability 2025-03-06 10:18:06 +03:00
nikzori
e00e96bea7 switched to Stopwatch for timers for more reliability 2025-03-06 10:16:30 +03:00
nikzori
80bf64b7d5 firmware test 2025-02-28 18:00:17 +03:00
8 changed files with 542 additions and 85 deletions

79
Datasheet.Designer.cs generated
View File

@@ -55,9 +55,16 @@
this.label4 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label();
this.sensorPanel = new System.Windows.Forms.FlowLayoutPanel(); this.sensorPanel = new System.Windows.Forms.FlowLayoutPanel();
this.label9 = new System.Windows.Forms.Label(); 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(); this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.nudModbusID)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.nudModbusID)).BeginInit();
this.groupBox2.SuspendLayout(); this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
// groupBox1 // groupBox1
@@ -312,7 +319,7 @@
this.sensorPanel.FlowDirection = System.Windows.Forms.FlowDirection.TopDown; this.sensorPanel.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
this.sensorPanel.Location = new System.Drawing.Point(13, 168); this.sensorPanel.Location = new System.Drawing.Point(13, 168);
this.sensorPanel.Name = "sensorPanel"; 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.TabIndex = 4;
this.sensorPanel.WrapContents = false; this.sensorPanel.WrapContents = false;
// //
@@ -325,11 +332,71 @@
this.label9.TabIndex = 5; this.label9.TabIndex = 5;
this.label9.Text = "Датчики"; 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 // Datasheet
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 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.label9);
this.Controls.Add(this.sensorPanel); this.Controls.Add(this.sensorPanel);
this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox2);
@@ -344,6 +411,8 @@
((System.ComponentModel.ISupportInitialize)(this.nudModbusID)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.nudModbusID)).EndInit();
this.groupBox2.ResumeLayout(false); this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout(); this.groupBox2.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.groupBox3.PerformLayout();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
@@ -377,5 +446,11 @@
private System.Windows.Forms.Button buttonSetSpeed; private System.Windows.Forms.Button buttonSetSpeed;
private System.Windows.Forms.Label label8; private System.Windows.Forms.Label label8;
private System.Windows.Forms.ComboBox cBoxSpeed; 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;
} }
} }

View File

@@ -1,9 +1,13 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.IO;
using System.IO.Ports; using System.IO.Ports;
using System.Linq; using System.Linq;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
@@ -18,22 +22,36 @@ namespace Gidrolock_Modbus_Scanner
SerialPort port = Modbus.port; SerialPort port = Modbus.port;
bool isPolling = false; bool isPolling = false;
bool isAwaitingResponse = false; static bool isAwaitingResponse = false;
static bool responseReceived = false;
bool isValveClosed = false; bool isValveClosed = false;
bool alarmStatus = false; bool alarmStatus = false;
bool cleaningStatus = false; bool cleaningStatus = false;
List<WiredSensor> wiredSensors = new List<WiredSensor>(); List<WiredSensor> wiredSensors = new List<WiredSensor>();
List<WirelessSensor> wirelessSensors; List<WirelessSensor> wirelessSensors;
public static string firmwarePath;
static int timeout = 1000;
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() public Datasheet(byte modbusID, Device device) : base()
{ {
InitializeComponent(); InitializeComponent();
firmwareProgressBar.Minimum = 0;
firmwareProgressBar.Maximum = 100;
nudModbusID.Minimum = 1; nudModbusID.Minimum = 1;
nudModbusID.Maximum = 246; nudModbusID.Maximum = 246;
nudModbusID.Value = modbusID; nudModbusID.Value = modbusID;
@@ -63,14 +81,14 @@ namespace Gidrolock_Modbus_Scanner
labelBattery.Text = "Нет"; labelBattery.Text = "Нет";
else 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++) for (int i = 0; i < device.wiredSensors; i++)
{ {
WiredSensor ws = new WiredSensor(i) { Width = 495, Height = 24 }; WiredSensor ws = new WiredSensor(i) { Width = 495, Height = 24 };
sensorPanel.Controls.Add(ws); sensorPanel.Controls.Add(ws);
ws.Visible = true; ws.Visible = true;
} }
if (device.hasScenarioSensor) if (device.hasScenarioSensor)
{ {
@@ -118,7 +136,7 @@ namespace Gidrolock_Modbus_Scanner
} }
} }
res = await PollEntry(device.valveStatus); res = await 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)
{ {
@@ -189,6 +207,7 @@ namespace Gidrolock_Modbus_Scanner
} }
} }
Console.WriteLine("Polling for radio status");
res = await PollEntry(device.radioStatus); res = await PollEntry(device.radioStatus);
if (res) if (res)
{ {
@@ -224,20 +243,23 @@ 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)
{ {
bool res = false; bool res = false;
Modbus.ReadRegAsync(modbusID, (FunctionCode)entry.registerType, entry.address, entry.length);
isAwaitingResponse = true; isAwaitingResponse = true;
Task.Delay(2000).ContinueWith(_ => Modbus.ReadRegAsync(modbusID, (FunctionCode)entry.registerType, entry.address, entry.length);
stopwatch.Restart();
while (isAwaitingResponse)
{ {
if (isAwaitingResponse) if (stopwatch.ElapsedMilliseconds > 1000)
{ {
MessageBox.Show("Превышено время ожидания ответа от устройства."); Console.WriteLine("Response timed out.");
isAwaitingResponse = false; break;
} }
}); }
while (isAwaitingResponse) { continue; }
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error) if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
res = true; res = true;
@@ -246,40 +268,43 @@ namespace Gidrolock_Modbus_Scanner
return res; return res;
} }
// Задать новый Slave ID для устройства
private async void buttonSetID_Click(object sender, EventArgs e) 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 byte newID = (byte)nudModbusID.Value; // should prevent assigning wrong ID if UpDown is fiddled with in the middle of request
isAwaitingResponse = true;
Modbus.WriteSingleAsync(FunctionCode.WriteRegister, modbusID, 128, newID); Modbus.WriteSingleAsync(FunctionCode.WriteRegister, modbusID, 128, newID);
await Task.Delay(2000).ContinueWith(_ =>
stopwatch.Restart();
while (isAwaitingResponse)
{ {
if (isAwaitingResponse) if (stopwatch.ElapsedMilliseconds > 1000)
{ {
MessageBox.Show("Превышено время ожидания ответа от устройства."); Console.WriteLine("Response timed out.");
isAwaitingResponse = false; break;
} }
return false; }
});
while (isAwaitingResponse) { continue; }
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error) if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
modbusID = newID; modbusID = newID;
} }
// Кран
private async void buttonValve_Click(object sender, EventArgs e) private async void buttonValve_Click(object sender, EventArgs e)
{ {
ushort value = isValveClosed ? (ushort)0: (ushort)0xFF00; ushort value = isValveClosed ? (ushort)0 : (ushort)0xFF00;
isAwaitingResponse = true;
Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.valveStatus.address, value); Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.valveStatus.address, value);
Task.Delay(2000).ContinueWith(_ => stopwatch.Restart();
while (isAwaitingResponse)
{ {
if (isAwaitingResponse) if (stopwatch.ElapsedMilliseconds > 1000)
{ {
MessageBox.Show("Превышено время ожидания ответа от устройства."); Console.WriteLine("Response timed out.");
isAwaitingResponse = false; break;
} }
return false; }
});
while (isAwaitingResponse) { continue; }
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error) if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
{ {
@@ -289,21 +314,22 @@ namespace Gidrolock_Modbus_Scanner
} }
} }
// Авария
private async void buttonAlarm_Click(object sender, EventArgs e) private async void buttonAlarm_Click(object sender, EventArgs e)
{ {
ushort value = alarmStatus ? (ushort)0 : (ushort)0xFF00; ushort value = alarmStatus ? (ushort)0 : (ushort)0xFF00;
isAwaitingResponse = true;
Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.alarmStatus.address, value); Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.alarmStatus.address, value);
Task.Delay(2000).ContinueWith(_ => stopwatch.Restart();
while (isAwaitingResponse)
{ {
if (isAwaitingResponse) if (stopwatch.ElapsedMilliseconds > 1000)
{ {
MessageBox.Show("Превышено время ожидания ответа от устройства."); Console.WriteLine("Response timed out.");
isAwaitingResponse = false; break;
} }
return false; }
});
while (isAwaitingResponse) { continue; }
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error) if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
{ {
@@ -313,21 +339,22 @@ namespace Gidrolock_Modbus_Scanner
} }
} }
// Режим уборки
private async void buttonCleaning_Click(object sender, EventArgs e) private async void buttonCleaning_Click(object sender, EventArgs e)
{ {
ushort value = cleaningStatus ? (ushort)0 : (ushort)0xFF00; ushort value = cleaningStatus ? (ushort)0 : (ushort)0xFF00;
isAwaitingResponse = true;
Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.cleaningMode.address, value); Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.cleaningMode.address, value);
Task.Delay(2000).ContinueWith(_ => stopwatch.Restart();
while (isAwaitingResponse)
{ {
if (isAwaitingResponse) if (stopwatch.ElapsedMilliseconds > 1000)
{ {
MessageBox.Show("Превышено время ожидания ответа от устройства."); Console.WriteLine("Response timed out.");
isAwaitingResponse = false; break;
} }
return false; }
});
while (isAwaitingResponse) { continue; }
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error) if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
{ {
@@ -337,6 +364,7 @@ namespace Gidrolock_Modbus_Scanner
} }
} }
// Задать скорость передачи данных для устройства
private async void buttonSetSpeed_Click(object sender, EventArgs e) private async void buttonSetSpeed_Click(object sender, EventArgs e)
{ {
try try
@@ -348,18 +376,20 @@ namespace Gidrolock_Modbus_Scanner
// send speed value to device // send speed value to device
// await for response // await for response
isAwaitingResponse = true;
Modbus.WriteSingleAsync(FunctionCode.WriteRegister, modbusID, device.baudRate.address, newSpeed); Modbus.WriteSingleAsync(FunctionCode.WriteRegister, modbusID, device.baudRate.address, newSpeed);
Task.Delay(2000).ContinueWith(_ =>
stopwatch.Restart();
while (isAwaitingResponse)
{ {
if (isAwaitingResponse) if (stopwatch.ElapsedMilliseconds > 1000)
{ {
MessageBox.Show("Превышено время ожидания ответа от устройства."); Console.WriteLine("Response timed out.");
isAwaitingResponse = false; break;
} }
return false; }
});
while (isAwaitingResponse) { continue; } if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
if (latestMessage.Status != ModbusStatus.Error)
{ {
port.Close(); port.Close();
port.BaudRate = newSpeed; port.BaudRate = newSpeed;
@@ -368,6 +398,194 @@ namespace Gidrolock_Modbus_Scanner
} }
catch (Exception ex) { MessageBox.Show(ex.Message); } 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;
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.");
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 public class Sensor : FlowLayoutPanel
{ {
@@ -377,7 +595,6 @@ namespace Gidrolock_Modbus_Scanner
} }
public class WiredSensor : Sensor public class WiredSensor : Sensor
{ {
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+

38
Main.cs
View File

@@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using System.IO.Ports; using System.IO.Ports;
using System.Reflection;
using System.Diagnostics;
namespace Gidrolock_Modbus_Scanner namespace Gidrolock_Modbus_Scanner
{ {
@@ -20,6 +22,7 @@ namespace Gidrolock_Modbus_Scanner
DateTime dateTime; DateTime dateTime;
Datasheet datasheet; Datasheet datasheet;
Stopwatch stopwatch = new Stopwatch();
#region Initialization #region Initialization
public App() public App()
{ {
@@ -56,12 +59,6 @@ namespace Gidrolock_Modbus_Scanner
System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location); System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.FileVersion; string version = fvi.FileVersion;
Console.WriteLine("Version: " + version); Console.WriteLine("Version: " + version);
Modbus.ResponseReceived += (sndr, msg) =>
{
latestMessage = msg;
isAwaitingResponse = false;
};
} }
void App_FormClosed(object sender, FormClosedEventArgs e) void App_FormClosed(object sender, FormClosedEventArgs e)
@@ -77,8 +74,10 @@ namespace Gidrolock_Modbus_Scanner
} }
#endregion #endregion
private async void ButtonConnect_Click(object sender, EventArgs e) private async void ButtonConnect_Click(object sender, EventArgs e)
{ {
if (cBoxPorts.SelectedItem.ToString() == "COM1") if (cBoxPorts.SelectedItem.ToString() == "COM1")
{ {
DialogResult res = MessageBox.Show("Выбран серийный порт COM1, который обычно является портом PS/2 или RS-232, не подключенным к Modbus устройству. Продолжить?", "Внимание", MessageBoxButtons.OKCancel); DialogResult res = MessageBox.Show("Выбран серийный порт COM1, который обычно является портом PS/2 или RS-232, не подключенным к Modbus устройству. Продолжить?", "Внимание", MessageBoxButtons.OKCancel);
@@ -123,18 +122,28 @@ namespace Gidrolock_Modbus_Scanner
latestMessage = null; latestMessage = null;
isAwaitingResponse = true; isAwaitingResponse = true;
var send = Modbus.ReadRegAsync((byte)upDownModbusID.Value, FunctionCode.ReadInput, 200, 6); 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("Истекло время ожидания ответа от устройства. Повторный запрос...");
isAwaitingResponse = false; isAwaitingResponse = false;
MessageBox.Show("Истекло время ожидания ответа от устройства."); counter++;
if (counter > 3)
{
AddLog("Устройство не отвечает. Проверьте соединение с устройством.");
return;
}
} }
}
if (latestMessage is null)
{
Console.WriteLine("Latest message is null;");
return; return;
}); }
while (isAwaitingResponse) { continue; }
if (latestMessage.Status == ModbusStatus.Error) if (latestMessage.Status == ModbusStatus.Error)
return; return;
@@ -189,6 +198,7 @@ namespace Gidrolock_Modbus_Scanner
void OnResponseReceived(object sender, ModbusResponseEventArgs e) void OnResponseReceived(object sender, ModbusResponseEventArgs e)
{ {
latestMessage = e;
isAwaitingResponse = false; isAwaitingResponse = false;
AddLog("Получен ответ: " + Modbus.ByteArrayToString(e.Message)); AddLog("Получен ответ: " + Modbus.ByteArrayToString(e.Message));
switch (e.Status) switch (e.Status)
@@ -352,7 +362,7 @@ namespace Gidrolock_Modbus_Scanner
d.cleaningMode = new Entry(RegisterType.Coil, 3); d.cleaningMode = new Entry(RegisterType.Coil, 3);
d.hasBattery = true; d.hasBattery = true;
d.batteryCharge = new Entry(RegisterType.Input, 1207); d.batteryCharge = new Entry(RegisterType.Input, 100);
d.wiredSensors = 7; d.wiredSensors = 7;
d.hasScenarioSensor = true; d.hasScenarioSensor = true;

100
Modbus.cs
View File

@@ -5,15 +5,16 @@ using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using System.Collections.Generic;
using static System.Net.Mime.MediaTypeNames; using static System.Net.Mime.MediaTypeNames;
using System.Diagnostics;
namespace Gidrolock_Modbus_Scanner namespace Gidrolock_Modbus_Scanner
{ {
public static class Modbus public static class Modbus
{ {
public static SerialPort port = new SerialPort(); public static SerialPort port = new SerialPort();
public static event EventHandler<ModbusResponseEventArgs> ResponseReceived = delegate { }; public static event EventHandler<ModbusResponseEventArgs> ResponseReceived = delegate { };
public static void Init() public static void Init()
@@ -210,7 +211,7 @@ namespace Gidrolock_Modbus_Scanner
#region CRC Computation #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 //Function expects a modbus message of any length as well as a 2 byte CRC array in which to
//return the CRC values: //return the CRC values:
@@ -235,33 +236,103 @@ namespace Gidrolock_Modbus_Scanner
CRC[1] = CRCHigh = (byte)((CRCFull >> 8) & 0xFF); CRC[1] = CRCHigh = (byte)((CRCFull >> 8) & 0xFF);
CRC[0] = CRCLow = (byte)(CRCFull & 0xFF); CRC[0] = CRCLow = (byte)(CRCFull & 0xFF);
} }
#endregion public static bool CheckResponse(byte[] response) // Проверка пакета на контрольную сумму
static void PortDataReceived(object sender, EventArgs e)
{ {
//Perform a basic CRC check:
byte[] CRC = new byte[2];
try try
{ {
Thread.Sleep(50); CRC[0] = response[response.Length - 2];
byte[] message = new byte[port.BytesToRead]; CRC[1] = response[response.Length - 1];
//Console.WriteLine("Bytes to read:" + port.BytesToRead); }
port.Read(message, 0, port.BytesToRead); catch (Exception err)
Console.WriteLine("Incoming message: " + ByteArrayToString(message, false)); {
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 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 byte[] buffer = new byte[255];
static int offset = 0;
static int count = 0;
static void PortDataReceived(object sender, EventArgs e)
{
try
{
stopwatch.Restart();
while (stopwatch.ElapsedMilliseconds < 20)
{
if (port.BytesToRead > 0)
{
stopwatch.Restart();
count = port.BytesToRead;
port.Read(buffer, offset, port.BytesToRead);
offset += count;
}
}
// 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)
{
endOfMessage = i;
break;
}
}
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 if (message[1] <= 0x04) // read functions
{ {
//Console.WriteLine("It's a read message"); //Console.WriteLine("It's a read message");
ResponseReceived.Invoke(null, new ModbusResponseEventArgs(message, ModbusStatus.ReadSuccess)); ResponseReceived.Invoke(null, new ModbusResponseEventArgs(message.ToArray(), ModbusStatus.ReadSuccess));
} }
else else
{ {
if (message[1] <= 0x10) // write functions if (message[1] <= 0x10) // write functions
{ {
//Console.WriteLine("It's a write message"); //Console.WriteLine("It's a write message");
ResponseReceived.Invoke(null, new ModbusResponseEventArgs(message, ModbusStatus.WriteSuccess)); ResponseReceived.Invoke(null, new ModbusResponseEventArgs(message.ToArray(), ModbusStatus.WriteSuccess));
} }
else // error codes else // error codes
{ {
//Console.WriteLine("It's an error"); //Console.WriteLine("It's an error");
ResponseReceived.Invoke(null, new ModbusResponseEventArgs(message, ModbusStatus.Error)); ResponseReceived.Invoke(null, new ModbusResponseEventArgs(message.ToArray(), ModbusStatus.Error));
} }
} }
} }
@@ -269,6 +340,7 @@ namespace Gidrolock_Modbus_Scanner
{ {
MessageBox.Show(err.Message, "Modbus message reception error"); MessageBox.Show(err.Message, "Modbus message reception error");
} }
port.DiscardInBuffer(); port.DiscardInBuffer();
} }

View File

@@ -30,6 +30,6 @@ using System.Runtime.InteropServices;
// Build Number // Build Number
// Revision // Revision
// //
[assembly: AssemblyVersion("0.9.2.0")] [assembly: AssemblyVersion("0.9.3.0")]
[assembly: AssemblyFileVersion("0.9.1.0")] [assembly: AssemblyFileVersion("0.9.3.0")]
[assembly: NeutralResourcesLanguage("en")] [assembly: NeutralResourcesLanguage("en")]

View File

@@ -7,5 +7,5 @@
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <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 - Standard Wi-Fi RS485
- Standard Radio RS485 - Standard Radio RS485
- Premium Plus - Premium Plus Wi-Fi

BIN
Screenshot_1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

83
Update.cs Normal file
View 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;
}
}
}
}
}