bro how tf did I make this work before

This commit is contained in:
nikzori
2025-01-21 14:38:59 +03:00
parent d70e5b155e
commit cce7b2295c
4 changed files with 277 additions and 176 deletions

170
Datasheet.Designer.cs generated
View File

@@ -39,16 +39,16 @@
this.label2 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox(); this.groupBox2 = new System.Windows.Forms.GroupBox();
this.labelCleaning = new System.Windows.Forms.Label();
this.buttonCleaning = new System.Windows.Forms.Button();
this.label7 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.buttonAlarm = new System.Windows.Forms.Button();
this.buttonValve = new System.Windows.Forms.Button();
this.labelValve = new System.Windows.Forms.Label(); this.labelValve = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label();
this.buttonValve = new System.Windows.Forms.Button();
this.buttonAlarm = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.buttonCleaning = new System.Windows.Forms.Button();
this.label8 = new System.Windows.Forms.Label();
this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel(); this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
this.label9 = new System.Windows.Forms.Label(); this.label9 = new System.Windows.Forms.Label();
this.label10 = new System.Windows.Forms.Label(); this.label10 = new System.Windows.Forms.Label();
@@ -84,6 +84,7 @@
this.buttonPoll.TabIndex = 7; this.buttonPoll.TabIndex = 7;
this.buttonPoll.Text = "Опрос"; this.buttonPoll.Text = "Опрос";
this.buttonPoll.UseVisualStyleBackColor = true; this.buttonPoll.UseVisualStyleBackColor = true;
this.buttonPoll.Click += new System.EventHandler(this.buttonPoll_Click);
// //
// buttonSetID // buttonSetID
// //
@@ -149,7 +150,7 @@
// //
// groupBox2 // groupBox2
// //
this.groupBox2.Controls.Add(this.label8); this.groupBox2.Controls.Add(this.labelCleaning);
this.groupBox2.Controls.Add(this.buttonCleaning); this.groupBox2.Controls.Add(this.buttonCleaning);
this.groupBox2.Controls.Add(this.label7); this.groupBox2.Controls.Add(this.label7);
this.groupBox2.Controls.Add(this.label6); this.groupBox2.Controls.Add(this.label6);
@@ -166,6 +167,81 @@
this.groupBox2.TabStop = false; this.groupBox2.TabStop = false;
this.groupBox2.Text = "Общее"; this.groupBox2.Text = "Общее";
// //
// labelCleaning
//
this.labelCleaning.AutoSize = true;
this.labelCleaning.Location = new System.Drawing.Point(52, 78);
this.labelCleaning.Name = "labelCleaning";
this.labelCleaning.Size = new System.Drawing.Size(33, 13);
this.labelCleaning.TabIndex = 9;
this.labelCleaning.Text = "выкл";
//
// buttonCleaning
//
this.buttonCleaning.Location = new System.Drawing.Point(192, 73);
this.buttonCleaning.Name = "buttonCleaning";
this.buttonCleaning.Size = new System.Drawing.Size(73, 23);
this.buttonCleaning.TabIndex = 8;
this.buttonCleaning.Text = "Включить";
this.buttonCleaning.UseVisualStyleBackColor = true;
this.buttonCleaning.Click += new System.EventHandler(this.buttonCleaning_Click);
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(6, 78);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(48, 13);
this.label7.TabIndex = 7;
this.label7.Text = "Уборка:";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(61, 48);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(24, 13);
this.label6.TabIndex = 6;
this.label6.Text = "нет";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(6, 48);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(58, 13);
this.label5.TabIndex = 5;
this.label5.Text = "Протечка:";
//
// button2
//
this.button2.Location = new System.Drawing.Point(271, 14);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(73, 52);
this.button2.TabIndex = 4;
this.button2.Text = "Аварийное открытие";
this.button2.UseVisualStyleBackColor = true;
//
// buttonAlarm
//
this.buttonAlarm.Location = new System.Drawing.Point(192, 43);
this.buttonAlarm.Name = "buttonAlarm";
this.buttonAlarm.Size = new System.Drawing.Size(73, 23);
this.buttonAlarm.TabIndex = 3;
this.buttonAlarm.Text = "Авария";
this.buttonAlarm.UseVisualStyleBackColor = true;
this.buttonAlarm.Click += new System.EventHandler(this.buttonAlarm_Click);
//
// buttonValve
//
this.buttonValve.Location = new System.Drawing.Point(192, 14);
this.buttonValve.Name = "buttonValve";
this.buttonValve.Size = new System.Drawing.Size(73, 23);
this.buttonValve.TabIndex = 2;
this.buttonValve.Text = "Закрыть";
this.buttonValve.UseVisualStyleBackColor = true;
this.buttonValve.Click += new System.EventHandler(this.buttonValve_Click);
//
// labelValve // labelValve
// //
this.labelValve.AutoSize = true; this.labelValve.AutoSize = true;
@@ -184,80 +260,6 @@
this.label4.TabIndex = 0; this.label4.TabIndex = 0;
this.label4.Text = "Кран:"; this.label4.Text = "Кран:";
// //
// buttonValve
//
this.buttonValve.Location = new System.Drawing.Point(192, 14);
this.buttonValve.Name = "buttonValve";
this.buttonValve.Size = new System.Drawing.Size(73, 23);
this.buttonValve.TabIndex = 2;
this.buttonValve.Text = "Закрыть";
this.buttonValve.UseVisualStyleBackColor = true;
this.buttonValve.Click += new System.EventHandler(this.buttonValve_Click);
//
// buttonAlarm
//
this.buttonAlarm.Location = new System.Drawing.Point(192, 43);
this.buttonAlarm.Name = "buttonAlarm";
this.buttonAlarm.Size = new System.Drawing.Size(73, 23);
this.buttonAlarm.TabIndex = 3;
this.buttonAlarm.Text = "Авария";
this.buttonAlarm.UseVisualStyleBackColor = true;
this.buttonAlarm.Click += new System.EventHandler(this.buttonAlarm_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(271, 14);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(73, 52);
this.button2.TabIndex = 4;
this.button2.Text = "Аварийное открытие";
this.button2.UseVisualStyleBackColor = true;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(6, 48);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(58, 13);
this.label5.TabIndex = 5;
this.label5.Text = "Протечка:";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(61, 48);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(24, 13);
this.label6.TabIndex = 6;
this.label6.Text = "нет";
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(6, 78);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(48, 13);
this.label7.TabIndex = 7;
this.label7.Text = "Уборка:";
//
// buttonCleaning
//
this.buttonCleaning.Location = new System.Drawing.Point(192, 73);
this.buttonCleaning.Name = "buttonCleaning";
this.buttonCleaning.Size = new System.Drawing.Size(73, 23);
this.buttonCleaning.TabIndex = 8;
this.buttonCleaning.Text = "Включить";
this.buttonCleaning.UseVisualStyleBackColor = true;
//
// label8
//
this.label8.AutoSize = true;
this.label8.Location = new System.Drawing.Point(52, 78);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(33, 13);
this.label8.TabIndex = 9;
this.label8.Text = "выкл";
//
// flowLayoutPanel1 // flowLayoutPanel1
// //
this.flowLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) this.flowLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
@@ -339,7 +341,7 @@
private System.Windows.Forms.Label label5; private System.Windows.Forms.Label label5;
private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button buttonAlarm; private System.Windows.Forms.Button buttonAlarm;
private System.Windows.Forms.Label label8; private System.Windows.Forms.Label labelCleaning;
private System.Windows.Forms.Button buttonCleaning; private System.Windows.Forms.Button buttonCleaning;
private System.Windows.Forms.Label label7; private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label6; private System.Windows.Forms.Label label6;

View File

@@ -20,10 +20,18 @@ namespace Gidrolock_Modbus_Scanner
byte modbusID; byte modbusID;
Device device; Device device;
byte[] message;
byte[] data;
EventHandler<ModbusResponseEventArgs> handler;
SerialPort port = Modbus.port; SerialPort port = Modbus.port;
bool isPolling = false; bool isPolling = false;
bool isAwaitingResponse = false; bool isAwaitingResponse = false;
byte[] message = new byte[255];
bool isValveClosed = false;
bool alarmStatus = false;
bool cleaningStatus = false;
public Datasheet(byte modbusID, Device device) public Datasheet(byte modbusID, Device device)
{ {
@@ -31,31 +39,78 @@ namespace Gidrolock_Modbus_Scanner
nudModbusID.Maximum = 246; nudModbusID.Maximum = 246;
this.modbusID = modbusID; this.modbusID = modbusID;
this.device = device; this.device = device;
if (!device.hasCleaningMode)
{
labelCleaning.Text = "Недоступна.";
buttonCleaning.Enabled = false;
} }
handler = (sndr, msg) =>
{
message = msg.Message;
data = msg.Data;
};
}
private async void buttonPoll_Click(object sender, EventArgs e)
{
// hardcoded for now, probably easier to keep it like this in the future
if (await PollEntry(device.valveStatus))
{
if (data.Last() > 0)
labelValve.Text = "Закрыт";
else labelValve.Text = "Открыт";
}
}
async Task<bool> PollEntry(Entry entry)
{
var send = Modbus.ReadRegAsync(modbusID, (FunctionCode)entry.registerType, entry.address, entry.length);
isAwaitingResponse = true;
Task<bool> delay = Task.WhenAny(Task.Delay(2000), Task.Run(() => { while (isAwaitingResponse) { } return true; })).ContinueWith((t) =>
{
if (isAwaitingResponse)
{
Console.WriteLine("Response timed out.");
isAwaitingResponse = false;
}
return false;
});
return await delay;
}
private async void buttonSetID_Click(object sender, EventArgs e) private async void buttonSetID_Click(object sender, EventArgs e)
{ {
Modbus.WriteSingleAsync(FunctionCode.WriteRegister, modbusID, 200, (byte)nudModbusID.Value); Modbus.WriteSingleAsync(FunctionCode.WriteRegister, modbusID, 200, (byte)nudModbusID.Value);
byte[] data = null;
Modbus.ResponseReceived += (sndr, msg) =>
{
data = msg.Data;
};
await Task.Run(() => await Task.Run(() =>
{ {
while (data is null) { continue; } while (data is null) { continue; }
if (message[1] > 0x10) //exception code check if (message[1] > 0x10) //exception code check
return; return;
this.modbusID = data[1]; //no exception, can just take the byte
message = null;
data = null;
}); });
} }
private void buttonValve_Click(object sender, EventArgs e) private void buttonValve_Click(object sender, EventArgs e)
{ {
ushort value = isValveClosed ? (ushort)0 : (ushort)1;
Modbus.WriteSingleAsync((FunctionCode)device.valveStatus.registerType, modbusID, device.valveStatus.address, value);
} }
private void buttonAlarm_Click(object sender, EventArgs e) private void buttonAlarm_Click(object sender, EventArgs e)
{
ushort value = alarmStatus ? (ushort)0 : (ushort)1;
Modbus.WriteSingleAsync((FunctionCode)device.valveStatus.registerType, modbusID, device.alarmStatus.address, value);
}
private void buttonCleaning_Click(object sender, EventArgs e)
{ {
} }

132
Main.cs
View File

@@ -20,7 +20,13 @@ namespace Gidrolock_Modbus_Scanner
public byte[] latestMessage; public byte[] latestMessage;
public Dictionary<string, string> models = new Dictionary<string, string>(); public Dictionary<string, string> models = new Dictionary<string, string>();
byte[] message = null;
byte[] data = null;
DateTime dateTime; DateTime dateTime;
Datasheet datasheet;
#region Initialization #region Initialization
public App() public App()
{ {
@@ -32,8 +38,8 @@ namespace Gidrolock_Modbus_Scanner
TextBox_Log.Text = "Приложение готово к работе."; TextBox_Log.Text = "Приложение готово к работе.";
cBoxDevice.Items.Add("Standard"); cBoxDevice.Items.Add("Standard");
cBoxDevice.Items.Add("Premium Plus");
cBoxDevice.Items.Add("Inteli"); cBoxDevice.Items.Add("Inteli");
cBoxDevice.Items.Add("Premium Plus");
cBoxDevice.Items.Add("Premium"); cBoxDevice.Items.Add("Premium");
cBoxDevice.SelectedIndex = 0; cBoxDevice.SelectedIndex = 0;
@@ -44,8 +50,8 @@ namespace Gidrolock_Modbus_Scanner
UpDown_ModbusID.Maximum = 247; UpDown_ModbusID.Maximum = 247;
models.Add("Standard", "STW485"); models.Add("Standard", "STW485");
models.Add("Premuim Plus", "BUP485");
models.Add("Inteli", "INTELI"); models.Add("Inteli", "INTELI");
models.Add("Premuim Plus", "BUP485");
models.Add("Premium", "BUP485"); models.Add("Premium", "BUP485");
/* - Version Check - */ /* - Version Check - */
@@ -53,6 +59,12 @@ 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) =>
{
message = msg.Message;
data = msg.Data;
};
} }
void App_FormClosed(object sender, FormClosedEventArgs e) void App_FormClosed(object sender, FormClosedEventArgs e)
@@ -108,62 +120,89 @@ namespace Gidrolock_Modbus_Scanner
// if matching model is found, instantiate device window // if matching model is found, instantiate device window
// setup event listener // setup event listener
byte[] message = null;
byte[] data = null;
Modbus.ResponseReceived += (sndr, msg) =>
{
message = msg.Message;
data = msg.Data;
};
// send message // send message
AddLog("Проверка модели устройства."); AddLog("Проверка модели устройства.");
byte id = (byte)UpDown_ModbusID.Value;
Modbus.ReadRegAsync(id, FunctionCode.ReadInput, 200, 6);
await Task.Run(() => if (await PollModel())
{ {
while (message is null) { continue; } Console.WriteLine("true");
if (message[1] > 0x10) //exception code check
return;
// confirm the model // confirm the model
string response = ByteArrayToUnicode(data); string response = ByteArrayToUnicode(data);
if (response != models[cBoxDevice.SelectedItem.ToString()]) if (response != models[cBoxDevice.SelectedItem.ToString()])
{ {
// response doesn't match expected model // response doesn't match expected model
AddLog("Ответ устройства не соответствует выбранной модели. Поиск подходящей модели.");
// check whether it matches anything else, offer to open that model if it does // check whether it matches anything else, offer to open that model if it does
string match = ""; string match = "";
string model = "";
int index = 0;
foreach (string key in models.Keys) foreach (string key in models.Keys)
{ {
if (models[key] == response) if (models[key] == response)
{
match = models[key]; match = models[key];
model = key;
break;
}
index++;
} }
// if no matches found,
if (match == "") if (match == "") // if no matches found
MessageBox.Show("Подключенное устройство не соответствует ни одной из поддерживаемых моделей.");
else // match found, offer to switch to that model instead
{ {
// report unknown device DialogResult result = MessageBox.Show(("Подключенное устройство соответствует модели " + model + ". Нажмите ОК, чтобы открыть настройки для модели " + model), "Внимание", MessageBoxButtons.OKCancel);
} if (result == DialogResult.OK)
else
{ {
// offer to switch to that model instead
// instantiate panel // instantiate panel
AddLog("Открываю панель конфигурации.");
Device device = GetDevice((DeviceType)index);
StartDatasheet(device);
}
} }
} }
else else
{ {
// model is correct, instantiate config panel // model is correct, instantiate config panel
int index = cBoxDevice.SelectedIndex;
AddLog("Устройство соответстует модели, открываю панель конфигурации.");
Device device = GetDevice((DeviceType)index);
StartDatasheet(device);
}
} }
});
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show(ex.Message); MessageBox.Show(ex.Message);
} }
} }
async Task<bool> PollModel()
{
var send = Modbus.ReadRegAsync((byte)UpDown_ModbusID.Value, FunctionCode.ReadInput, 200, 6);
isAwaitingResponse = true;
Task<bool> delay = Task.WhenAny(Task.Delay(2000), Task.Run(() => { while (isAwaitingResponse) { } return true; })).ContinueWith((t) =>
{
if (isAwaitingResponse)
{
Console.WriteLine("Response timed out.");
isAwaitingResponse = false;
}
return false;
});
return await delay;
}
void StartDatasheet(Device device)
{
datasheet = new Datasheet((byte)UpDown_ModbusID.Value, device);
datasheet.Show();
}
void CBox_Ports_Click(object sender, EventArgs e) void CBox_Ports_Click(object sender, EventArgs e)
@@ -292,8 +331,51 @@ namespace Gidrolock_Modbus_Scanner
break; break;
} }
} }
public Device GetDevice(DeviceType dt)
{
Device d = new Device();
switch (dt)
{
case DeviceType.Standard:
d.modelName = "Standard";
d.name = "Gidrolock Standard Wi-Fi RS485";
d.id = 30;
d.modelName = "STW485";
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 = true;
d.sensorsAlarm = new Entry(RegisterType.Discrete, 1343, 24);
d.radioStatus = new Entry(RegisterType.Input, 1215, 21);
break;
case DeviceType.Inteli:
d.modelName = "Inteli";
break;
case DeviceType.PremiumPlus:
d.modelName = "Premium Plus";
break;
case DeviceType.Premium:
d.modelName = "Premium";
break;
default:
break;
}
return d;
}
} }
} }
public enum FunctionCode { ReadCoil = 1, ReadDiscrete = 2, ReadHolding = 3, ReadInput = 4, WriteCoil = 5, WriteRegister = 6, WriteMultCoils = 15, WriteMultRegisters = 16 }; public enum FunctionCode { ReadCoil = 1, ReadDiscrete = 2, ReadHolding = 3, ReadInput = 4, WriteCoil = 5, WriteRegister = 6, WriteMultCoils = 15, WriteMultRegisters = 16 };
public enum SelectedPath { File, Folder }; //public enum SelectedPath { File, Folder };
public enum DeviceType { Standard, Inteli, PremiumPlus, Premium };

View File

@@ -1,82 +1,44 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Gidrolock_Modbus_Scanner namespace Gidrolock_Modbus_Scanner
{ {
public class Device : public class Device
{ {
public string name; public string name;
public byte id; public byte id;
public string modelName; public string modelName;
public List<Entry> entries;
public Device(string name, string modelName) public Entry valveStatus;
{ public Entry alarmStatus;
this.name = name;
}
} public bool hasCleaningMode;
public Entry cleaningMode;
public interface IHasWiredSensors public bool hasBattery;
{ public Entry batteryCharge;
Entry GetAlarmStatuses(); public Entry batteryUsed; // питание от баттареи/электросети для устройств с баттареями
Entry GetSignalLoss();
} public int wiredSensors;
public interface IHasWirelessSensors public bool hasScenarioSensor;
{ public Entry sensorsAlarm;
Entry GetWirelessSensors();
Entry GetSensors(); public Entry wiredLineBreak;
}
public interface IHasEmergencyOpen public Entry radioStatus;
{
void EmergencyOpen();
} }
public struct Entry public struct Entry
{ {
public string name;
public RegisterType registerType; public RegisterType registerType;
public ushort address; public ushort address;
public ushort length; public ushort length;
public string dataType;
public List<string> labels;
public Dictionary<string, string> valueParse;
public bool readOnce;
public bool isModbusID;
public Entry(string name, RegisterType registerType, ushort address, ushort length = 1, string dataType = "uint16", List<string> labels = null, Dictionary<string, string> valueParse = null, bool readOnce = false, bool isModbusID = false) public Entry(RegisterType registerType, ushort address, ushort length = 1)
{
this.name = name;
this.registerType = registerType;
this.address = address;
this.length = length;
this.dataType = dataType;
this.labels = labels;
this.valueParse = valueParse;
this.readOnce = readOnce;
this.isModbusID = isModbusID;
}
}
public struct CheckEntry
{
public RegisterType registerType;
public ushort address;
public ushort length;
public string dataType;
public string expectedValue;
public CheckEntry(RegisterType registerType, ushort address, ushort length, string dataType, string expectedValue)
{ {
this.registerType = registerType; this.registerType = registerType;
this.address = address; this.address = address;
this.length = length; this.length = length;
this.dataType = dataType;
this.expectedValue = expectedValue;
} }
} }
public enum RegisterType { Coil = 1, Discrete = 2, Holding = 3, Input = 4} public enum RegisterType { Coil = 1, Discrete = 2, Holding = 3, Input = 4}