xbee 값 설정기능 추가

This commit is contained in:
backuppc
2025-12-23 13:07:01 +09:00
parent 8499c1c5be
commit 35df73fd29
5 changed files with 209 additions and 91 deletions

View File

@@ -28,19 +28,21 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button();
this.tbBaud = new System.Windows.Forms.TextBox(); this.tbBaud = new System.Windows.Forms.TextBox();
this.tbPortName = new System.Windows.Forms.ComboBox(); this.tbPortName = new System.Windows.Forms.ComboBox();
this.panel1 = new System.Windows.Forms.Panel(); this.panel1 = new System.Windows.Forms.Panel();
this.panel2 = new System.Windows.Forms.Panel(); this.button2 = new System.Windows.Forms.Button();
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.btmy = new System.Windows.Forms.Button(); this.btmy = new System.Windows.Forms.Button();
this.tbmy = new System.Windows.Forms.TextBox(); 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.panel1.SuspendLayout();
this.panel2.SuspendLayout(); this.panel2.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
@@ -92,6 +94,76 @@
this.panel1.Size = new System.Drawing.Size(580, 118); this.panel1.Size = new System.Drawing.Size(580, 118);
this.panel1.TabIndex = 4; 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 // panel2
// //
this.panel2.Controls.Add(this.rtXbee); this.panel2.Controls.Add(this.rtXbee);
@@ -121,75 +193,10 @@
this.rtXbee.TabIndex = 2; this.rtXbee.TabIndex = 2;
this.rtXbee.Text = ""; this.rtXbee.Text = "";
// //
// tbpanid // serialPort1
// //
this.tbpanid.Font = new System.Drawing.Font("굴림", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129))); this.serialPort1.ReadTimeout = 1000;
this.tbpanid.Location = new System.Drawing.Point(374, 15); this.serialPort1.WriteTimeout = 1000;
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);
// //
// fXbeeSetting // fXbeeSetting
// //
@@ -227,5 +234,6 @@
private System.Windows.Forms.Button btch; private System.Windows.Forms.Button btch;
private System.Windows.Forms.TextBox tbch; private System.Windows.Forms.TextBox tbch;
private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button2;
private System.IO.Ports.SerialPort serialPort1;
} }
} }

View File

@@ -7,7 +7,8 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using AR;
using COMM;
namespace Project.Dialog namespace Project.Dialog
{ {
public partial class fXbeeSetting : Form public partial class fXbeeSetting : Form
@@ -15,7 +16,9 @@ namespace Project.Dialog
public fXbeeSetting() public fXbeeSetting()
{ {
InitializeComponent(); 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) private void fXbeeSetting_Load(object sender, EventArgs e)
{ {
@@ -27,11 +30,16 @@ namespace Project.Dialog
this.tbPortName.Text = PUB.setting.Port_XBE; this.tbPortName.Text = PUB.setting.Port_XBE;
this.tbBaud.Text = PUB.setting.Baud_XBE.ToString(); 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) void showlog(arCtl.LogTextBox rtRx, DateTime LogTime, string TypeStr, string Message)
{ {
if (rtRx.Visible) if (rtRx.Visible)
@@ -41,27 +49,96 @@ namespace Project.Dialog
} }
private void button1_Click(object sender, EventArgs e) private void button1_Click(object sender, EventArgs e)
{ {
if (PUB.XBE.IsOpen) if (this.serialPort1.IsOpen)
PUB.XBE.Close(); {
serialPort1.Close();
showlog("closed");
}
else else
{ {
PUB.XBE.PortName = tbPortName.Text; serialPort1.PortName = tbPortName.Text;
PUB.XBE.Open(); serialPort1.BaudRate = int.Parse(tbBaud.Text);
serialPort1.Open();
showlog("open");
} }
} }
private void fXbeeSetting_FormClosed(object sender, FormClosedEventArgs e) 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) private void btpand_Click(object sender, EventArgs e)
{ {
//각명령마다 회신을 확인하고 다음명령을 실행해야함
//명령수신호 10ms 대기후 다음 명령을 전송
//명령을 설정하면 응답은 OK\d 형태로 입력된다.
var cmds = new string[] { var cmds = new string[] {
"+++", "+++",
$"ATID{tbpanid.Text}\r" , $"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) private void btch_Click(object sender, EventArgs e)
@@ -69,7 +146,15 @@ namespace Project.Dialog
var cmds = new string[] { var cmds = new string[] {
"+++", "+++",
$"ATCH{tbch.Text}\r" , $"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) private void btmy_Click(object sender, EventArgs e)
@@ -77,13 +162,32 @@ namespace Project.Dialog
var cmds = new string[] { var cmds = new string[] {
"+++", "+++",
$"ATMY{tbmy.Text}\r" , $"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) private void button2_Click(object sender, EventArgs e)
{ {
//read all(id,ch,my) //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;
}));
}
} }
} }
} }

View File

@@ -117,4 +117,7 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<metadata name="serialPort1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root> </root>

View File

@@ -46,10 +46,13 @@ namespace Project
// XBee 연결 // XBee 연결
lock (connectobj) lock (connectobj)
{
if (VAR.BOOL[eVarBool.DISABLE_AUTOCONN_XBEE] == false)
{ {
ConnectSerialPort(PUB.XBE, PUB.setting.Port_XBE, PUB.setting.Baud_XBE, ConnectSerialPort(PUB.XBE, PUB.setting.Port_XBE, PUB.setting.Baud_XBE,
eVarTime.LastConn_XBE, eVarTime.LastConnTry_XBE, null); eVarTime.LastConn_XBE, eVarTime.LastConnTry_XBE, null);
} }
}
// BMS 연결 // BMS 연결

View File

@@ -114,7 +114,7 @@ namespace COMM
WAIT_CHARGEACK, WAIT_CHARGEACK,
//agv area start ( 64 ~ 95) //agv area start ( 64 ~ 95)
DISABLE_AUTOCONN_XBEE,
//area start (96~127) //area start (96~127)