📄 form1.cs
字号:
}
SetupComm(hComm, MAXBLOCK, MAXBLOCK);
// SET THE COMM TIMEOUTS.
COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();
GetCommTimeouts(hComm,ref ctoCommPort);
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 CSharpSerialPort
{
/// <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.Button button1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.TextBox textBoxReceive;
private System.Windows.Forms.Button button2;
private SerialAPI.SerialPort Serial=new SerialAPI.SerialPort();
bool PortOpen=false;
private System.Windows.Forms.TextBox textBoxSend;
private System.Windows.Forms.Label label1;
private Microsoft.WindowsCE.Forms.InputPanel inputPanel1;
private System.Windows.Forms.Label label2;
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.button1 = new System.Windows.Forms.Button();
this.textBoxReceive = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.textBoxSend = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.inputPanel1 = new Microsoft.WindowsCE.Forms.InputPanel();
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);
//
// button1
//
this.button1.Location = new System.Drawing.Point(128, 230);
this.button1.Size = new System.Drawing.Size(72, 24);
this.button1.Text = "发送";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBoxReceive
//
this.textBoxReceive.Location = new System.Drawing.Point(24, 22);
this.textBoxReceive.Multiline = true;
this.textBoxReceive.Size = new System.Drawing.Size(192, 144);
this.textBoxReceive.Text = "";
//
// button2
//
this.button2.Location = new System.Drawing.Point(40, 230);
this.button2.Size = new System.Drawing.Size(72, 24);
this.button2.Text = "打开";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBoxSend
//
this.textBoxSend.Location = new System.Drawing.Point(24, 191);
this.textBoxSend.Size = new System.Drawing.Size(192, 21);
this.textBoxSend.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(23, 4);
this.label1.Size = new System.Drawing.Size(72, 16);
this.label1.Text = "接收区";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 173);
this.label2.Size = new System.Drawing.Size(64, 16);
this.label2.Text = "发送区";
//
// Form1
//
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBoxSend);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBoxReceive);
this.Controls.Add(this.button1);
this.Menu = this.mainMenu1;
this.Text = "Form1";
}
#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)
{
if(textBoxSend.TextLength==0)
{
return;
}
byte[] buf;
buf=new byte [textBoxSend.TextLength];
for(int i=0;i<textBoxSend.TextLength;i++)
{
buf[i]=Convert.ToByte(textBoxSend.Text[i]);
}
Serial.WritePort(buf);
}
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);
button2.Text="关闭";
}
}
else
{
Receive=false;
Serial.ClosePort();
PortOpen=false;
button2.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++)
{
textBoxReceive.Text+=Convert.ToChar(buf[i]).ToString();
}
}
//Application.DoEvents();
}
}
//Thread.Sleep(0);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -