firmware test
This commit is contained in:
69
Datasheet.Designer.cs
generated
69
Datasheet.Designer.cs
generated
@@ -55,9 +55,15 @@
|
|||||||
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.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 +318,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 +331,63 @@
|
|||||||
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.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 = "Прошивка";
|
||||||
|
//
|
||||||
// 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 +402,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 +437,10 @@
|
|||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
96
Datasheet.cs
96
Datasheet.cs
@@ -2,8 +2,10 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
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.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
|
||||||
@@ -27,9 +29,23 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
|
|
||||||
List<WiredSensor> wiredSensors = new List<WiredSensor>();
|
List<WiredSensor> wiredSensors = new List<WiredSensor>();
|
||||||
List<WirelessSensor> wirelessSensors;
|
List<WirelessSensor> wirelessSensors;
|
||||||
|
public static string firmwarePath;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
//firmwarePathLabel.Text = ofd.FileName;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
public Datasheet(byte modbusID, Device device) : base()
|
public Datasheet(byte modbusID, Device device) : base()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
@@ -368,6 +384,86 @@ 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.TrySetApartmentState(ApartmentState.STA);
|
||||||
|
fileThread.Start();
|
||||||
|
while (!fileThread.IsAlive) { Thread.Sleep(1); }
|
||||||
|
Thread.Sleep(1);
|
||||||
|
fileThread.Join();
|
||||||
|
}
|
||||||
|
catch (Exception err) { MessageBox.Show(err.Message); }
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WriteFirmware_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
FileStream fileStream = File.OpenRead(firmwarePath);
|
||||||
|
long bytesLeft = fileStream.Length;
|
||||||
|
int offset = 0;
|
||||||
|
int count;
|
||||||
|
byte[] buffer;
|
||||||
|
byte[] bdma;
|
||||||
|
List<byte> message;
|
||||||
|
int dma = 0;
|
||||||
|
Task.Run(() =>
|
||||||
|
{
|
||||||
|
while (bytesLeft > 0)
|
||||||
|
{
|
||||||
|
count = bytesLeft > 64 ? 64 : (int)bytesLeft;
|
||||||
|
buffer = new byte[count];
|
||||||
|
fileStream.Read(buffer, offset, count);
|
||||||
|
|
||||||
|
|
||||||
|
bdma = new byte[2];
|
||||||
|
bdma[0] = (byte)((dma & 0xFF_00) >> 16);
|
||||||
|
bdma[1] = (byte)(dma & 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(bdma[0]);
|
||||||
|
message.Add(bdma[1]);
|
||||||
|
|
||||||
|
|
||||||
|
for (int i = 0; i < buffer.Length; i++)
|
||||||
|
{
|
||||||
|
message.Add(buffer[i]);
|
||||||
|
}
|
||||||
|
byte[] CRC = new byte[2];
|
||||||
|
Modbus.GetCRC(message.ToArray(), ref CRC);
|
||||||
|
message.Add(CRC[0]);
|
||||||
|
message.Add(CRC[1]);
|
||||||
|
|
||||||
|
Console.WriteLine("Outgoing firmware message: " + Modbus.ByteArrayToString(message.ToArray()));
|
||||||
|
|
||||||
|
isAwaitingResponse = true;
|
||||||
|
port.Write(message.ToArray(), 0, message.Count);
|
||||||
|
|
||||||
|
Task.Delay(2000).ContinueWith(_ =>
|
||||||
|
{
|
||||||
|
if (isAwaitingResponse)
|
||||||
|
{
|
||||||
|
MessageBox.Show("Превышено время ожидания ответа от устройства.");
|
||||||
|
isAwaitingResponse = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
while (isAwaitingResponse) { continue; }
|
||||||
|
|
||||||
|
bytesLeft -= count;
|
||||||
|
offset += count;
|
||||||
|
dma += 32;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public class Sensor : FlowLayoutPanel
|
public class Sensor : FlowLayoutPanel
|
||||||
{
|
{
|
||||||
|
|||||||
3
Main.cs
3
Main.cs
@@ -134,7 +134,8 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
});
|
});
|
||||||
|
|
||||||
while (isAwaitingResponse) { continue; }
|
while (isAwaitingResponse) { continue; }
|
||||||
|
if (latestMessage is null)
|
||||||
|
return;
|
||||||
if (latestMessage.Status == ModbusStatus.Error)
|
if (latestMessage.Status == ModbusStatus.Error)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|||||||
@@ -210,7 +210,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:
|
||||||
|
|||||||
@@ -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")]
|
||||||
|
|||||||
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