From 35df73fd29d158b5ca2520ab6ca0ae6c1328a3b2 Mon Sep 17 00:00:00 2001 From: backuppc Date: Tue, 23 Dec 2025 13:07:01 +0900 Subject: [PATCH] =?UTF-8?q?xbee=20=EA=B0=92=20=EC=84=A4=EC=A0=95=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Project/Dialog/fXbeeSetting.Designer.cs | 158 +++++++++--------- Cs_HMI/Project/Dialog/fXbeeSetting.cs | 130 ++++++++++++-- Cs_HMI/Project/Dialog/fXbeeSetting.resx | 3 + Cs_HMI/Project/StateMachine/_SPS.cs | 7 +- Cs_HMI/SubProject/CommData/Enum.cs | 2 +- 5 files changed, 209 insertions(+), 91 deletions(-) diff --git a/Cs_HMI/Project/Dialog/fXbeeSetting.Designer.cs b/Cs_HMI/Project/Dialog/fXbeeSetting.Designer.cs index 212becd..003e5ca 100644 --- a/Cs_HMI/Project/Dialog/fXbeeSetting.Designer.cs +++ b/Cs_HMI/Project/Dialog/fXbeeSetting.Designer.cs @@ -28,19 +28,21 @@ /// private void InitializeComponent() { + this.components = new System.ComponentModel.Container(); this.button1 = new System.Windows.Forms.Button(); this.tbBaud = new System.Windows.Forms.TextBox(); this.tbPortName = new System.Windows.Forms.ComboBox(); this.panel1 = new System.Windows.Forms.Panel(); - this.panel2 = new System.Windows.Forms.Panel(); - this.rtXbee = new arCtl.LogTextBox(); - this.tbpanid = new System.Windows.Forms.TextBox(); - this.btpand = new System.Windows.Forms.Button(); - this.btch = new System.Windows.Forms.Button(); - this.tbch = new System.Windows.Forms.TextBox(); + this.button2 = new System.Windows.Forms.Button(); this.btmy = new System.Windows.Forms.Button(); this.tbmy = new System.Windows.Forms.TextBox(); - this.button2 = new System.Windows.Forms.Button(); + this.btch = new System.Windows.Forms.Button(); + this.tbch = new System.Windows.Forms.TextBox(); + this.btpand = new System.Windows.Forms.Button(); + this.tbpanid = new System.Windows.Forms.TextBox(); + this.panel2 = new System.Windows.Forms.Panel(); + this.rtXbee = new arCtl.LogTextBox(); + this.serialPort1 = new System.IO.Ports.SerialPort(this.components); this.panel1.SuspendLayout(); this.panel2.SuspendLayout(); this.SuspendLayout(); @@ -92,6 +94,76 @@ this.panel1.Size = new System.Drawing.Size(580, 118); this.panel1.TabIndex = 4; // + // button2 + // + this.button2.Location = new System.Drawing.Point(293, 15); + this.button2.Name = "button2"; + this.button2.Size = new System.Drawing.Size(75, 89); + this.button2.TabIndex = 10; + this.button2.Text = "Read Setting"; + this.button2.UseVisualStyleBackColor = true; + this.button2.Click += new System.EventHandler(this.button2_Click); + // + // btmy + // + this.btmy.Location = new System.Drawing.Point(480, 78); + this.btmy.Name = "btmy"; + this.btmy.Size = new System.Drawing.Size(86, 26); + this.btmy.TabIndex = 9; + this.btmy.Text = "My"; + this.btmy.UseVisualStyleBackColor = true; + this.btmy.Click += new System.EventHandler(this.btmy_Click); + // + // tbmy + // + this.tbmy.Font = new System.Drawing.Font("굴림", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129))); + this.tbmy.Location = new System.Drawing.Point(374, 78); + this.tbmy.Name = "tbmy"; + this.tbmy.Size = new System.Drawing.Size(100, 26); + this.tbmy.TabIndex = 8; + this.tbmy.Text = "9600"; + this.tbmy.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; + // + // btch + // + this.btch.Location = new System.Drawing.Point(480, 47); + this.btch.Name = "btch"; + this.btch.Size = new System.Drawing.Size(86, 26); + this.btch.TabIndex = 7; + this.btch.Text = "Channel"; + this.btch.UseVisualStyleBackColor = true; + this.btch.Click += new System.EventHandler(this.btch_Click); + // + // tbch + // + this.tbch.Font = new System.Drawing.Font("굴림", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129))); + this.tbch.Location = new System.Drawing.Point(374, 47); + this.tbch.Name = "tbch"; + this.tbch.Size = new System.Drawing.Size(100, 26); + this.tbch.TabIndex = 6; + this.tbch.Text = "9600"; + this.tbch.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; + // + // btpand + // + this.btpand.Location = new System.Drawing.Point(480, 15); + this.btpand.Name = "btpand"; + this.btpand.Size = new System.Drawing.Size(86, 26); + this.btpand.TabIndex = 5; + this.btpand.Text = "PanID"; + this.btpand.UseVisualStyleBackColor = true; + this.btpand.Click += new System.EventHandler(this.btpand_Click); + // + // tbpanid + // + this.tbpanid.Font = new System.Drawing.Font("굴림", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129))); + this.tbpanid.Location = new System.Drawing.Point(374, 15); + this.tbpanid.Name = "tbpanid"; + this.tbpanid.Size = new System.Drawing.Size(100, 26); + this.tbpanid.TabIndex = 4; + this.tbpanid.Text = "9600"; + this.tbpanid.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; + // // panel2 // this.panel2.Controls.Add(this.rtXbee); @@ -121,75 +193,10 @@ this.rtXbee.TabIndex = 2; this.rtXbee.Text = ""; // - // tbpanid + // serialPort1 // - this.tbpanid.Font = new System.Drawing.Font("굴림", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129))); - this.tbpanid.Location = new System.Drawing.Point(374, 15); - this.tbpanid.Name = "tbpanid"; - this.tbpanid.Size = new System.Drawing.Size(100, 26); - this.tbpanid.TabIndex = 4; - this.tbpanid.Text = "9600"; - this.tbpanid.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; - // - // btpand - // - this.btpand.Location = new System.Drawing.Point(480, 15); - this.btpand.Name = "btpand"; - this.btpand.Size = new System.Drawing.Size(86, 26); - this.btpand.TabIndex = 5; - this.btpand.Text = "PanID"; - this.btpand.UseVisualStyleBackColor = true; - this.btpand.Click += new System.EventHandler(this.btpand_Click); - // - // btch - // - this.btch.Location = new System.Drawing.Point(480, 47); - this.btch.Name = "btch"; - this.btch.Size = new System.Drawing.Size(86, 26); - this.btch.TabIndex = 7; - this.btch.Text = "Channel"; - this.btch.UseVisualStyleBackColor = true; - this.btch.Click += new System.EventHandler(this.btch_Click); - // - // tbch - // - this.tbch.Font = new System.Drawing.Font("굴림", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129))); - this.tbch.Location = new System.Drawing.Point(374, 47); - this.tbch.Name = "tbch"; - this.tbch.Size = new System.Drawing.Size(100, 26); - this.tbch.TabIndex = 6; - this.tbch.Text = "9600"; - this.tbch.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; - // - // btmy - // - this.btmy.Location = new System.Drawing.Point(480, 78); - this.btmy.Name = "btmy"; - this.btmy.Size = new System.Drawing.Size(86, 26); - this.btmy.TabIndex = 9; - this.btmy.Text = "My"; - this.btmy.UseVisualStyleBackColor = true; - this.btmy.Click += new System.EventHandler(this.btmy_Click); - // - // tbmy - // - this.tbmy.Font = new System.Drawing.Font("굴림", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129))); - this.tbmy.Location = new System.Drawing.Point(374, 78); - this.tbmy.Name = "tbmy"; - this.tbmy.Size = new System.Drawing.Size(100, 26); - this.tbmy.TabIndex = 8; - this.tbmy.Text = "9600"; - this.tbmy.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; - // - // button2 - // - this.button2.Location = new System.Drawing.Point(293, 15); - this.button2.Name = "button2"; - this.button2.Size = new System.Drawing.Size(75, 89); - this.button2.TabIndex = 10; - this.button2.Text = "Read Setting"; - this.button2.UseVisualStyleBackColor = true; - this.button2.Click += new System.EventHandler(this.button2_Click); + this.serialPort1.ReadTimeout = 1000; + this.serialPort1.WriteTimeout = 1000; // // fXbeeSetting // @@ -227,5 +234,6 @@ private System.Windows.Forms.Button btch; private System.Windows.Forms.TextBox tbch; private System.Windows.Forms.Button button2; + private System.IO.Ports.SerialPort serialPort1; } } \ No newline at end of file diff --git a/Cs_HMI/Project/Dialog/fXbeeSetting.cs b/Cs_HMI/Project/Dialog/fXbeeSetting.cs index e50ac45..217cf1f 100644 --- a/Cs_HMI/Project/Dialog/fXbeeSetting.cs +++ b/Cs_HMI/Project/Dialog/fXbeeSetting.cs @@ -7,7 +7,8 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; - +using AR; +using COMM; namespace Project.Dialog { public partial class fXbeeSetting : Form @@ -15,7 +16,9 @@ namespace Project.Dialog public fXbeeSetting() { InitializeComponent(); - PUB.logxbee.RaiseMsg += Log_RaiseMsgcal; + VAR.BOOL[eVarBool.DISABLE_AUTOCONN_XBEE] = true; + PUB.XBE.Close(); + this.serialPort1.DataReceived += SerialPort1_DataReceived; } private void fXbeeSetting_Load(object sender, EventArgs e) { @@ -27,11 +30,16 @@ namespace Project.Dialog this.tbPortName.Text = PUB.setting.Port_XBE; this.tbBaud.Text = PUB.setting.Baud_XBE.ToString(); + } - private void Log_RaiseMsgcal(DateTime LogTime, string TypeStr, string Message) + void showlog(string Message) { - showlog(rtXbee, LogTime, TypeStr, Message); + if (rtXbee.Visible) + { + rtXbee.AddMsg(DateTime.Now, "NORMAL", Message); + } } + void showlog(arCtl.LogTextBox rtRx, DateTime LogTime, string TypeStr, string Message) { if (rtRx.Visible) @@ -41,27 +49,96 @@ namespace Project.Dialog } private void button1_Click(object sender, EventArgs e) { - if (PUB.XBE.IsOpen) - PUB.XBE.Close(); + if (this.serialPort1.IsOpen) + { + serialPort1.Close(); + showlog("closed"); + } else { - PUB.XBE.PortName = tbPortName.Text; - PUB.XBE.Open(); + serialPort1.PortName = tbPortName.Text; + serialPort1.BaudRate = int.Parse(tbBaud.Text); + serialPort1.Open(); + showlog("open"); } } private void fXbeeSetting_FormClosed(object sender, FormClosedEventArgs e) { - PUB.logxbee.RaiseMsg -= Log_RaiseMsgcal; + VAR.BOOL[eVarBool.DISABLE_AUTOCONN_XBEE] = false; } + private volatile bool isCommandExecuting = false; + + private void SerialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) + { + if (isCommandExecuting) return; + + try + { + string data = serialPort1.ReadExisting(); + var hexdata = System.Text.Encoding.Default.GetBytes(data); + var hexstr = string.Join(" ", hexdata.Select(t => t.ToString("X2"))); + if (!string.IsNullOrEmpty(data)) + { + this.BeginInvoke(new Action(() => + { + showlog($"RxAsync: {hexstr}"); + })); + } + } + catch { } + } + + private string Cmd(string cmd, int timeout = 1000) + { + isCommandExecuting = true; + try + { + if (!serialPort1.IsOpen) return "Error: Port Closed"; + + serialPort1.DiscardInBuffer(); + serialPort1.Write(cmd); + + System.Threading.Thread.Sleep(20); + serialPort1.ReadTimeout = timeout; + string res = serialPort1.ReadTo("\r"); + System.Threading.Thread.Sleep(20); + showlog($"Tx:{cmd.Trim()}, Rx:{res}"); + + //명령수신호 10ms 대기후 다음 명령을 전송 + System.Threading.Thread.Sleep(20); + + return res; + } + catch (Exception ex) + { + showlog($"Err: {ex.Message}"); + return "Error"; + } + finally + { + isCommandExecuting = false; + } + } private void btpand_Click(object sender, EventArgs e) { + //각명령마다 회신을 확인하고 다음명령을 실행해야함 + //명령수신호 10ms 대기후 다음 명령을 전송 + //명령을 설정하면 응답은 OK\d 형태로 입력된다. var cmds = new string[] { "+++", $"ATID{tbpanid.Text}\r" , - $"ATCN"}; + $"ATCN\r"}; + foreach (var cmd in cmds) + { + if(!Cmd(cmd).Contains("OK")) + { + showlog("FAIL"); + break; + } + } } private void btch_Click(object sender, EventArgs e) @@ -69,7 +146,15 @@ namespace Project.Dialog var cmds = new string[] { "+++", $"ATCH{tbch.Text}\r" , - $"ATCN"}; + $"ATCN\r"}; + foreach (var cmd in cmds) + { + if (!Cmd(cmd).Contains("OK")) + { + showlog("FAIL"); + break; + } + } } private void btmy_Click(object sender, EventArgs e) @@ -77,13 +162,32 @@ namespace Project.Dialog var cmds = new string[] { "+++", $"ATMY{tbmy.Text}\r" , - $"ATCN"}; + $"ATCN\r"}; + foreach (var cmd in cmds) + { + if (!Cmd(cmd).Contains("OK")) + { + showlog("FAIL"); + break; + } + } } private void button2_Click(object sender, EventArgs e) { //read all(id,ch,my) - + if (Cmd("+++").Contains("OK")) + { + var id = Cmd("ATID\r"); + var ch = Cmd("ATCH\r"); + var my = Cmd("ATMY\r"); + Cmd("ATCN\r"); + this.BeginInvoke(new Action(() => { + this.tbpanid.Text = id; + this.tbch.Text = ch; + this.tbmy.Text = my; + })); + } } } } diff --git a/Cs_HMI/Project/Dialog/fXbeeSetting.resx b/Cs_HMI/Project/Dialog/fXbeeSetting.resx index 1af7de1..c358324 100644 --- a/Cs_HMI/Project/Dialog/fXbeeSetting.resx +++ b/Cs_HMI/Project/Dialog/fXbeeSetting.resx @@ -117,4 +117,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + \ No newline at end of file diff --git a/Cs_HMI/Project/StateMachine/_SPS.cs b/Cs_HMI/Project/StateMachine/_SPS.cs index 47f7f86..570bd5a 100644 --- a/Cs_HMI/Project/StateMachine/_SPS.cs +++ b/Cs_HMI/Project/StateMachine/_SPS.cs @@ -47,8 +47,11 @@ namespace Project // XBee 연결 lock (connectobj) { - ConnectSerialPort(PUB.XBE, PUB.setting.Port_XBE, PUB.setting.Baud_XBE, + if (VAR.BOOL[eVarBool.DISABLE_AUTOCONN_XBEE] == false) + { + ConnectSerialPort(PUB.XBE, PUB.setting.Port_XBE, PUB.setting.Baud_XBE, eVarTime.LastConn_XBE, eVarTime.LastConnTry_XBE, null); + } } @@ -72,7 +75,7 @@ namespace Project else if (PUB.BMS.IsValid == false) { var ts = VAR.TIME.RUN(eVarTime.LastConnTry_BAT); - if (ts.TotalSeconds > ( Math.Max(10,PUB.setting.interval_bms) * 2.5)) + if (ts.TotalSeconds > (Math.Max(10, PUB.setting.interval_bms) * 2.5)) { this.BeginInvoke(new Action(() => { diff --git a/Cs_HMI/SubProject/CommData/Enum.cs b/Cs_HMI/SubProject/CommData/Enum.cs index c67c696..41181ef 100644 --- a/Cs_HMI/SubProject/CommData/Enum.cs +++ b/Cs_HMI/SubProject/CommData/Enum.cs @@ -114,7 +114,7 @@ namespace COMM WAIT_CHARGEACK, //agv area start ( 64 ~ 95) - + DISABLE_AUTOCONN_XBEE, //area start (96~127)