DigitalIndicator 프로젝트, 인가된 포트 목록

This commit is contained in:
shark219-hub
2024-11-27 09:25:55 +09:00
parent b710f33e07
commit cb6a25f0a2
2 changed files with 51 additions and 34 deletions

View File

@@ -29,7 +29,6 @@
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.tbport = new System.Windows.Forms.TextBox();
this.tbbaud = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
@@ -41,27 +40,17 @@
this.label4 = new System.Windows.Forms.Label();
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
this.button3 = new System.Windows.Forms.Button();
this.cmbIndiport = new System.Windows.Forms.ComboBox();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
this.SuspendLayout();
//
// tbport
//
this.tbport.Font = new System.Drawing.Font("맑은 고딕", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129)));
this.tbport.Location = new System.Drawing.Point(113, 17);
this.tbport.Margin = new System.Windows.Forms.Padding(3, 5, 3, 5);
this.tbport.Name = "tbport";
this.tbport.Size = new System.Drawing.Size(222, 35);
this.tbport.TabIndex = 0;
this.tbport.Text = "com8";
this.tbport.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// tbbaud
//
this.tbbaud.Font = new System.Drawing.Font("맑은 고딕", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129)));
this.tbbaud.Location = new System.Drawing.Point(471, 17);
this.tbbaud.Margin = new System.Windows.Forms.Padding(3, 5, 3, 5);
this.tbbaud.Name = "tbbaud";
this.tbbaud.Size = new System.Drawing.Size(215, 35);
this.tbbaud.Size = new System.Drawing.Size(215, 49);
this.tbbaud.TabIndex = 0;
this.tbbaud.Text = "9600";
this.tbbaud.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
@@ -72,7 +61,7 @@
this.label1.Font = new System.Drawing.Font("맑은 고딕", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129)));
this.label1.Location = new System.Drawing.Point(24, 22);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(53, 30);
this.label1.Size = new System.Drawing.Size(80, 45);
this.label1.TabIndex = 1;
this.label1.Text = "port";
//
@@ -82,7 +71,7 @@
this.label2.Font = new System.Drawing.Font("맑은 고딕", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129)));
this.label2.Location = new System.Drawing.Point(348, 22);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(62, 30);
this.label2.Size = new System.Drawing.Size(94, 45);
this.label2.TabIndex = 1;
this.label2.Text = "baud";
//
@@ -108,7 +97,7 @@
this.textBox1.Location = new System.Drawing.Point(120, 142);
this.textBox1.Margin = new System.Windows.Forms.Padding(3, 5, 3, 5);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(215, 35);
this.textBox1.Size = new System.Drawing.Size(215, 49);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "65636";
this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
@@ -130,7 +119,7 @@
this.label3.Font = new System.Drawing.Font("맑은 고딕", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129)));
this.label3.Location = new System.Drawing.Point(12, 102);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(91, 30);
this.label3.Size = new System.Drawing.Size(141, 45);
this.label3.TabIndex = 6;
this.label3.Text = "slave no";
//
@@ -140,7 +129,7 @@
this.label4.Font = new System.Drawing.Font("맑은 고딕", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129)));
this.label4.Location = new System.Drawing.Point(12, 147);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(62, 30);
this.label4.Size = new System.Drawing.Size(97, 45);
this.label4.TabIndex = 7;
this.label4.Text = "value";
//
@@ -153,7 +142,7 @@
0,
0});
this.numericUpDown1.Name = "numericUpDown1";
this.numericUpDown1.Size = new System.Drawing.Size(215, 35);
this.numericUpDown1.Size = new System.Drawing.Size(215, 49);
this.numericUpDown1.TabIndex = 8;
this.numericUpDown1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.numericUpDown1.Value = new decimal(new int[] {
@@ -173,11 +162,20 @@
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// cmbIndiport
//
this.cmbIndiport.FormattingEnabled = true;
this.cmbIndiport.Location = new System.Drawing.Point(120, 19);
this.cmbIndiport.Name = "cmbIndiport";
this.cmbIndiport.Size = new System.Drawing.Size(147, 53);
this.cmbIndiport.TabIndex = 46;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 30F);
this.AutoScaleDimensions = new System.Drawing.SizeF(18F, 45F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1100, 558);
this.Controls.Add(this.cmbIndiport);
this.Controls.Add(this.button3);
this.Controls.Add(this.numericUpDown1);
this.Controls.Add(this.label4);
@@ -188,7 +186,6 @@
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.tbbaud);
this.Controls.Add(this.tbport);
this.Font = new System.Drawing.Font("맑은 고딕", 15.75F);
this.Margin = new System.Windows.Forms.Padding(3, 5, 3, 5);
this.Name = "Form1";
@@ -202,8 +199,6 @@
}
#endregion
private System.Windows.Forms.TextBox tbport;
private System.Windows.Forms.TextBox tbbaud;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
@@ -215,6 +210,7 @@
private System.Windows.Forms.Label label4;
private System.Windows.Forms.NumericUpDown numericUpDown1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.ComboBox cmbIndiport;
}
}

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -24,17 +25,26 @@ namespace DigitalIndicator
private void Form1_Load(object sender, EventArgs e)
{
string[] ports = GetAvailablePorts();
cmbIndiport.Items.Clear();
cmbIndiport.Items.AddRange(ports);
timer1.Start();
}
private void button1_Click(object sender, EventArgs e)
private static string[] GetAvailablePorts()
{
if (tbport.Text.isEmpty())
return SerialPort.GetPortNames();
}
private void button1_Click(object sender, EventArgs e)
{ // 연결/끊기
if (cmbIndiport.Text.isEmpty())
{
UTIL.MsgE("포트번호를 입력하세요");
return;
}
if (int.TryParse(tbbaud.Text, out int port) == false)
if (int.TryParse(tbbaud.Text, out int baud) == false)
{
UTIL.MsgE("baud rate 값을 입력하세요\n(기본:9600)");
return;
@@ -49,10 +59,14 @@ namespace DigitalIndicator
}
else
{
dev.PortName = this.tbport.Text;
dev.PortName = this.cmbIndiport.Text;
dev.BaudRate = baud;
dev.DataBits = 8; // 데이터 비트
dev.Parity = System.IO.Ports.Parity.None;
dev.StopBits = System.IO.Ports.StopBits.One;
dev.ReadTimeout = 2000;
dev.WriteTimeout = 2000;
try
{
dev.Open();
@@ -78,7 +92,7 @@ namespace DigitalIndicator
}
private void button2_Click(object sender, EventArgs e)
{
{ // 쓰기
if (Int32.TryParse(this.textBox1.Text, out int value) == false)
{
UTIL.MsgE("숫자로 입력하세요");
@@ -92,16 +106,23 @@ namespace DigitalIndicator
var valuebuffer = splitI32(value);
var slave = (byte)numericUpDown1.Value;
modbus.WriteMultipleRegisters(slave, 0, valuebuffer); //ADDRESS 0=40001
try
{
modbus.WriteMultipleRegisters(slave, 0, valuebuffer); //ADDRESS 0=40001
}
catch (System.TimeoutException ex)
{
UTIL.MsgE(ex.Message);
}
}
UInt16[] splitI32(Int32 value)
{
var hValue = (UInt16)(value >> 16);
var lValue = (UInt16)(value & 0xFFFF);
return new ushort[] { hValue, lValue };
UInt16[] splitI32(Int32 value) // 0 ~ 65535
{ // 32비트 정수(Int32) --> 2개(상위/하위)의 16비트 부호 없는 정수(UInt16)로 분할
var hValue = (UInt16)(value >> 16); // 오른쪽으로 16비트 시프트하여 상위 16비트를 추출
var lValue = (UInt16)(value & 0xFFFF); // (0xFFFF = 16비트의 모든 비트가 1인 값)와의 비트 AND 연산을 수행하여 하위 16비트를 추출
return new ushort[] { hValue, lValue }; // 16비트 부호없는 정수 배열을 반환 (UInt16 = ushort)
}
private void button3_Click(object sender, EventArgs e)