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(); connection.Open();
var createtablecmd = connection.CreateCommand(); 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(); createtablecmd.ExecuteNonQuery();
Debug.WriteLine("CreateFSMTable success"); Debug.WriteLine("CreateFSMTable success");
return true; return true;
@@ -79,7 +79,8 @@ namespace FAtoPA
{ {
while (reader.Read()) 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); fsmDatas.Add(fsmData);
} }
} }
@@ -97,10 +98,11 @@ namespace FAtoPA
foreach (var item in data) foreach (var item in data)
{ {
var insertCmd = connection.CreateCommand(); 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("@SIID", item.SIID);
insertCmd.Parameters.AddWithValue("@Enable", item.Enable); 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 try
{ {
int result = insertCmd.ExecuteNonQuery(); 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) public bool RemoveFSMDatabySIID(String SIID)
{ {
using (var conn = new SQLiteConnection(connectionString)) 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() public List<ModbusData> GetModbusDatas()
{ {
List<ModbusData> modbusDatas = new List<ModbusData>(); List<ModbusData> modbusDatas = new List<ModbusData>();
@@ -235,7 +283,7 @@ namespace FAtoPA
{ {
while (reader.Read()) 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); modbusDatas.Add(modbusdata);
} }
} }
@@ -306,6 +354,29 @@ namespace FAtoPA
return false; 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() public List<VXData> GetVXDatas()
{ {
List<VXData> vxDatas = new List<VXData>(); List<VXData> vxDatas = new List<VXData>();
@@ -319,7 +390,7 @@ namespace FAtoPA
{ {
while (reader.Read()) 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); vxDatas.Add(vxdata);
} }
} }
@@ -388,36 +459,89 @@ namespace FAtoPA
class FSMData : INotifyPropertyChanged class FSMData : INotifyPropertyChanged
{ {
public String SIID { get; set; } private String _siid;
public Boolean Enable { get; set; } public String SIID
public String Description { get; set; } {
public String Value { get; set; } get => _siid;
public String LastUpdate { get; set; } set
public FSMData(String siid, Boolean enable, String description) {
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.SIID = siid;
this.Enable = enable; this.Enable = enable;
this.Description = description; this.Label = label;
this.Type = type;
this.Value = ""; this.Value = "";
this.LastUpdate = ""; 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) protected void OnPropertyChanged(string propertyName)
{ {
@@ -425,16 +549,77 @@ namespace FAtoPA
} }
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
} }
class ModbusData class ModbusData : INotifyPropertyChanged
{ {
public String SIID { get; set; } private String _siid;
public UInt16 Register { get; set; } public String SIID
public String Description { get; set; } {
get => _siid;
public String Value { get; set; } set
public String LastUpdate { get; 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) public ModbusData(String siid, UInt16 register, String description)
{ {
this.SIID = siid; this.SIID = siid;
@@ -451,22 +636,100 @@ namespace FAtoPA
this.Value = ""; this.Value = "";
this.LastUpdate = ""; 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; } private String _siid;
public Byte FrameID { get; set; } public String SIID
public Byte CIN { get; set; } {
public String Description { get; set; } get => _siid;
public String Value { get; set; } set
public String LastUpdate { get; 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) public VXData(String siid, Byte frameid, Byte cin, String Description)
{ {
this.SIID = siid; this.SIID = siid;
this.FrameID = frameid; this.FrameID = frameid;
this.CIN = cin; this.CIN = cin;
this.Description=Description; this.Description = Description;
this.Value = ""; this.Value = "";
this.LastUpdate = ""; this.LastUpdate = "";
} }
@@ -475,9 +738,15 @@ namespace FAtoPA
this.SIID = siid; this.SIID = siid;
this.FrameID = frameid; this.FrameID = frameid;
this.CIN = cin; this.CIN = cin;
this.Description = siid+" To "+ frameid + "." + cin; this.Description = siid + " To " + frameid + "." + cin;
this.Value = ""; this.Value = "";
this.LastUpdate = ""; this.LastUpdate = "";
} }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
} }
} }

View File

@@ -58,7 +58,7 @@
<ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Grid Grid.Column="0"> <Grid Visibility="Hidden" Grid.Column="0">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="*"/> <RowDefinition Height="*"/>
<RowDefinition Height="*"/> <RowDefinition Height="*"/>
@@ -66,7 +66,7 @@
<Label Content="NetGroup" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="0"/> <Label Content="NetGroup" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="0"/>
<ComboBox x:Name="netGroupNumber" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="1" Margin="5,0,5,5" ItemsSource="{Binding NetGroupList, IsAsync=True}" SelectedIndex="0"/> <ComboBox x:Name="netGroupNumber" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="1" Margin="5,0,5,5" ItemsSource="{Binding NetGroupList, IsAsync=True}" SelectedIndex="0"/>
</Grid> </Grid>
<Grid Grid.Column="1"> <Grid Visibility="Hidden" Grid.Column="1">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="*"/> <RowDefinition Height="*"/>
<RowDefinition Height="*"/> <RowDefinition Height="*"/>
@@ -74,7 +74,7 @@
<Label Content="NetNode" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="0"/> <Label Content="NetNode" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="0"/>
<ComboBox x:Name="netNodeNumber" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="1" Margin="5,0,5,5" ItemsSource="{Binding NetNodeList, IsAsync=True}" SelectedIndex="0"/> <ComboBox x:Name="netNodeNumber" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="1" Margin="5,0,5,5" ItemsSource="{Binding NetNodeList, IsAsync=True}" SelectedIndex="0"/>
</Grid> </Grid>
<Grid Grid.Column="2"> <Grid Visibility="Hidden" Grid.Column="2">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="*"/> <RowDefinition Height="*"/>
<RowDefinition Height="*"/> <RowDefinition Height="*"/>
@@ -82,7 +82,7 @@
<Label Content="SI Type" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="0"/> <Label Content="SI Type" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="0"/>
<ComboBox x:Name="siType" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="1" Margin="5,0,5,5" ItemsSource="{Binding SIType, IsAsync=True}" SelectedIndex="0"/> <ComboBox x:Name="siType" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="1" Margin="5,0,5,5" ItemsSource="{Binding SIType, IsAsync=True}" SelectedIndex="0"/>
</Grid> </Grid>
<Grid Grid.Column="3"> <Grid Visibility="Hidden" Grid.Column="3">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="*"/> <RowDefinition Height="*"/>
<RowDefinition Height="*"/> <RowDefinition Height="*"/>
@@ -90,7 +90,7 @@
<Label Content="SI Number" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="0"/> <Label Content="SI Number" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="0"/>
<ComboBox x:Name="siNumber" Text="1" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="1" Margin="5,0,5,5" ItemsSource="{Binding SINumberList, IsAsync=True}" SelectedIndex="0"/> <ComboBox x:Name="siNumber" Text="1" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="1" Margin="5,0,5,5" ItemsSource="{Binding SINumberList, IsAsync=True}" SelectedIndex="0"/>
</Grid> </Grid>
<Grid Grid.Column="4"> <Grid Visibility="Hidden" Grid.Column="4">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="*"/> <RowDefinition Height="*"/>
<RowDefinition Height="*"/> <RowDefinition Height="*"/>
@@ -105,7 +105,7 @@
<ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Button Grid.Column="0" Margin="5" x:Name="btnAddSIID" Content="Manual Add To Table" Click="btnAddSIID_Click"/> <Button Visibility="Hidden" Grid.Column="0" Margin="5" x:Name="btnAddSIID" Content="Manual Add To Table" Click="btnAddSIID_Click"/>
<Button Grid.Column="1" Margin="5" x:Name="btnDelSIID" Content="Remove From Table" Click="btnDelSIID_Click"/> <Button Grid.Column="1" Margin="5" x:Name="btnDelSIID" Content="Remove From Table" Click="btnDelSIID_Click"/>
<Button Grid.Column="2" Margin="5" x:Name="btnClearSIID" Content="Clear Table" Click="btnClearSIID_Click"/> <Button Grid.Column="2" Margin="5" x:Name="btnClearSIID" Content="Clear Table" Click="btnClearSIID_Click"/>
</Grid> </Grid>

View File

@@ -41,17 +41,23 @@ namespace FAtoPA.Net
// untuk values di Combobox Modbus Registers // untuk values di Combobox Modbus Registers
public ObservableCollection<int> ModbusRegisters { get; set; } public ObservableCollection<int> ModbusRegisters { get; set; }
// Untuk manual add SIID to FSM Table
public ObservableCollection<int> NetGroupList { get; set; } public ObservableCollection<int> NetGroupList { get; set; }
public ObservableCollection<int> NetNodeList { get; set; } public ObservableCollection<int> NetNodeList { get; set; }
public ObservableCollection<int> PNAList { get; set; } public ObservableCollection<int> PNAList { get; set; }
public ObservableCollection<int> SINumberList { get; set; } public ObservableCollection<int> SINumberList { get; set; }
public ObservableCollection<int> SISubList { get; set; } public ObservableCollection<int> SISubList { get; set; }
// Isi FSM Table
ObservableCollection<FSMData> FsmTableMember { get; set; } ObservableCollection<FSMData> FsmTableMember { get; set; }
// Isi Modbus Table
ObservableCollection<ModbusData> ModbusTableMember { get; set; } ObservableCollection<ModbusData> ModbusTableMember { get; set; }
// Isi VX Table
ObservableCollection<VXData> VXTableMember { get; set; } ObservableCollection<VXData> VXTableMember { get; set; }
public List<String> ConditionON;
public List<String> ConditionOFF;
FSMEvent fsmEvent; FSMEvent fsmEvent;
public MainWindow() public MainWindow()
{ {
@@ -100,6 +106,14 @@ namespace FAtoPA.Net
siType.ItemsSource = Enum.GetValues(typeof(SIType)).Cast<SIType>().ToList(); siType.ItemsSource = Enum.GetValues(typeof(SIType)).Cast<SIType>().ToList();
this.DataContext = this; this.DataContext = this;
//TODO : Add Condition for ON and OFF
ConditionON = new List<string>();
ConditionON.Add(SILogicalState.ACTIVATION.ToString());
ConditionON.Add(SILogicalState.FIRE.ToString());
ConditionOFF = new List<string>();
ConditionOFF.Add(SILogicalState.NORMAL.ToString());
} }
private void Window_Loaded(object sender, RoutedEventArgs e) private void Window_Loaded(object sender, RoutedEventArgs e)
@@ -142,7 +156,11 @@ namespace FAtoPA.Net
// Load Database // Load Database
database = new Database(); database = new Database();
database.GetFSMDatas().ForEach(f => FsmTableMember.Add(f)); database.GetFSMDatas().ForEach(f =>
{
FsmTableMember.Add(f);
FSMSIID.Add(f.SIID);
});
database.GetModbusDatas().ForEach(m => ModbusTableMember.Add(m)); database.GetModbusDatas().ForEach(m => ModbusTableMember.Add(m));
database.GetVXDatas().ForEach(v => VXTableMember.Add(v)); database.GetVXDatas().ForEach(v => VXTableMember.Add(v));
@@ -160,10 +178,12 @@ namespace FAtoPA.Net
ModbusRegisters.Clear(); ModbusRegisters.Clear();
for (int i = 0; i < config.Modbus_MaxRegister; i++) for (int i = 0; i < config.Modbus_MaxRegister; i++)
{ {
ModbusRegisters.Add(i); ModbusRegisters.Add(i);
} }
Debug.WriteLine($"Creating ModbusRegisters untuk Combobox, length={ModbusRegisters.Count}");
// Load Modbus Slave // Load Modbus Slave
modbusSlave = new ModbusSlave(new ModbusEvent(modbusstatusbar, ConnectedModbusClients, ConnectedModbusCount), config.Modbus_MaxRegister); modbusSlave = new ModbusSlave(new ModbusEvent(modbusstatusbar, ConnectedModbusClients, ConnectedModbusCount), config.Modbus_MaxRegister);
@@ -176,8 +196,8 @@ namespace FAtoPA.Net
fsm.AddListener(new FSMTableUpdater(FsmTableMember, DetectedSIID, DetectedSIIDCount)); fsm.AddListener(new FSMTableUpdater(FsmTableMember, DetectedSIID, DetectedSIIDCount));
fsm.AddListener(new ModbusTriggerFromFSM(FsmTableMember, ModbusTableMember, modbusSlave)); fsm.AddListener(new ModbusTriggerFromFSM(FsmTableMember, ModbusTableMember, modbusSlave, ConditionON, ConditionOFF));
fsm.AddListener(new VXTriggerFromFSM(FsmTableMember, VXTableMember, vx3k)); fsm.AddListener(new VXTriggerFromFSM(FsmTableMember, VXTableMember, vx3k, ConditionON, ConditionOFF));
} }
private void Timer1s_Tick(object sender, EventArgs e) private void Timer1s_Tick(object sender, EventArgs e)
@@ -778,13 +798,15 @@ namespace FAtoPA.Net
{ {
if (database != null) if (database != null)
{ {
FSMData f = new FSMData(selected.ToString()); //TODO Manual Add SIID, perlukah ?
if (database.AddFSMData(f))
{ //FSMData f = new FSMData(selected.ToString());
FSMTable.ItemsSource = database.GetFSMDatas(); //if (database.AddFSMData(f))
FSMSIID.Add(selected.ToString()); //{
} // FSMTable.ItemsSource = database.GetFSMDatas();
else MessageBox.Show("Failed to add to database"); // FSMSIID.Add(selected.ToString());
//}
//else MessageBox.Show("Failed to add to database");
} }
else MessageBox.Show("Database is null"); else MessageBox.Show("Database is null");
} }
@@ -795,19 +817,19 @@ namespace FAtoPA.Net
private void btnDelSIID_Click(object sender, RoutedEventArgs e) private void btnDelSIID_Click(object sender, RoutedEventArgs e)
{ {
int selectedindex = FSMTable.SelectedIndex; //int selectedindex = FSMTable.SelectedIndex;
if (selectedindex >= 0) //if (selectedindex >= 0)
{ //{
FSMData selected = (FSMData)FSMTable.Items[selectedindex]; // FSMData selected = (FSMData)FSMTable.Items[selectedindex];
MessageBoxResult result = MessageBox.Show("Delete SIID " + selected.ToString() + " ?", "Delete SIID", MessageBoxButton.YesNo); // MessageBoxResult result = MessageBox.Show("Delete SIID " + selected.ToString() + " ?", "Delete SIID", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes) // if (result == MessageBoxResult.Yes)
{ // {
FsmTableMember.Remove(selected); // FsmTableMember.Remove(selected);
database.RemoveFSMDatabySIID(selected.SIID); // database.RemoveFSMDatabySIID(selected.SIID);
// FSMSIID.Remove(selected.SIID);
} // }
} //}
else MessageBox.Show("Select a row in table to delete"); //else MessageBox.Show("Select a row in table to delete");
} }
@@ -819,7 +841,7 @@ namespace FAtoPA.Net
{ {
FsmTableMember.Clear(); FsmTableMember.Clear();
database.ClearFSMTable(); database.ClearFSMTable();
FSMSIID.Clear();
} }
} }
@@ -1060,19 +1082,20 @@ namespace FAtoPA.Net
switch (e.PropertyName) switch (e.PropertyName)
{ {
case "SIID": case "SIID":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto); e.Column.Width = DataGridLength.Auto;
break; break;
case "Enable": case "Enable":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto); e.Column.Width = DataGridLength.Auto;
break; break;
case "Description": case "Description":
e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star); e.Column.Width = DataGridLength.Auto;
break; break;
case "Value": case "Value":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto); e.Column.Width = DataGridLength.Auto;
break; break;
case "LastUpdate": case "LastUpdate":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto); e.Column.Width = DataGridLength.Auto;
break; break;
} }
} }
@@ -1083,19 +1106,19 @@ namespace FAtoPA.Net
switch (e.PropertyName) switch (e.PropertyName)
{ {
case "SIID": case "SIID":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto); e.Column.Width = DataGridLength.Auto;
break; break;
case "Register": case "Register":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto); e.Column.Width = DataGridLength.Auto;
break; break;
case "Description": case "Description":
e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star); e.Column.Width = DataGridLength.Auto;
break; break;
case "Value": case "Value":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto); e.Column.Width = DataGridLength.Auto;
break; break;
case "LastUpdate": case "LastUpdate":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto); e.Column.Width = DataGridLength.Auto;
break; break;
} }
} }
@@ -1106,22 +1129,22 @@ namespace FAtoPA.Net
switch (e.PropertyName) switch (e.PropertyName)
{ {
case "SIID": case "SIID":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto); e.Column.Width = DataGridLength.Auto;
break; break;
case "ID": case "ID":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto); e.Column.Width = DataGridLength.Auto;
break; break;
case "CIN": case "CIN":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto); e.Column.Width = DataGridLength.Auto;
break; break;
case "Description": case "Description":
e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star); e.Column.Width = DataGridLength.Auto;
break; break;
case "Value": case "Value":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto); e.Column.Width = DataGridLength.Auto;
break; break;
case "LastUpdate": case "LastUpdate":
e.Column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto); e.Column.Width = DataGridLength.Auto;
break; break;
} }
} }
@@ -1135,13 +1158,23 @@ namespace FAtoPA.Net
if (selected.Tag is NodeData) if (selected.Tag is NodeData)
{ {
NodeData data = (NodeData)selected.Tag; NodeData data = (NodeData)selected.Tag;
FSMData fSMData = new FSMData(data.SIID.ToString(), true, data.Description); FSMData fSMData = new FSMData(data.SIID.ToString(), true, data.Label, data.Description);
if (database.FSMDataHaveSIID(fSMData.SIID)!=null)
{
Debug.WriteLine($"database already have SIID={fSMData.SIID}");
} else
{
if (database.AddFSMData(fSMData)) if (database.AddFSMData(fSMData))
{ {
FsmTableMember.Add(fSMData); FsmTableMember.Add(fSMData);
database.AddFSMData(fSMData); FSMSIID.Add(fSMData.SIID);
Debug.WriteLine($"Added SIID={fSMData.SIID}, Label={fSMData.Label}, Type= {fSMData.Type} to database");
} }
else MessageBox.Show("Failed to add to database"); else MessageBox.Show($"Failed to add SIID={fSMData.SIID}, Label={fSMData.Label}, Type={fSMData.Type} to database");
}
} }
else MessageBox.Show("Selected SIID dont have NodeData"); else MessageBox.Show("Selected SIID dont have NodeData");
} else MessageBox.Show("No SIID Selected"); } else MessageBox.Show("No SIID Selected");
@@ -1191,7 +1224,8 @@ namespace FAtoPA.Net
private void refresh_connectedlist() private void refresh_connectedlist()
{ {
Application.Current.Dispatcher.Invoke(() =>
{
connectedlist.Items.Clear(); connectedlist.Items.Clear();
foreach (ModbusClientRecord client in ModbusSlave) foreach (ModbusClientRecord client in ModbusSlave)
{ {
@@ -1200,10 +1234,13 @@ namespace FAtoPA.Net
l.Margin = new Thickness(5, 0, 5, 0); l.Margin = new Thickness(5, 0, 5, 0);
l.TextWrapping = TextWrapping.Wrap; l.TextWrapping = TextWrapping.Wrap;
UpdateLabel(l, client); UpdateLabel(l, client);
connectedlist.Items.Add(l); connectedlist.Items.Add(l);
} }
if (connectedcount != null) connectedcount.Content = "Connected : " + connectedlist.Items.Count; if (connectedcount != null) connectedcount.Content = "Connected : " + connectedlist.Items.Count;
});
} }
void ModbusSlaveEvent.Log(string msg) void ModbusSlaveEvent.Log(string msg)
@@ -1242,7 +1279,6 @@ namespace FAtoPA.Net
private void UpdateLabel(TextBlock l, ModbusClientRecord client) private void UpdateLabel(TextBlock l, ModbusClientRecord client)
{ {
l.Text = client.remoteEP + " TX: " + client.TXBytes + " RX: " + client.RXBytes + "TXOK: " + client.TXResponse + " RXOK: " + client.RXValidRequest; l.Text = client.remoteEP + " TX: " + client.TXBytes + " RX: " + client.RXBytes + "TXOK: " + client.TXResponse + " RXOK: " + client.RXValidRequest;
} }
@@ -1276,16 +1312,17 @@ namespace FAtoPA.Net
public void StatisticUpdate(uint TXOK, uint RXOK, uint TXErr, uint RXerr, uint TXBytes, uint RXBytes) public void StatisticUpdate(uint TXOK, uint RXOK, uint TXErr, uint RXerr, uint TXBytes, uint RXBytes)
{ {
if (statusbar != null) //if (statusbar != null)
{ //{
statusbar.Dispatcher.Invoke(() =>
{ // statusbar.Dispatcher.Invoke(() =>
statusbar.Text = "FSM : TXOK: " + TXOK + " RXOK: " + RXOK + " TXErr: " + TXErr + " RXErr: " + RXerr + " TXBytes: " + TXBytes + " RXBytes: " + RXBytes;
}); // {
// statusbar.Text = "FSM : TXOK: " + TXOK + " RXOK: " + RXOK + " TXErr: " + TXErr + " RXErr: " + RXerr + " TXBytes: " + TXBytes + " RXBytes: " + RXBytes;
// });
} //}
} }
} }
@@ -1305,12 +1342,20 @@ namespace FAtoPA.Net
public void DiscoveredSIID(string SIID, NodeData type) public void DiscoveredSIID(string SIID, NodeData type)
{ {
Debug.WriteLine("Discovered SIID : " + SIID + " Type : " + type.Description); Debug.WriteLine($"Discovered SIID={SIID} Label={type.Label} Type={type.Description}");
if (type.Label != null && type.Label.Length > 0)
{
if (type.Description != null && type.Description.Length > 0)
{
// yang punya Label dan Description saja yang masuk ke Listbox dan dihitung
Application.Current.Dispatcher.Invoke(() => Application.Current.Dispatcher.Invoke(() =>
{ {
listbox.Items.Add(new Label() { Content = $"{SIID} : {type.Label} : {type.Description}", Tag = type }); listbox.Items.Add(new Label() { Content = $"{SIID} : {type.Label} : {type.Description}", Tag = type });
countlabel.Content = "Count : " + listbox.Items.Count; countlabel.Content = "Count : " + listbox.Items.Count;
}); });
}
}
} }
@@ -1318,8 +1363,10 @@ namespace FAtoPA.Net
public void NewState(string SIID, NodeState previous, NodeState current) public void NewState(string SIID, NodeState previous, NodeState current)
{ {
Debug.WriteLine("New State : " + SIID + " Previous : " + previous?.LogicalState + " Current : " + current.LogicalState); Debug.WriteLine("New State : " + SIID + " Previous : " + previous?.LogicalState + " Current : " + current.LogicalState);
if (data != null) if (data != null)
{ {
// update yang ada di FsmTable saja
foreach (var dd in data) foreach (var dd in data)
{ {
if (dd.SIID.Equals(SIID)) if (dd.SIID.Equals(SIID))
@@ -1327,6 +1374,7 @@ namespace FAtoPA.Net
dd.LastUpdate = DateTime.Now.ToString(); dd.LastUpdate = DateTime.Now.ToString();
dd.Value = current.LogicalState ?? "Unknown"; dd.Value = current.LogicalState ?? "Unknown";
Debug.WriteLine($"Changing row in FSM Table for SIID={SIID} Value={dd.Value}"); Debug.WriteLine($"Changing row in FSM Table for SIID={SIID} Value={dd.Value}");
return; return;
} }
} }
@@ -1344,11 +1392,15 @@ namespace FAtoPA.Net
ObservableCollection<FSMData> source; ObservableCollection<FSMData> source;
ObservableCollection<ModbusData> data; ObservableCollection<ModbusData> data;
ModbusSlave slave; ModbusSlave slave;
public ModbusTriggerFromFSM(ObservableCollection<FSMData> source, ObservableCollection<ModbusData> data, ModbusSlave slave) List<String> ConditionON;
List<String> ConditionOFF;
public ModbusTriggerFromFSM(ObservableCollection<FSMData> source, ObservableCollection<ModbusData> data, ModbusSlave slave, List<string> conditionON, List<string> conditionOFF)
{ {
this.source = source; this.source = source;
this.data = data; this.data = data;
this.slave = slave; this.slave = slave;
ConditionON = conditionON;
ConditionOFF = conditionOFF;
} }
public void DiscoveredSIID(string SIID, NodeData type) public void DiscoveredSIID(string SIID, NodeData type)
{ {
@@ -1358,33 +1410,71 @@ namespace FAtoPA.Net
public void NewState(string SIID, NodeState previous, NodeState current) public void NewState(string SIID, NodeState previous, NodeState current)
{ {
//FSMData src = source.First(d => d.SIID == SIID);
//ModbusData dt = data.First(d => d.SIID == SIID);
//if (src != null && dt != null)
//{
// if (src.Enable)
// {
// if (dt.Register >= 0)
// {
// dt.LastUpdate = DateTime.Now.ToString();
// dt.Value = current.LogicalState ?? "Unknown";
// if (slave != null)
// {
// switch (current.LogicalState)
// {
// case "ON":
// slave.SetRegister(dt.Register, 1);
// break;
// case "NORMAL":
// case "OFF":
// slave.SetRegister(dt.Register, 0);
// break;
// }
// }
// }
// } FSMData src = null;
//} ModbusData dt = null;
foreach(var x in source)
{
if (x.SIID.Equals(SIID))
{
src = x;
break;
}
}
foreach (var x in data)
{
if (x.SIID.Equals(SIID))
{
dt = x;
break;
}
}
if (src != null && dt != null)
{
if (src.Enable)
{
if (dt.Register >= 0)
{
if (slave != null)
{
if (ConditionON != null && ConditionOFF != null)
{
if (ConditionON.Contains(current.LogicalState))
{
Debug.WriteLine($"NewState for SIID={SIID} State={current.LogicalState} is ConditionON");
slave.SetRegister(dt.Register, 1);
dt.Value = "ON";
}
else if (ConditionOFF.Contains(current.LogicalState))
{
Debug.WriteLine($"NewState for SIID={SIID} State={current.LogicalState} is ConditionOFF");
slave.SetRegister(dt.Register, 0);
dt.Value = "OFF";
}
else
{
Debug.WriteLine($"NewState for SIID={SIID} in Modbus is ignored because Condition ON/OFF is not met");
dt.Value = "RULE NOT AVAILABLE";
}
}
else
{
Debug.WriteLine($"Condition ON/OFF for SIID={SIID} in Modbus is not set");
dt.Value = "RULE NOT SET";
}
}
else
{
Debug.WriteLine($"Not connected to Modbus Slave, NewState for SIID={SIID} in Modbus is ignored");
dt.Value = "NO MODBUS";
}
dt.LastUpdate = DateTime.Now.ToString();
} else Debug.WriteLine($"NewState for SIID={SIID} in Modbus is ignored because Register is not set");
} else Debug.WriteLine($"NewState for SIID={SIID} in Modbus is ignored because FSMData is Disabled");
} else Debug.WriteLine($"NewState for SIID={SIID} in Modbus is ignored because not registered in ModbusTable");
} }
} }
@@ -1394,11 +1484,15 @@ namespace FAtoPA.Net
ObservableCollection<VXData> data; ObservableCollection<VXData> data;
ObservableCollection<FSMData> source; ObservableCollection<FSMData> source;
VX3K vx; VX3K vx;
public VXTriggerFromFSM(ObservableCollection<FSMData> source, ObservableCollection<VXData> data, VX3K vx) List<String> ConditionON;
List<String> ConditionOFF;
public VXTriggerFromFSM(ObservableCollection<FSMData> source, ObservableCollection<VXData> data, VX3K vx, List<string> conditionON, List<string> conditionOFF)
{ {
this.source = source; this.source = source;
this.data = data; this.data = data;
this.vx = vx; this.vx = vx;
ConditionON = conditionON;
ConditionOFF = conditionOFF;
} }
public void DiscoveredSIID(string SIID, NodeData type) public void DiscoveredSIID(string SIID, NodeData type)
{ {
@@ -1408,31 +1502,66 @@ namespace FAtoPA.Net
public void NewState(string SIID, NodeState previous, NodeState current) public void NewState(string SIID, NodeState previous, NodeState current)
{ {
//FSMData src = source.First(d => d.SIID == SIID); FSMData src = null;
//VXData dt = data.First(d => d.SIID == SIID); VXData dt = null;
//if (src != null && dt != null) foreach (var x in source)
//{ {
// if (src.Enable) if (x.SIID.Equals(SIID))
// { {
// dt.LastUpdate = DateTime.Now.ToString(); src = x;
// dt.Value = current.LogicalState ?? "Unknown"; break;
// if (vx != null && vx.IsConnected()) }
// { }
// switch (current.LogicalState) foreach (var x in data)
// { {
// case "ON": if (x.SIID.Equals(SIID))
// vx.Virtual_Contact_Input(dt.FrameID, dt.CIN, true); {
// break; dt = x;
// case "NORMAL": break;
// case "OFF": }
// vx.Virtual_Contact_Input(dt.FrameID, dt.CIN, false); }
// break;
// }
// } if (src != null && dt != null)
{
if (src.Enable)
{
if (vx != null && vx.IsConnected())
{
if (ConditionON != null && ConditionOFF != null)
{
if (ConditionON.Contains(current.LogicalState))
{
Debug.WriteLine($"NewState for SIID={SIID} State={current.LogicalState} is ConditionON");
vx.Virtual_Contact_Input(dt.FrameID, dt.CIN, true);
dt.Value = "ON";
}
else if (ConditionOFF.Contains(current.LogicalState))
{
Debug.WriteLine($"NewState for SIID={SIID} State={current.LogicalState} is ConditionOFF");
vx.Virtual_Contact_Input(dt.FrameID, dt.CIN, false);
dt.Value = "OFF";
}
else
{
Debug.WriteLine($"NewState for SIID={SIID} in VX3K is ignored because Condition ON/OFF is not met");
dt.Value = "RULE NOT AVAILABLE";
}
}
else
{
Debug.WriteLine($"Condition ON/OFF for SIID={SIID} in VX3K is not set");
dt.Value = "RULE NOT SET";
}
}
else
{
Debug.WriteLine($"Not connected to VX3K, NewState for SIID={SIID} in VX3K is ignored");
dt.Value = "NO VX";
}
dt.LastUpdate = DateTime.Now.ToString();
} else Debug.WriteLine($"NewState for SIID={SIID} in VX3K is ignored because FSMData is Disabled");
} else Debug.WriteLine($"NewState for SIID={SIID} in VX3K is ignored because not registered in VXTable");
// }
//}
} }
} }
} }

View File

@@ -301,6 +301,7 @@ namespace FAtoPA
client = null; client = null;
} }
} }
catch (Exception e) catch (Exception e)
{ {
@@ -309,8 +310,13 @@ namespace FAtoPA
if (key != null && client != null) if (key != null && client != null)
{ {
ModbusClientRecord prev = null;
if (_event!=null) _event.Log("ModbusSlave: New Connection from " + key); if (_event!=null) _event.Log("ModbusSlave: New Connection from " + key);
ModbusClientRecord prev = clientmap[key]; if (clientmap.ContainsKey(key))
{
prev = clientmap[key];
}
if (prev != null) if (prev != null)
{ {
@@ -354,9 +360,14 @@ namespace FAtoPA
// 2 byte Register Count, index [10,11] // 2 byte Register Count, index [10,11]
byte[] cmd = new byte[12]; byte[] cmd = new byte[12];
try { try {
int readcount = await stream.ReadAsync(new byte[12], 0, 12); int readcount = await stream.ReadAsync(cmd, 0, 12);
if (readcount >= 12) if (readcount >= 12)
{ {
Debug.WriteLine($"Read from {key}, size={readcount}");
String cmdString = BitConverter.ToString(cmd);
Debug.WriteLine($"cmd={cmdString}");
client.RXBytes += (uint)readcount; client.RXBytes += (uint)readcount;
UInt16 transactionID = GetTransactionID(cmd); UInt16 transactionID = GetTransactionID(cmd);
UInt16 protocolID = GetProtocolID(cmd); UInt16 protocolID = GetProtocolID(cmd);
@@ -364,7 +375,9 @@ namespace FAtoPA
Byte unitID = GetUnitID(cmd); Byte unitID = GetUnitID(cmd);
Byte functionCode = GetFunctionCode(cmd); Byte functionCode = GetFunctionCode(cmd);
byte[] payload = GetModbusPayload(cmd); byte[] payload = GetModbusPayload(cmd);
Debug.WriteLine($"TransactionID={transactionID}, ProtocolID={protocolID}, Length={length}, UnitID={unitID}, FunctionCode={functionCode}, Payload={payload.Length}");
String payloadstring = BitConverter.ToString(payload);
Debug.WriteLine($"Payload={payloadstring}");
if (protocolID == 0 && length == payload.Length + 2 && unitID == 1) if (protocolID == 0 && length == payload.Length + 2 && unitID == 1)
{ {
if (functionCode == 3) if (functionCode == 3)
@@ -483,6 +496,8 @@ namespace FAtoPA
client.TXResponse++; client.TXResponse++;
} }
} }
else if (readcount == 0) break; // connection closed
} }
catch (Exception e) catch (Exception e)
{ {