Modbus working 22/11/2024

This commit is contained in:
2024-11-22 11:54:01 +07:00
parent 869ab4bc21
commit 092429cec8
4 changed files with 592 additions and 179 deletions

View File

@@ -33,7 +33,7 @@ namespace FAtoPA
{
connection.Open();
var createtablecmd = connection.CreateCommand();
createtablecmd.CommandText = "CREATE TABLE IF NOT EXISTS FsmData (SIID TEXT PRIMARY KEY, Enable INTEGER, Description TEXT)";
createtablecmd.CommandText = "CREATE TABLE IF NOT EXISTS FsmData (SIID TEXT PRIMARY KEY, Enable INTEGER, Label TEXT, Type TEXT)";
createtablecmd.ExecuteNonQuery();
Debug.WriteLine("CreateFSMTable success");
return true;
@@ -79,7 +79,8 @@ namespace FAtoPA
{
while (reader.Read())
{
FSMData fsmData = new FSMData(reader.GetString(0), reader.GetBoolean(1), reader.GetString(2));
FSMData fsmData = new FSMData(reader.GetString(0), reader.GetBoolean(1), reader.GetString(2), reader.GetString(3));
fsmDatas.Add(fsmData);
}
}
@@ -97,10 +98,11 @@ namespace FAtoPA
foreach (var item in data)
{
var insertCmd = connection.CreateCommand();
insertCmd.CommandText = "INSERT INTO FsmData (SIID, Enable, Description) VALUES (@SIID, @Enable, @Description)";
insertCmd.CommandText = "INSERT INTO FsmData (SIID, Enable, Label, Type) VALUES (@SIID, @Enable, @Label, @Type)";
insertCmd.Parameters.AddWithValue("@SIID", item.SIID);
insertCmd.Parameters.AddWithValue("@Enable", item.Enable);
insertCmd.Parameters.AddWithValue("@Description", item.Description);
insertCmd.Parameters.AddWithValue("@Label", item.Label);
insertCmd.Parameters.AddWithValue("@Type", item.Type);
try
{
int result = insertCmd.ExecuteNonQuery();
@@ -123,6 +125,29 @@ namespace FAtoPA
}
}
public FSMData FSMDataHaveSIID(String SIID)
{
if (SIID!=null && SIID.Length > 0)
{
using (var conn = new SQLiteConnection(connectionString))
{
conn.Open();
var findCmd = conn.CreateCommand();
findCmd.CommandText = "SELECT FROM FsmData WHERE SIID = @SIID";
findCmd.Parameters.AddWithValue("@SIID", SIID);
using (var reader = findCmd.ExecuteReader())
{
if (reader.Read())
{
FSMData result = new FSMData(reader.GetString(0), reader.GetBoolean(1), reader.GetString(2), reader.GetString(3));
return result;
}
}
}
}
return null;
}
public bool RemoveFSMDatabySIID(String SIID)
{
using (var conn = new SQLiteConnection(connectionString))
@@ -222,6 +247,29 @@ namespace FAtoPA
}
}
public ModbusData ModbusDataHaveSIID(String SIID)
{
if (SIID != null && SIID.Length > 0)
{
using (var conn = new SQLiteConnection(connectionString))
{
conn.Open();
var findCmd = conn.CreateCommand();
findCmd.CommandText = "SELECT FROM ModbusData WHERE SIID = @SIID";
findCmd.Parameters.AddWithValue("@SIID", SIID);
using (var reader = findCmd.ExecuteReader())
{
if (reader.Read())
{
ModbusData result = new ModbusData(reader.GetString(0),(ushort) reader.GetInt16(1), reader.GetString(2));
return result;
}
}
}
}
return null;
}
public List<ModbusData> GetModbusDatas()
{
List<ModbusData> modbusDatas = new List<ModbusData>();
@@ -235,7 +283,7 @@ namespace FAtoPA
{
while (reader.Read())
{
ModbusData modbusdata = new ModbusData(reader.GetString(0), reader.GetFieldValue<UInt16>(1), reader.GetString(2));
ModbusData modbusdata = new ModbusData(reader.GetString(0), (ushort) reader.GetInt16(1), reader.GetString(2));
modbusDatas.Add(modbusdata);
}
}
@@ -306,6 +354,29 @@ namespace FAtoPA
return false;
}
public VXData VXDataHaveSIID(String SIID)
{
if (SIID != null && SIID.Length > 0)
{
using (var conn = new SQLiteConnection(connectionString))
{
conn.Open();
var findCmd = conn.CreateCommand();
findCmd.CommandText = "SELECT FROM VxTable WHERE SIID = @SIID";
findCmd.Parameters.AddWithValue("@SIID", SIID);
using (var reader = findCmd.ExecuteReader())
{
if (reader.Read())
{
VXData result = new VXData(reader.GetString(0), reader.GetByte(1), reader.GetByte(2), reader.GetString(3));
return result;
}
}
}
}
return null;
}
public List<VXData> GetVXDatas()
{
List<VXData> vxDatas = new List<VXData>();
@@ -319,7 +390,7 @@ namespace FAtoPA
{
while (reader.Read())
{
VXData vxdata = new VXData(reader.GetString(0), reader.GetByte(1), reader.GetByte(2));
VXData vxdata = new VXData(reader.GetString(0), reader.GetByte(1), reader.GetByte(2), reader.GetString(3));
vxDatas.Add(vxdata);
}
}
@@ -388,36 +459,89 @@ namespace FAtoPA
class FSMData : INotifyPropertyChanged
{
public String SIID { get; set; }
public Boolean Enable { get; set; }
public String Description { get; set; }
public String Value { get; set; }
public String LastUpdate { get; set; }
public FSMData(String siid, Boolean enable, String description)
private String _siid;
public String SIID
{
get => _siid;
set
{
if (_siid != value)
{
_siid = value;
OnPropertyChanged(nameof(SIID));
}
}
}
private Boolean _enable;
public Boolean Enable
{
get => _enable;
set
{
if (_enable != value)
{
_enable = value;
OnPropertyChanged(nameof(Enable));
}
}
}
private String _label;
public String Label {
get => _label;
set {
if (_label != value)
{
_label = value;
OnPropertyChanged(nameof(Label));
}
}
}
private String _type;
public String Type
{
get => _type;
set
{
if (_type != value)
{
_type = value;
OnPropertyChanged(nameof(Type));
}
}
}
private String _value;
public String Value {
get => _value;
set
{
if (_value != value)
{
_value = value;
OnPropertyChanged(nameof(Value));
}
}
}
private String _lastupdate;
public String LastUpdate {
get => _lastupdate;
set {
if (_lastupdate != value)
{
_lastupdate = value;
OnPropertyChanged(nameof(LastUpdate));
}
}
}
public FSMData(String siid, Boolean enable, String label, String type)
{
this.SIID = siid;
this.Enable = enable;
this.Description = description;
this.Label = label;
this.Type = type;
this.Value = "";
this.LastUpdate = "";
}
public FSMData(String siid)
{
this.SIID = siid;
this.Enable = true;
this.Description = "";
this.Value = "";
this.LastUpdate = "";
}
public FSMData(String siid, Boolean enable)
{
this.SIID = siid;
this.Enable = enable;
this.Description = "";
this.LastUpdate = "";
this.Value = "";
}
protected void OnPropertyChanged(string propertyName)
{
@@ -425,16 +549,77 @@ namespace FAtoPA
}
public event PropertyChangedEventHandler PropertyChanged;
}
class ModbusData
{
public String SIID { get; set; }
public UInt16 Register { get; set; }
public String Description { get; set; }
public String Value { get; set; }
public String LastUpdate { get; set; }
class ModbusData : INotifyPropertyChanged
{
private String _siid;
public String SIID
{
get => _siid;
set
{
if (_siid != value)
{
_siid = value;
OnPropertyChanged(nameof(SIID));
}
}
}
private UInt16 _register;
public UInt16 Register
{
get => _register;
set
{
if (_register != value)
{
_register = value;
OnPropertyChanged(nameof(Register));
}
}
}
private String _description;
public String Description
{
get => _description;
set
{
if (_description != value)
{
_description = value;
OnPropertyChanged(nameof(Description));
}
}
}
private String _value;
public String Value
{
get => _value;
set
{
if (_value != value)
{
_value = value;
OnPropertyChanged(nameof(Value));
}
}
}
private String _lastupdate;
public String LastUpdate
{
get => _lastupdate;
set
{
if (_lastupdate != value)
{
_lastupdate = value;
OnPropertyChanged(nameof(LastUpdate));
}
}
}
public ModbusData(String siid, UInt16 register, String description)
{
this.SIID = siid;
@@ -451,22 +636,100 @@ namespace FAtoPA
this.Value = "";
this.LastUpdate = "";
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
class VXData
class VXData : INotifyPropertyChanged
{
public String SIID { get; set; }
public Byte FrameID { get; set; }
public Byte CIN { get; set; }
public String Description { get; set; }
public String Value { get; set; }
public String LastUpdate { get; set; }
private String _siid;
public String SIID
{
get => _siid;
set
{
if (_siid != value)
{
_siid = value;
OnPropertyChanged(nameof(SIID));
}
}
}
private Byte _frameid;
public Byte FrameID
{
get => _frameid;
set
{
if (_frameid != value)
{
_frameid = value;
OnPropertyChanged(nameof(FrameID));
}
}
}
private Byte _cin;
public Byte CIN
{
get => _cin;
set
{
if (_cin != value)
{
_cin = value;
OnPropertyChanged(nameof(CIN));
}
}
}
private String _description;
public String Description
{
get => _description;
set
{
if (_description != value)
{
_description = value;
OnPropertyChanged(nameof(Description));
}
}
}
private String _value;
public String Value
{
get => _value;
set
{
if (_value != value)
{
_value = value;
OnPropertyChanged(nameof(Value));
}
}
}
private String _lastupdate;
public String LastUpdate
{
get => _lastupdate;
set
{
if (_lastupdate != value)
{
_lastupdate = value;
OnPropertyChanged(nameof(LastUpdate));
}
}
}
public VXData(String siid, Byte frameid, Byte cin, String Description)
{
this.SIID = siid;
this.FrameID = frameid;
this.CIN = cin;
this.Description=Description;
this.Description = Description;
this.Value = "";
this.LastUpdate = "";
}
@@ -475,9 +738,15 @@ namespace FAtoPA
this.SIID = siid;
this.FrameID = frameid;
this.CIN = cin;
this.Description = siid+" To "+ frameid + "." + cin;
this.Description = siid + " To " + frameid + "." + cin;
this.Value = "";
this.LastUpdate = "";
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}