📄 form1.cs
字号:
this.t_stopbyte.Size = new System.Drawing.Size(80, 21);
this.t_stopbyte.TabIndex = 2;
this.t_stopbyte.Text = "1";
//
// labParitybit
//
this.labParitybit.Location = new System.Drawing.Point(16, 157);
this.labParitybit.Name = "labParitybit";
this.labParitybit.Size = new System.Drawing.Size(56, 16);
this.labParitybit.TabIndex = 1;
this.labParitybit.Text = "校验位:";
//
// t_parity
//
this.t_parity.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.t_parity.Enabled = false;
this.t_parity.Location = new System.Drawing.Point(80, 152);
this.t_parity.Name = "t_parity";
this.t_parity.Size = new System.Drawing.Size(80, 21);
this.t_parity.TabIndex = 2;
this.t_parity.Text = "0";
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(12, 272);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(40, 23);
this.btnClear.TabIndex = 3;
this.btnClear.Text = "清空";
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// btnInit
//
this.btnInit.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnInit.Location = new System.Drawing.Point(165, 429);
this.btnInit.Name = "btnInit";
this.btnInit.Size = new System.Drawing.Size(72, 23);
this.btnInit.TabIndex = 6;
this.btnInit.Text = "初始化";
//
// textBox8
//
this.textBox8.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox8.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
this.textBox8.Location = new System.Drawing.Point(12, 432);
this.textBox8.Name = "textBox8";
this.textBox8.Size = new System.Drawing.Size(136, 21);
this.textBox8.TabIndex = 7;
//
// labLocalAddr
//
this.labLocalAddr.Location = new System.Drawing.Point(10, 397);
this.labLocalAddr.Name = "labLocalAddr";
this.labLocalAddr.Size = new System.Drawing.Size(100, 16);
this.labLocalAddr.TabIndex = 8;
this.labLocalAddr.Text = "本机地址设置:";
//
// btnShut
//
this.btnShut.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnShut.Location = new System.Drawing.Point(12, 489);
this.btnShut.Name = "btnShut";
this.btnShut.Size = new System.Drawing.Size(64, 23);
this.btnShut.TabIndex = 9;
this.btnShut.Text = "关闭串口";
this.btnShut.Click += new System.EventHandler(this.btnShut_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(512, 533);
this.Controls.Add(this.btnShut);
this.Controls.Add(this.textBox8);
this.Controls.Add(this.labLocalAddr);
this.Controls.Add(this.btnInit);
this.Controls.Add(this.grpBoxPara);
this.Controls.Add(this.btnSend);
this.Controls.Add(this.t_send);
this.Controls.Add(this.msg);
this.Controls.Add(this.labDataPkg);
this.Controls.Add(this.btnClear);
this.Name = "Form1";
this.Text = "串口通讯PC端";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.Load += new System.EventHandler(this.Form1_Load);
this.grpBoxPara.ResumeLayout(false);
this.grpBoxPara.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//程序开启,串口初始化
private void Form1_Load(object sender, System.EventArgs e)
{
PCSerialCom1.PortNum = iPort;
PCSerialCom1.BaudRate = iRate;
PCSerialCom1.ByteSize = bSize;
PCSerialCom1.Parity = bParity;
PCSerialCom1.StopBits = bStopBits;
PCSerialCom1.ReadTimeout = iTimeout;
if (this.OpenCom())
msg.AppendText("串口初始化成功……\r\n");
else
msg.AppendText("串口初始化失败!\r\n");
}
//显示包信息
public string dis_package(byte[] reb)
{
string temp = "";
foreach (byte b in reb)
temp += b.ToString("X2") + " ";
return temp;
}
//开串口
public bool OpenCom()
{
try
{
if (PCSerialCom1.Opened)
{
PCSerialCom1.Close();
PCSerialCom1.Open(); //打开串口
}
else
{
PCSerialCom1.Open();//打开串口
}
return true;
}
catch (Exception e)
{
MessageBox.Show("错误:" + e.Message);
return false;
}
}
//发送按钮
private void btnSend_Click(object sender, System.EventArgs e)
{
if (t_send.Text == "")
{ MessageBox.Show("发送数据为空!"); return; }
byte[] temp1 = mysendb();
int sendnumb = 0;
try
{
sendnumb = PCSerialCom1.Write(temp1);
msg.AppendText("\r\n发送数据(" + sendnumb + "):" + dis_package(temp1));
recb = PCSerialCom1.Read(50);
//if(recb.Length!=0)
msg.AppendText("\r\n接收到数据包:" + dis_package(recb));
}
catch
{ msg.AppendText("\r\n发送失败!"); return; }
//OpenCom();
}
//去掉发送数组中的空格
public string delspace(string putin)
{
string putout = "";
for (int i = 0; i < putin.Length; i++)
{
if (putin[i] != ' ')
putout += putin[i];
}
return putout;
}
//提取数据包
public byte[] mysendb()
{
string temps = delspace(t_send.Text);
byte[] tempb = new byte[50];
int j = 0;
for (int i = 0; i < temps.Length; i = i + 2, j++)
tempb[j] = Convert.ToByte(temps.Substring(i, 2), 16);
byte[] send = new byte[j];
Array.Copy(tempb, send, j);
return send;
}
//清空按钮
private void btnClear_Click(object sender, System.EventArgs e)
{
t_send.Text = string.Empty;
msg.Text = string.Empty;
}
//参数设置
private void btnApply_Click(object sender, System.EventArgs e)
{
PCSerialCom1.PortNum = Convert.ToInt16(t_port.Text); //1,2,3,4
PCSerialCom1.BaudRate = Convert.ToInt16(t_rate.Text); //1200,2400,4800,9600
PCSerialCom1.ByteSize = Convert.ToByte(t_bytesize.Text, 10); //8 bits
PCSerialCom1.Parity = Convert.ToByte(t_parity.Text, 10); // 0-4=no,odd,even,mark,space
PCSerialCom1.StopBits = Convert.ToByte(t_stopbyte.Text, 10); // 0,1,2 = 1, 1.5, 2
//iTimeout=3;
if (this.OpenCom())
msg.AppendText("串口初始化成功……\r\n");
else
msg.AppendText("串口初始化失败!\r\n");
}
//程序关闭,结束串口
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
PCSerialCom1.Close();
}
private void btnShut_Click(object sender, System.EventArgs e)
{
if (PCSerialCom1.Opened)
{
PCSerialCom1.Close();
btnShut.Text = "开启串口";
msg.AppendText("\r\n串口被关闭……");
}
else
{
PCSerialCom1.Open();
btnShut.Text = "关闭串口";
msg.AppendText("\r\n串口成功开启……");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -