📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using Microsoft.WindowsCE.Forms;
namespace COMTest
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.ComboBox comboBox2;
private System.Windows.Forms.ComboBox comboBox3;
private System.Windows.Forms.ComboBox comboBox4;
private System.Windows.Forms.ComboBox comboBox5;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button4;
#region 串口变量定义
const uint COM_SINGLEFRAME=4096;
const uint COM_FRAMECOUNT=16;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
const uint WM_COMUSERDEFINE=0x0501;
enum COM_NO{COM1=0,COM2,COM3,COM4,COM5,COM6,COM7,COM8,COM9};
enum COM_StopBits{COM_StopBits_1=0,COM_StopBits_1_5,COM_StopBits_2};
enum COM_ByteSize{COM_ByteSize_5=0, COM_ByteSize_6, COM_ByteSize_7, COM_ByteSize_8};
enum COM_Parity{COM_Parity_None=0, COM_Parity_Odd, COM_Parity_Even, COM_Parity_Mark, COM_Parity_Space};
enum COM_Baudrate{COM_Baudrate_110=0,COM_Baudrate_300,COM_Baudrate_600,COM_Baudrate_1200,COM_Baudrate_2400,COM_Baudrate_4800,COM_Baudrate_9600,COM_Baudrate_19200,COM_Baudrate_38400,COM_Baudrate_57600,COM_Baudrate_115200};
enum THREAD_PRIORITY{PRIORITY_TIME_CRITICAL=0,PRIORITY_HIGHEST,PRIORITY_ABOVE_NORMAL,PRIORITY_NORMAL,PRIORITY_BELOW_NORMAL,PRIORITY_LOWEST,PRIORITY_ABOVE_IDLE,PRIORITY_IDLE};
[DllImport("COMPort.dll",CallingConvention=CallingConvention.Winapi)]
static extern IntPtr COMPort_Init(
COM_NO comNo,
COM_Baudrate BaudRate,
COM_Parity Parity,
COM_ByteSize ByteSize,
COM_StopBits StopBits,
uint FrameCount,
uint SingleFrameLength,
THREAD_PRIORITY SendThreadPriority,
THREAD_PRIORITY RecvThreadPriority,
uint WM_USERDEFINE,
IntPtr MainHWnd
);
[DllImport("COMPort.dll",CallingConvention=CallingConvention.Winapi)]
static extern void COMPort_DeInit(IntPtr ComPointer);
[DllImport("COMPort.dll",CallingConvention=CallingConvention.Winapi)]
static extern void COMPort_SendByte(IntPtr ComPointer,byte[] Data,uint DataLen);
[DllImport("COMPort.dll",CallingConvention=CallingConvention.Winapi)]
static extern void COMPort_RecvByte(IntPtr ComPointer,byte[] Data,ref uint DataLen);
#endregion 串口变量定义
myMessageWindow MMW=null;
byte[] CurrentByte=new byte[COM_SINGLEFRAME*COM_FRAMECOUNT];
uint AllCount=0;
IntPtr ComPointer=IntPtr.Zero;
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.button1 = new System.Windows.Forms.Button();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.comboBox2 = new System.Windows.Forms.ComboBox();
this.comboBox3 = new System.Windows.Forms.ComboBox();
this.comboBox4 = new System.Windows.Forms.ComboBox();
this.comboBox5 = new System.Windows.Forms.ComboBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.listBox1 = new System.Windows.Forms.ListBox();
this.button4 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 56);
this.button1.Size = new System.Drawing.Size(88, 32);
this.button1.Text = "打开串口";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// comboBox1
//
this.comboBox1.Items.Add("COM1");
this.comboBox1.Items.Add("COM2");
this.comboBox1.Items.Add("COM3");
this.comboBox1.Items.Add("COM4");
this.comboBox1.Items.Add("COM5");
this.comboBox1.Items.Add("COM6");
this.comboBox1.Items.Add("COM7");
this.comboBox1.Items.Add("COM8");
this.comboBox1.Items.Add("COM9");
this.comboBox1.Location = new System.Drawing.Point(8, 16);
this.comboBox1.Size = new System.Drawing.Size(64, 20);
//
// comboBox2
//
this.comboBox2.Items.Add("110");
this.comboBox2.Items.Add("300");
this.comboBox2.Items.Add("600");
this.comboBox2.Items.Add("1200");
this.comboBox2.Items.Add("2400");
this.comboBox2.Items.Add("4800");
this.comboBox2.Items.Add("9600");
this.comboBox2.Items.Add("19200");
this.comboBox2.Items.Add("38400");
this.comboBox2.Items.Add("57600");
this.comboBox2.Items.Add("115200");
this.comboBox2.Location = new System.Drawing.Point(88, 16);
this.comboBox2.Size = new System.Drawing.Size(64, 20);
//
// comboBox3
//
this.comboBox3.Items.Add("None");
this.comboBox3.Items.Add("ODD");
this.comboBox3.Items.Add("Even");
this.comboBox3.Items.Add("Mark");
this.comboBox3.Items.Add("Space");
this.comboBox3.Location = new System.Drawing.Point(168, 16);
this.comboBox3.Size = new System.Drawing.Size(64, 20);
//
// comboBox4
//
this.comboBox4.Items.Add("5");
this.comboBox4.Items.Add("6");
this.comboBox4.Items.Add("7");
this.comboBox4.Items.Add("8");
this.comboBox4.Location = new System.Drawing.Point(240, 16);
this.comboBox4.Size = new System.Drawing.Size(64, 20);
//
// comboBox5
//
this.comboBox5.Items.Add("1");
this.comboBox5.Items.Add("1.5");
this.comboBox5.Items.Add("2");
this.comboBox5.Location = new System.Drawing.Point(320, 16);
this.comboBox5.Size = new System.Drawing.Size(64, 20);
//
// button2
//
this.button2.Location = new System.Drawing.Point(104, 56);
this.button2.Size = new System.Drawing.Size(64, 32);
this.button2.Text = "关闭串口";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(176, 56);
this.button3.Size = new System.Drawing.Size(80, 32);
this.button3.Text = "发送数据";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(272, 64);
this.textBox1.Size = new System.Drawing.Size(40, 21);
this.textBox1.Text = "255";
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(8, 96);
this.listBox1.Size = new System.Drawing.Size(544, 278);
//
// button4
//
this.button4.Location = new System.Drawing.Point(344, 56);
this.button4.Size = new System.Drawing.Size(72, 32);
this.button4.Text = "clear";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(440, 24);
this.label1.Size = new System.Drawing.Size(112, 20);
this.label1.Text = "Clear以后总个数";
//
// label2
//
this.label2.Location = new System.Drawing.Point(440, 64);
this.label2.Text = "0";
//
// Form1
//
this.ClientSize = new System.Drawing.Size(562, 384);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button4);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.comboBox5);
this.Controls.Add(this.comboBox4);
this.Controls.Add(this.comboBox3);
this.Controls.Add(this.comboBox2);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.button1);
this.Text = "串口测试";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.Load += new System.EventHandler(this.Form1_Load);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.comboBox1.SelectedIndex=0; //com1
this.comboBox2.SelectedIndex=6; //9600
this.comboBox3.SelectedIndex=0; //None
this.comboBox4.SelectedIndex=3; //8
this.comboBox5.SelectedIndex=0; //1
//得到自己的接受消息的窗口
this.MMW=new myMessageWindow(this);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -