📄 form1.cs
字号:
ctoCommPort.ReadIntervalTimeout = Int32.MaxValue;
ctoCommPort.ReadTotalTimeoutConstant = 0;
ctoCommPort.ReadTotalTimeoutMultiplier = 0;
ctoCommPort.WriteTotalTimeoutMultiplier = 10;
ctoCommPort.WriteTotalTimeoutConstant = 1000;
SetCommTimeouts(hComm,ref ctoCommPort);
// SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
// THERE ARE OTHER WAYS OF DOING SETTING THESE BUT THIS IS THE EASIEST.
// IF YOU WANT TO LATER ADD CODE FOR OTHER BAUD RATES, REMEMBER
// THAT THE ARGUMENT FOR BuildCommDCB MUST BE A POINTER TO A STRING.
// ALSO NOTE THAT BuildCommDCB() DEFAULTS TO NO HANDSHAKING.
DCB dcbCommPort = new DCB();
dcbCommPort.DCBlength = Marshal.SizeOf(dcbCommPort);
GetCommState(hComm, ref dcbCommPort);
dcbCommPort.BaudRate = baudRate;
dcbCommPort.Parity = parity;
dcbCommPort.ByteSize = byteSize;
dcbCommPort.StopBits = stopBits;
SetCommState(hComm, ref dcbCommPort);
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
PurgeComm(hComm, PURGE_TXCLEAR | PURGE_TXABORT);
bOpened = true;
return true;
}
// 关闭串口
public bool ClosePort()
{
if (hComm == INVALID_HANDLE_VALUE)
{
return false;
}
if (CloseHandle(hComm))
{
hComm = INVALID_HANDLE_VALUE;
bOpened = false;
return true;
}
else
{
return false;
}
}
// 往串口写数据
public bool WritePort(byte[] WriteBytes)
{
if (hComm == INVALID_HANDLE_VALUE)
{
return false;
}
COMSTAT ComStat = new COMSTAT();
int dwErrorFlags = 0;
ClearCommError(hComm, ref dwErrorFlags, ref ComStat);
if (dwErrorFlags != 0)
PurgeComm(hComm, PURGE_TXCLEAR | PURGE_TXABORT);
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesWritten = 0;
return WriteFile(hComm, WriteBytes, WriteBytes.Length, ref BytesWritten, ref ovlCommPort);
}
// 从串口读数据
public int ReadPort(int NumBytes, byte[] commRead)
{
if (hComm == INVALID_HANDLE_VALUE)
{
return 0;
}
COMSTAT ComStat = new COMSTAT();
int dwErrorFlags = 0;
ClearCommError(hComm, ref dwErrorFlags, ref ComStat);
if (dwErrorFlags != 0)
{
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
}
if (ComStat.cbInQue > 0)
{
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesRead = 0;
ReadFile(hComm, commRead, NumBytes, ref BytesRead, ref ovlCommPort);
return BytesRead;
}
else
{
return 0;
}
}
}
}
namespace SmartDeviceApplication__SerialPort
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MainMenu mainMenu1;
private SerialAPI.SerialPort Serial=new SerialAPI.SerialPort();
bool PortOpen=false;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button btnSend;
private System.Windows.Forms.TextBox txtReceive;
private System.Windows.Forms.Button btnOpen;
private System.Windows.Forms.TextBox txtSend;
bool Receive=false;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.btnSend = new System.Windows.Forms.Button();
this.txtReceive = new System.Windows.Forms.TextBox();
this.btnOpen = new System.Windows.Forms.Button();
this.txtSend = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
//
// mainMenu1
//
this.mainMenu1.MenuItems.Add(this.menuItem1);
//
// menuItem1
//
this.menuItem1.MenuItems.Add(this.menuItem2);
this.menuItem1.Text = "操作";
//
// menuItem2
//
this.menuItem2.Text = "退出";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// btnSend
//
this.btnSend.Location = new System.Drawing.Point(232, 296);
this.btnSend.Size = new System.Drawing.Size(72, 24);
this.btnSend.Text = "发送";
this.btnSend.Click += new System.EventHandler(this.button1_Click);
//
// txtReceive
//
this.txtReceive.Location = new System.Drawing.Point(128, 88);
this.txtReceive.Multiline = true;
this.txtReceive.Size = new System.Drawing.Size(192, 144);
this.txtReceive.Text = "";
//
// btnOpen
//
this.btnOpen.Location = new System.Drawing.Point(144, 296);
this.btnOpen.Size = new System.Drawing.Size(72, 24);
this.btnOpen.Text = "打开";
this.btnOpen.Click += new System.EventHandler(this.button2_Click);
//
// txtSend
//
this.txtSend.Location = new System.Drawing.Point(128, 256);
this.txtSend.Size = new System.Drawing.Size(192, 21);
this.txtSend.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(128, 64);
this.label1.Size = new System.Drawing.Size(72, 16);
this.label1.Text = "接收区";
//
// label2
//
this.label2.Location = new System.Drawing.Point(128, 240);
this.label2.Size = new System.Drawing.Size(64, 16);
this.label2.Text = "发送区";
//
// Form1
//
this.ClientSize = new System.Drawing.Size(634, 464);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtSend);
this.Controls.Add(this.btnOpen);
this.Controls.Add(this.txtReceive);
this.Controls.Add(this.btnSend);
this.Menu = this.mainMenu1;
this.Text = "串口通讯";
this.Load += new System.EventHandler(this.Form1_Load);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
static void Main()
{
Application.Run(new Form1());
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
Receive=false;
Serial.ClosePort();
this.Close();
}
private void button1_Click(object sender, System.EventArgs e)
{
bool flag = false;
if(txtSend.TextLength==0)
{
return;
}
byte[] buf;
buf=new byte [txtSend.TextLength];
for(int i=0;i<txtSend.TextLength;i++)
{
buf[i]=Convert.ToByte(txtSend.Text[i]);
}
flag = Serial.WritePort(buf);
if(flag)
{
MessageBox.Show("发送成功!","提示");
}
else
{
MessageBox.Show("发送失败!","提示");
}
}
private void button2_Click(object sender, System.EventArgs e)
{
if(!PortOpen)
{
if(Serial.OpenPort("COM1:",9600,0,8,1))
{
PortOpen=true;
Receive=true;
ThreadPool.QueueUserWorkItem(new WaitCallback(SerialReceive),0);
btnOpen.Text="关闭";
}
}
else
{
Receive=false;
Serial.ClosePort();
PortOpen=false;
btnOpen.Text="打开";
}
}
public void SerialReceive(Object a)
{
byte [] buf;
buf=new byte [1];
int bytesRead=0;
int i;
while(Receive)
{
if(Serial.Opened)
{
bytesRead=Serial.ReadPort(1,buf);
if(bytesRead>0)
{
for(i=0;i<bytesRead;i++)
{
txtReceive.Text+=Convert.ToChar(buf[i]).ToString();
}
}
}
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -