This commit is contained in:
backuppc
2025-12-17 14:54:33 +09:00
parent eb0e08d290
commit 1f37871336
22 changed files with 1218 additions and 884 deletions

View File

@@ -23,6 +23,7 @@ namespace Project
DateTime agvsendstarttime = DateTime.Now;
DateTime lastXbeStatusSendTime = DateTime.Now;
DateTime lastBmsQueryTime = DateTime.Now;
object connectobj = new object();
void sm_SPS(object sender, EventArgs e)
{
@@ -36,41 +37,54 @@ namespace Project
// ========== 1. 장치 연결 관리 ==========
// AGV 연결
ConnectSerialPort(PUB.AGV, PUB.setting.Port_AGV, PUB.setting.Baud_AGV,
eVarTime.LastConn_AGV, eVarTime.LastConnTry_AGV, eVarTime.LastRecv_AGV);
lock (connectobj)
{
ConnectSerialPort(PUB.AGV, PUB.setting.Port_AGV, PUB.setting.Baud_AGV,
eVarTime.LastConn_AGV, eVarTime.LastConnTry_AGV, eVarTime.LastRecv_AGV);
}
// XBee 연결
ConnectSerialPort(PUB.XBE, PUB.setting.Port_XBE, PUB.setting.Baud_XBE,
eVarTime.LastConn_XBE, eVarTime.LastConnTry_XBE, eVarTime.LastRecv_XBE);
lock (connectobj)
{
ConnectSerialPort(PUB.XBE, PUB.setting.Port_XBE, PUB.setting.Baud_XBE,
eVarTime.LastConn_XBE, eVarTime.LastConnTry_XBE, eVarTime.LastRecv_XBE);
}
// BMS 연결
if (PUB.BMS.IsOpen == false)
lock (connectobj)
{
var ts = VAR.TIME.RUN(eVarTime.LastConn_BAT);
if (ts.TotalSeconds > 3)
if (PUB.BMS.IsOpen == false)
{
PUB.log.Add($"BMS 연결 시도: {PUB.setting.Port_BAT}");
PUB.BMS.PortName = PUB.setting.Port_BAT;
if (PUB.BMS.Open())
PUB.log.AddI($"BMS 연결 완료({PUB.setting.Port_BAT})");
var ts = VAR.TIME.RUN(eVarTime.LastConn_BAT);
if (ts.TotalSeconds > 3)
{
PUB.log.Add($"BMS 연결 시도: {PUB.setting.Port_BAT}");
PUB.BMS.PortName = PUB.setting.Port_BAT;
if (PUB.BMS.Open())
PUB.log.AddI($"BMS 연결 완료({PUB.setting.Port_BAT})");
VAR.TIME.Update(eVarTime.LastConn_BAT);
VAR.TIME.Update(eVarTime.LastConnTry_BAT);
VAR.TIME.Update(eVarTime.LastConn_BAT);
VAR.TIME.Update(eVarTime.LastConnTry_BAT);
}
}
}
else if (PUB.BMS.IsValid == false)
{
var ts = VAR.TIME.RUN(eVarTime.LastConnTry_BAT);
if (ts.TotalSeconds > (PUB.setting.interval_bms * 2.5))
else if (PUB.BMS.IsValid == false)
{
this.BeginInvoke(new Action(() => {
PUB.log.Add("BMS 자동 연결 해제 (응답 없음)");
PUB.BMS.Close();
}));
VAR.TIME.Set(eVarTime.LastConn_BAT, DateTime.Now.AddSeconds(5));
var ts = VAR.TIME.RUN(eVarTime.LastConnTry_BAT);
if (ts.TotalSeconds > (PUB.setting.interval_bms * 2.5))
{
this.BeginInvoke(new Action(() =>
{
PUB.log.Add("BMS 자동 연결 해제 (응답 없음)");
PUB.BMS.Close();
}));
VAR.TIME.Set(eVarTime.LastConn_BAT, DateTime.Now.AddSeconds(5));
}
}
}
// ========== 2. XBee 상태 전송 ==========
if (PUB.XBE != null && PUB.XBE.IsOpen)
{
@@ -133,7 +147,7 @@ namespace Project
/// <summary>
/// 시리얼 포트 연결 (arDev.arRS232)
/// </summary>
bool ConnectSerialPort(arDev.arRS232 dev, string port, int baud, eVarTime conn, eVarTime conntry, eVarTime recvtime)
bool ConnectSerialPort(arDev.ISerialComm dev, string port, int baud, eVarTime conn, eVarTime conntry, eVarTime recvtime)
{
if (port.isEmpty()) return false;
@@ -151,7 +165,8 @@ namespace Project
PUB.log.Add($"Connect to {port}:{baud}");
if (dev.Open())
{
PUB.log.Add(port, $"[AGV:{port}:{baud}] 연결 완료");
VAR.TIME[recvtime] = DateTime.Now; //값을 수신한것처럼한다
PUB.log.Add(port, $"[{port}:{baud}] 연결 완료");
}
else
{
@@ -163,7 +178,7 @@ namespace Project
}
else
{
var errmessage = dev.errorMessage;
var errmessage = dev.ErrorMessage;
PUB.log.AddE($"[AGV:{port}:{baud}] {errmessage}");
}
}
@@ -177,12 +192,14 @@ namespace Project
}
}
else if (dev.PortName.Equals(port) == false)
{
this.BeginInvoke(new Action(() => {
{
this.BeginInvoke(new Action(() =>
{
PUB.log.Add(port, $"포트 변경({dev.PortName}->{port})으로 연결 종료");
VAR.TIME.Set(conntry, DateTime.Now);
dev.Close();
}));
VAR.TIME.Update(conntry);
}
else if (dev.IsOpen)
@@ -191,8 +208,9 @@ namespace Project
var tsRecv = VAR.TIME.RUN(recvtime);
var tsConn = VAR.TIME.RUN(conntry);
if (tsRecv.TotalSeconds > 10 && tsConn.TotalSeconds > 5)
{
this.BeginInvoke(new Action(() => {
{
this.BeginInvoke(new Action(() =>
{
PUB.log.Add($"{port} 자동 연결 해제 (응답 없음)");
dev.Close();
}));
@@ -202,49 +220,7 @@ namespace Project
return true;
}
/// <summary>
/// 시리얼 포트 연결 (Device.Xbee)
/// </summary>
void ConnectSerialPort(Device.Xbee dev, string port, int baud, eVarTime conn, eVarTime conntry, eVarTime recvtime)
{
if (dev.IsOpen == false && port.isEmpty() == false)
{
var tsPLC = VAR.TIME.RUN(conntry);
if (tsPLC.TotalSeconds > 5)
{
VAR.TIME.Update(conntry);
try
{
VAR.TIME.Update(recvtime);
dev.PortName = port;
dev.BaudRate = baud;
if (dev.Open())
{
PUB.log.Add(port, $"[XBEE:{port}:{baud}] 연결 완료");
}
else
{
var errmessage = dev.errorMessage;
PUB.log.AddE($"[XBEE:{port}:{baud}] {errmessage}");
}
VAR.TIME.Update(conn);
VAR.TIME.Update(conntry);
}
catch (Exception ex)
{
PUB.log.AddE(ex.Message);
}
}
}
else if (dev.PortName.Equals(port) == false)
{
this.BeginInvoke(new Action(() => {
PUB.log.Add(port, $"포트 변경({dev.PortName}->{port})으로 연결 종료");
dev.Close();
}));
VAR.TIME[(int)conntry] = DateTime.Now;
}
}
}
}