Form1.cs
上传用户:svehicle
上传日期:2013-07-27
资源大小:62k
文件大小:17k
- using System;
- using System.Drawing;
- using System.Collections;
- //using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.Runtime.InteropServices;
- using System.Threading;
- namespace SerialAPI
- {
- /// <summary>
- /// SerialPort 的摘要说明。
- /// </summary>
- public class SerialPort
- {
- #region 申明要引用的和串口调用有关的API
- //win32 api constants
- private const uint GENERIC_READ = 0x80000000;
- private const uint GENERIC_WRITE = 0x40000000;
- private const int OPEN_EXISTING = 3;
- private const int INVALID_HANDLE_VALUE = -1;
- private const int MAXBLOCK = 4096;
- private const uint PURGE_TXABORT = 0x0001; // Kill the pending/current writes to the comm port.
- private const uint PURGE_RXABORT = 0x0002; // Kill the pending/current reads to the comm port.
- private const uint PURGE_TXCLEAR = 0x0004; // Kill the transmit queue if there.
- private const uint PURGE_RXCLEAR = 0x0008; // Kill the typeahead buffer if there.
- [StructLayout(LayoutKind.Sequential)]
- private struct DCB
- {
- //taken from c struct in platform sdk
- public int DCBlength; // sizeof(DCB)
- public int BaudRate; // current baud rate
- public int fBinary; // binary mode, no EOF check
- public int fParity; // enable parity checking
- public int fOutxCtsFlow; // CTS output flow control
- public int fOutxDsrFlow; // DSR output flow control
- public int fDtrControl; // DTR flow control type
- public int fDsrSensitivity; // DSR sensitivity
- public int fTXContinueOnXoff; // XOFF continues Tx
- public int fOutX; // XON/XOFF out flow control
- public int fInX; // XON/XOFF in flow control
- public int fErrorChar; // enable error replacement
- public int fNull; // enable null stripping
- public int fRtsControl; // RTS flow control
- public int fAbortOnError; // abort on error
- public int fDummy2; // reserved
- public ushort wReserved; // not currently used
- public ushort XonLim; // transmit XON threshold
- public ushort XoffLim; // transmit XOFF threshold
- public byte ByteSize; // number of bits/byte, 4-8
- public byte Parity; // 0-4=no,odd,even,mark,space
- public byte StopBits; // 0,1,2 = 1, 1.5, 2
- public char XonChar; // Tx and Rx XON character
- public char XoffChar; // Tx and Rx XOFF character
- public char ErrorChar; // error replacement character
- public char EofChar; // end of input character
- public char EvtChar; // received event character
- public ushort wReserved1; // reserved; do not use
- }
- [StructLayout(LayoutKind.Sequential)]
- private struct COMMTIMEOUTS
- {
- public int ReadIntervalTimeout;
- public int ReadTotalTimeoutMultiplier;
- public int ReadTotalTimeoutConstant;
- public int WriteTotalTimeoutMultiplier;
- public int WriteTotalTimeoutConstant;
- }
- [StructLayout(LayoutKind.Sequential)]
- private struct OVERLAPPED
- {
- public int Internal;
- public int InternalHigh;
- public int Offset;
- public int OffsetHigh;
- public int hEvent;
- }
- [StructLayout(LayoutKind.Sequential)]
- private struct COMSTAT
- {
- /*public int fCtsHold;
- public int fDsrHold;
- public int fRlsdHold;
- public int fXoffHold;
- public int fXoffSent;
- public int fEof;
- public int fTxim;
- public int fReserved;
- public int cbInQue;
- public int cbOutQue;*/
- // Should have a reverse, i don't know why!!!!!
- public int cbOutQue;
- public int cbInQue;
- public int fReserved;
- public int fTxim;
- public int fEof;
- public int fXoffSent;
- public int fXoffHold;
- public int fRlsdHold;
- public int fDsrHold;
- public int fCtsHold;
- }
- #if FULLFRAMEWORK
- [DllImport("kernel32")]
- private static extern int CreateFile(
- string lpFileName, // file name
- uint dwDesiredAccess, // access mode
- int dwShareMode, // share mode
- int lpSecurityAttributes, // SD
- int dwCreationDisposition, // how to create
- int dwFlagsAndAttributes, // file attributes
- int hTemplateFile // handle to template file
- );
- #else
- [DllImport("coredll")]
- private static extern int CreateFile(
- string lpFileName, // file name
- uint dwDesiredAccess, // access mode
- int dwShareMode, // share mode
- int lpSecurityAttributes, // SD
- int dwCreationDisposition, // how to create
- int dwFlagsAndAttributes, // file attributes
- int hTemplateFile // handle to template file
- );
- #endif
- #if FULLFRAMEWORK
- [DllImport("kernel32")]
- private static extern bool GetCommState(
- int hFile, // handle to communications device
- ref DCB lpDCB // device-control block
- );
- #else
- [DllImport("coredll")]
- private static extern bool GetCommState(
- int hFile, // handle to communications device
- ref DCB lpDCB // device-control block
- );
- #endif
- #if FULLFRAMEWORK
- [DllImport("kernel32")]
- private static extern bool BuildCommDCB(
- string lpDef, // device-control string
- ref DCB lpDCB // device-control block
- );
- #else
- [DllImport("coredll")]
- private static extern bool BuildCommDCB(
- string lpDef, // device-control string
- ref DCB lpDCB // device-control block
- );
- #endif
- #if FULLFRAMEWORK
- [DllImport("kernel32")]
- private static extern bool SetCommState(
- int hFile, // handle to communications device
- ref DCB lpDCB // device-control block
- );
- #else
- [DllImport("coredll")]
- private static extern bool SetCommState(
- int hFile, // handle to communications device
- ref DCB lpDCB // device-control block
- );
- #endif
- #if FULLFRAMEWORK
- [DllImport("kernel32")]
- private static extern bool GetCommTimeouts(
- int hFile, // handle to comm device
- ref COMMTIMEOUTS lpCommTimeouts // time-out values
- );
- #else
- [DllImport("coredll")]
- private static extern bool GetCommTimeouts(
- int hFile, // handle to comm device
- ref COMMTIMEOUTS lpCommTimeouts // time-out values
- );
- #endif
- #if FULLFRAMEWORK
- [DllImport("kernel32")]
- private static extern bool SetCommTimeouts(
- int hFile, // handle to comm device
- ref COMMTIMEOUTS lpCommTimeouts // time-out values
- );
- #else
- [DllImport("coredll")]
- private static extern bool SetCommTimeouts(
- int hFile, // handle to comm device
- ref COMMTIMEOUTS lpCommTimeouts // time-out values
- );
- #endif
- #if FULLFRAMEWORK
- [DllImport("kernel32")]
- private static extern bool ReadFile(
- int hFile, // handle to file
- byte[] lpBuffer, // data buffer
- int nNumberOfBytesToRead, // number of bytes to read
- ref int lpNumberOfBytesRead, // number of bytes read
- ref OVERLAPPED lpOverlapped // overlapped buffer
- );
- #else
- [DllImport("coredll")]
- private static extern bool ReadFile(
- int hFile, // handle to file
- byte[] lpBuffer, // data buffer
- int nNumberOfBytesToRead, // number of bytes to read
- ref int lpNumberOfBytesRead, // number of bytes read
- ref OVERLAPPED lpOverlapped // overlapped buffer
- );
- #endif
- #if FULLFRAMEWORK
- [DllImport("kernel32")]
- private static extern bool WriteFile(
- int hFile, // handle to file
- byte[] lpBuffer, // data buffer
- int nNumberOfBytesToWrite, // number of bytes to write
- ref int lpNumberOfBytesWritten, // number of bytes written
- ref OVERLAPPED lpOverlapped // overlapped buffer
- );
- #else
- [DllImport("coredll")]
- private static extern bool WriteFile(
- int hFile, // handle to file
- byte[] lpBuffer, // data buffer
- int nNumberOfBytesToWrite, // number of bytes to write
- ref int lpNumberOfBytesWritten, // number of bytes written
- ref OVERLAPPED lpOverlapped // overlapped buffer
- );
- #endif
- #if FULLFRAMEWORK
- [DllImport("kernel32")]
- private static extern bool CloseHandle(
- int hObject // handle to object
- );
- #else
- [DllImport("coredll")]
- private static extern bool CloseHandle(
- int hObject // handle to object
- );
- #endif
- #if FULLFRAMEWORK
- [DllImport("kernel32")]
- private static extern bool ClearCommError(
- int hFile, // handle to file
- ref int lpErrors,
- ref COMSTAT lpStat
- );
- #else
- [DllImport("coredll")]
- private static extern bool ClearCommError(
- int hFile, // handle to file
- ref int lpErrors,
- ref COMSTAT lpStat
- );
- #endif
- #if FULLFRAMEWORK
- [DllImport("kernel32")]
- private static extern bool PurgeComm(
- int hFile, // handle to file
- uint dwFlags
- );
- #else
- [DllImport("coredll")]
- private static extern bool PurgeComm(
- int hFile, // handle to file
- uint dwFlags
- );
- #endif
- #if FULLFRAMEWORK
- [DllImport("kernel32")]
- private static extern bool SetupComm(
- int hFile,
- int dwInQueue,
- int dwOutQueue
- );
- #else
- [DllImport("coredll")]
- private static extern bool SetupComm(
- int hFile,
- int dwInQueue,
- int dwOutQueue
- );
- #endif
- #endregion
- // SerialPort的成员变量
- private int hComm = INVALID_HANDLE_VALUE;
- private bool bOpened = false;
- public bool Opened
- {
- get
- {
- return bOpened;
- }
- }
- /// <summary>
- ///串口的初始化函数
- ///lpFileName 端口名
- ///baudRate 波特率
- ///parity 校验位
- ///byteSize 数据位
- ///stopBits 停止位
- /// <summary>
- public bool OpenPort(string lpFileName,int baudRate,byte parity, byte byteSize, byte stopBits)
- {
- // OPEN THE COMM PORT.
- hComm = CreateFile(lpFileName ,GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
- // IF THE PORT CANNOT BE OPENED, BAIL OUT.
- if(hComm == INVALID_HANDLE_VALUE)
- {
- return false;
- }
- 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 SerialPortPC
- {
- /// <summary>
- /// Form1 的摘要说明。
- /// </summary>
- public class Form1 : System.Windows.Forms.Form
- {
- private System.Windows.Forms.Label label2;
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.TextBox textBoxSend;
- private System.Windows.Forms.Button button2;
- private System.Windows.Forms.TextBox textBoxReceive;
- private System.Windows.Forms.Button button1;
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.Container components = null;
- private SerialAPI.SerialPort Serial=new SerialAPI.SerialPort();
- bool PortOpen=false;
-
- bool Receive=false;
- public Form1()
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
- //
- }
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows 窗体设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.label2 = new System.Windows.Forms.Label();
- this.label1 = new System.Windows.Forms.Label();
- this.textBoxSend = new System.Windows.Forms.TextBox();
- this.button2 = new System.Windows.Forms.Button();
- this.textBoxReceive = new System.Windows.Forms.TextBox();
- this.button1 = new System.Windows.Forms.Button();
- this.SuspendLayout();
- //
- // label2
- //
- this.label2.Location = new System.Drawing.Point(48, 160);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(64, 16);
- this.label2.TabIndex = 6;
- this.label2.Text = "发送区";
- //
- // label1
- //
- this.label1.Location = new System.Drawing.Point(56, 16);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(72, 16);
- this.label1.TabIndex = 7;
- this.label1.Text = "接收区";
- //
- // textBoxSend
- //
- this.textBoxSend.Location = new System.Drawing.Point(48, 192);
- this.textBoxSend.Name = "textBoxSend";
- this.textBoxSend.Size = new System.Drawing.Size(192, 21);
- this.textBoxSend.TabIndex = 8;
- this.textBoxSend.Text = "c8 00 00 c8";
- //
- // button2
- //
- this.button2.Location = new System.Drawing.Point(48, 224);
- this.button2.Name = "button2";
- this.button2.Size = new System.Drawing.Size(72, 24);
- this.button2.TabIndex = 9;
- this.button2.Text = "打开";
- this.button2.Click += new System.EventHandler(this.button2_Click);
- //
- // textBoxReceive
- //
- this.textBoxReceive.Location = new System.Drawing.Point(51, 52);
- this.textBoxReceive.Multiline = true;
- this.textBoxReceive.Name = "textBoxReceive";
- this.textBoxReceive.Size = new System.Drawing.Size(192, 98);
- this.textBoxReceive.TabIndex = 10;
- this.textBoxReceive.Text = "";
- //
- // button1
- //
- this.button1.Location = new System.Drawing.Point(160, 224);
- this.button1.Name = "button1";
- this.button1.Size = new System.Drawing.Size(72, 24);
- this.button1.TabIndex = 11;
- this.button1.Text = "发送";
- this.button1.Click += new System.EventHandler(this.button1_Click);
- //
- // Form1
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(292, 273);
- 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.Name = "Form1";
- this.Text = "Form1";
- this.ResumeLayout(false);
- }
- #endregion
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.Run(new Form1());
- }
- 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="打开";
- }
- }
- 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]);
- // }
- byte[] buf=new byte[4];
- buf[0]=200;
- buf[1]=0;
- buf[2]=0;
- buf[3]=200;
- Serial.WritePort(buf);
- }
- 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);
- }
- }
- }