Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36304e1fd0 | ||
|
|
580e2246bb | ||
|
|
e32c701bd6 | ||
|
|
bcf0fb7a5c | ||
|
|
e00e96bea7 | ||
|
|
80bf64b7d5 | ||
|
|
e9f3a8d350 | ||
|
|
615394a479 | ||
|
|
795488d01e |
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;
|
||||
}
|
||||
}
|
||||
375
Datasheet.cs
375
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;
|
||||
|
||||
@@ -18,22 +22,36 @@ namespace Gidrolock_Modbus_Scanner
|
||||
|
||||
SerialPort port = Modbus.port;
|
||||
bool isPolling = false;
|
||||
bool isAwaitingResponse = false;
|
||||
static bool isAwaitingResponse = false;
|
||||
static bool responseReceived = false;
|
||||
|
||||
|
||||
bool isValveClosed = false;
|
||||
bool isValveClosed = false;
|
||||
bool alarmStatus = false;
|
||||
bool cleaningStatus = false;
|
||||
|
||||
List<WiredSensor> wiredSensors = new List<WiredSensor>();
|
||||
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()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
firmwareProgressBar.Minimum = 0;
|
||||
firmwareProgressBar.Maximum = 100;
|
||||
nudModbusID.Minimum = 1;
|
||||
nudModbusID.Maximum = 246;
|
||||
nudModbusID.Value = modbusID;
|
||||
@@ -63,14 +81,14 @@ 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++)
|
||||
{
|
||||
WiredSensor ws = new WiredSensor(i) { Width = 495, Height = 24 };
|
||||
sensorPanel.Controls.Add(ws);
|
||||
ws.Visible = true;
|
||||
|
||||
|
||||
}
|
||||
if (device.hasScenarioSensor)
|
||||
{
|
||||
@@ -102,7 +120,23 @@ namespace Gidrolock_Modbus_Scanner
|
||||
// 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
|
||||
bool res = await PollEntry(device.firmware);
|
||||
Console.WriteLine("Polling for alarm status, poll success: " + res);
|
||||
if (res)
|
||||
{
|
||||
labelFirmware.Text = App.ByteArrayToUnicode(latestMessage.Data);
|
||||
}
|
||||
|
||||
if (device.hasBattery)
|
||||
{
|
||||
res = await PollEntry(device.batteryCharge);
|
||||
if (res)
|
||||
{
|
||||
labelBattery.Text = latestMessage.Data.Last().ToString();
|
||||
}
|
||||
}
|
||||
|
||||
res = await PollEntry(device.valveStatus);
|
||||
Console.WriteLine("Polling for valve status, poll success: " + res);
|
||||
if (res)
|
||||
{
|
||||
@@ -124,6 +158,8 @@ namespace Gidrolock_Modbus_Scanner
|
||||
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;
|
||||
@@ -146,42 +182,14 @@ namespace Gidrolock_Modbus_Scanner
|
||||
if (latestMessage.Data.Last() > 0)
|
||||
{
|
||||
cleaningStatus = true;
|
||||
buttonAlarm.Text = "Выключить";
|
||||
labelAlarm.Text = "вкл";
|
||||
buttonCleaning.Text = "Выключить";
|
||||
labelCleaning.Text = "вкл";
|
||||
}
|
||||
else
|
||||
{
|
||||
cleaningStatus = false;
|
||||
buttonAlarm.Text = "Включить";
|
||||
labelAlarm.Text = "выкл";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (device.hasBattery)
|
||||
{
|
||||
res = await PollEntry(device.batteryCharge);
|
||||
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 = "выкл";
|
||||
buttonCleaning.Text = "Включить";
|
||||
labelCleaning.Text = "выкл";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -199,6 +207,7 @@ namespace Gidrolock_Modbus_Scanner
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("Polling for radio status");
|
||||
res = await PollEntry(device.radioStatus);
|
||||
if (res)
|
||||
{
|
||||
@@ -234,20 +243,23 @@ namespace Gidrolock_Modbus_Scanner
|
||||
catch (Exception err) { MessageBox.Show(err.Message); }
|
||||
}
|
||||
|
||||
//Опрос всех записей
|
||||
async Task<bool> PollEntry(Entry entry)
|
||||
{
|
||||
bool res = false;
|
||||
Modbus.ReadRegAsync(modbusID, (FunctionCode)entry.registerType, entry.address, entry.length);
|
||||
isAwaitingResponse = true;
|
||||
await 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("Превышено время ожидания ответа от устройства.");
|
||||
isAwaitingResponse = false;
|
||||
Console.WriteLine("Response timed out.");
|
||||
break;
|
||||
}
|
||||
});
|
||||
while (isAwaitingResponse) { continue; }
|
||||
}
|
||||
|
||||
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
|
||||
res = true;
|
||||
@@ -256,40 +268,43 @@ namespace Gidrolock_Modbus_Scanner
|
||||
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;
|
||||
Modbus.WriteSingleAsync(FunctionCode.WriteRegister, modbusID, 128, newID);
|
||||
await Task.Delay(2000).ContinueWith(_ =>
|
||||
|
||||
stopwatch.Restart();
|
||||
while (isAwaitingResponse)
|
||||
{
|
||||
if (isAwaitingResponse)
|
||||
if (stopwatch.ElapsedMilliseconds > 1000)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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);
|
||||
|
||||
await Task.Delay(2000).ContinueWith(_ =>
|
||||
stopwatch.Restart();
|
||||
while (isAwaitingResponse)
|
||||
{
|
||||
if (isAwaitingResponse)
|
||||
if (stopwatch.ElapsedMilliseconds > 1000)
|
||||
{
|
||||
MessageBox.Show("Превышено время ожидания ответа от устройства.");
|
||||
isAwaitingResponse = false;
|
||||
Console.WriteLine("Response timed out.");
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
while (isAwaitingResponse) { continue; }
|
||||
}
|
||||
|
||||
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
|
||||
{
|
||||
@@ -299,21 +314,22 @@ namespace Gidrolock_Modbus_Scanner
|
||||
}
|
||||
}
|
||||
|
||||
// Авария
|
||||
private async void buttonAlarm_Click(object sender, EventArgs e)
|
||||
{
|
||||
ushort value = alarmStatus ? (ushort)0 : (ushort)0xFF00;
|
||||
isAwaitingResponse = true;
|
||||
Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.alarmStatus.address, value);
|
||||
|
||||
await Task.Delay(2000).ContinueWith(_ =>
|
||||
stopwatch.Restart();
|
||||
while (isAwaitingResponse)
|
||||
{
|
||||
if (isAwaitingResponse)
|
||||
if (stopwatch.ElapsedMilliseconds > 1000)
|
||||
{
|
||||
MessageBox.Show("Превышено время ожидания ответа от устройства.");
|
||||
isAwaitingResponse = false;
|
||||
Console.WriteLine("Response timed out.");
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
while (isAwaitingResponse) { continue; }
|
||||
}
|
||||
|
||||
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
|
||||
{
|
||||
@@ -323,21 +339,22 @@ namespace Gidrolock_Modbus_Scanner
|
||||
}
|
||||
}
|
||||
|
||||
// Режим уборки
|
||||
private async void buttonCleaning_Click(object sender, EventArgs e)
|
||||
{
|
||||
ushort value = cleaningStatus ? (ushort)0 : (ushort)0xFF00;
|
||||
isAwaitingResponse = true;
|
||||
Modbus.WriteSingleAsync(FunctionCode.WriteCoil, modbusID, device.cleaningMode.address, value);
|
||||
|
||||
await Task.Delay(2000).ContinueWith(_ =>
|
||||
stopwatch.Restart();
|
||||
while (isAwaitingResponse)
|
||||
{
|
||||
if (isAwaitingResponse)
|
||||
if (stopwatch.ElapsedMilliseconds > 1000)
|
||||
{
|
||||
MessageBox.Show("Превышено время ожидания ответа от устройства.");
|
||||
isAwaitingResponse = false;
|
||||
Console.WriteLine("Response timed out.");
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
while (isAwaitingResponse) { continue; }
|
||||
}
|
||||
|
||||
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
|
||||
{
|
||||
@@ -347,6 +364,7 @@ namespace Gidrolock_Modbus_Scanner
|
||||
}
|
||||
}
|
||||
|
||||
// Задать скорость передачи данных для устройства
|
||||
private async void buttonSetSpeed_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
@@ -358,18 +376,20 @@ namespace Gidrolock_Modbus_Scanner
|
||||
|
||||
// send speed value to device
|
||||
// await for response
|
||||
isAwaitingResponse = true;
|
||||
Modbus.WriteSingleAsync(FunctionCode.WriteRegister, modbusID, device.baudRate.address, newSpeed);
|
||||
await Task.Delay(2000).ContinueWith(_ =>
|
||||
|
||||
stopwatch.Restart();
|
||||
while (isAwaitingResponse)
|
||||
{
|
||||
if (isAwaitingResponse)
|
||||
if (stopwatch.ElapsedMilliseconds > 1000)
|
||||
{
|
||||
MessageBox.Show("Превышено время ожидания ответа от устройства.");
|
||||
isAwaitingResponse = false;
|
||||
Console.WriteLine("Response timed out.");
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
while (isAwaitingResponse) { continue; }
|
||||
if (latestMessage.Status != ModbusStatus.Error)
|
||||
}
|
||||
|
||||
if (latestMessage != null && latestMessage.Status != ModbusStatus.Error)
|
||||
{
|
||||
port.Close();
|
||||
port.BaudRate = newSpeed;
|
||||
@@ -378,6 +398,194 @@ namespace Gidrolock_Modbus_Scanner
|
||||
}
|
||||
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
|
||||
{
|
||||
@@ -387,7 +595,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+
|
||||
|
||||
|
||||
38
Main.cs
38
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()
|
||||
{
|
||||
@@ -56,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)
|
||||
@@ -77,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);
|
||||
@@ -123,18 +122,28 @@ 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("Истекло время ожидания ответа от устройства. Повторный запрос...");
|
||||
isAwaitingResponse = false;
|
||||
MessageBox.Show("Истекло время ожидания ответа от устройства.");
|
||||
counter++;
|
||||
if (counter > 3)
|
||||
{
|
||||
AddLog("Устройство не отвечает. Проверьте соединение с устройством.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (latestMessage is null)
|
||||
{
|
||||
Console.WriteLine("Latest message is null;");
|
||||
return;
|
||||
});
|
||||
|
||||
while (isAwaitingResponse) { continue; }
|
||||
|
||||
}
|
||||
if (latestMessage.Status == ModbusStatus.Error)
|
||||
return;
|
||||
|
||||
@@ -189,6 +198,7 @@ namespace Gidrolock_Modbus_Scanner
|
||||
|
||||
void OnResponseReceived(object sender, ModbusResponseEventArgs e)
|
||||
{
|
||||
latestMessage = e;
|
||||
isAwaitingResponse = false;
|
||||
AddLog("Получен ответ: " + Modbus.ByteArrayToString(e.Message));
|
||||
switch (e.Status)
|
||||
@@ -352,7 +362,7 @@ 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;
|
||||
|
||||
100
Modbus.cs
100
Modbus.cs
@@ -5,15 +5,16 @@ 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
|
||||
{
|
||||
public static class Modbus
|
||||
{
|
||||
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,33 +236,103 @@ 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));
|
||||
CRC[0] = response[response.Length - 2];
|
||||
CRC[1] = response[response.Length - 1];
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
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
|
||||
{
|
||||
//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
|
||||
{
|
||||
if (message[1] <= 0x10) // write functions
|
||||
{
|
||||
//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
|
||||
{
|
||||
//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");
|
||||
}
|
||||
|
||||
port.DiscardInBuffer();
|
||||
}
|
||||
|
||||
|
||||
@@ -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.3.0")]
|
||||
[assembly: AssemblyFileVersion("0.9.3.0")]
|
||||
[assembly: NeutralResourcesLanguage("en")]
|
||||
|
||||
@@ -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>:
|
||||
- Standard Wi-Fi RS485
|
||||
- Standard Radio RS485
|
||||
- Premium Plus
|
||||
- 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