在C#中實現串口通訊的方法

來源:互聯網
上載者:User
通常,在C#中實現串口通訊,我們有四種方法:
第一:通過MSCOMM控制項這是最簡單的,最方便的方法。可功能上很難做到控制自如,同時這個控制項並不是系統本身所帶,所以還得註冊,不在本文討論範圍。可以訪問http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=320 ,一個國外網友的寫的教程,作者很熱心,我曾有發郵件給他,很快就回複了。

第二:微軟在.NET新推出了一個串口控制項,基於.NET的P/Invoke調用方法實現,詳細的大家可以訪問微軟網站http://msdn.microsoft.com/msdnmag/issues/02/10/NETSerialComm/default.aspx,方便得到更多資料。

第三:就是用第三方控制項啦,可一般都要付費的,不太合實際,不作考慮

第四:自己用API寫串口通訊,這樣難度高點,但對於我們來說,可以方便實現自己想要的各種功能

在本文,我們採用第四種方法來實現串口通訊,不過不是自己寫,用一個國外網友現成的已經封裝好的類庫,不過功能簡單點,相對我們來說已經夠用了。

在整個終端簡訊的操作過程中,與串口的通訊,只用到了四個功能,開啟、寫、讀、關閉串口。下面是類庫對這四個功能的定義:

開啟串口:

函數原型:public void Open()

說明:開啟事先設定好的連接埠

樣本:

using JustinIO;    static JustinIO.CommPort ss_port = new JustinIO.CommPort(); ss_port.PortNum = COM1; //連接埠號碼 ss_port.BaudRate = 19200; //串口通訊傳輸速率 ss_port.ByteSize = 8; //資料位元 ss_port.Parity = 0; //同位 ss_port.StopBits = 1;//停止位 ss_port.ReadTimeout = 1000; //讀逾時 try {  if (ss_port.Opened)  {   ss_port.Close();   ss_port.Open(); //開啟串口  }  else  {   ss_port.Open();//開啟串口  }  return true; } catch(Exception e)  {  MessageBox.Show("錯誤:" + e.Message);  return false; }

寫串口:

函數原型:public void Write(byte[] WriteBytes)

WriteBytes 就是你的寫入的位元組,注意,字串要轉換成位元組數組才能進行通訊

樣本:

ss_port.Write(Encoding.ASCII.GetBytes("AT+CGMI\r")); //擷取手機品牌

讀串口:

函數原型:public byte[] Read(int NumBytes)

NumBytes 讀入緩衝數,注意讀取來的是位元組數組,要實際應用中要進行字元轉換

樣本:

string response = Encoding.ASCII.GetString(ss_port.Read(128)); //讀取128個位元組緩衝

關閉串口:

函數原型:ss_port.Close()

樣本:

ss_port.Close();

由於篇幅,以及串口通訊涉及內容廣泛,我在這裡只講這些。

在上面我們已經把終端簡訊所需的各種原始技術有所瞭解,是可以小試牛刀的時候了。

using System; using System.Runtime.InteropServices;    namespace BusApp {  ///   ///   ///   public class mycom  {   public mycom()   {    //     // TODO: 在此處添加建構函式邏輯    //   }   public int PortNum; //1,2,3,4   public int BaudRate; //1200,2400,4800,9600   public byte ByteSize; //8 bits   public byte Parity; // 0-4=no,odd,even,mark,space    public byte StopBits; // 0,1,2 = 1, 1.5, 2    public int ReadTimeout; //10        //comm port win32 file handle   private int hComm = -1;        public bool Opened = false;         //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;        [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;    }          [DllImport("kernel32.dll")]   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    );   [DllImport("kernel32.dll")]   private static extern bool GetCommState(    int hFile,  // handle to communications device    ref DCB lpDCB    // device-control block    );    [DllImport("kernel32.dll")]   private static extern bool BuildCommDCB(    string lpDef,  // device-control string    ref DCB lpDCB     // device-control block    );   [DllImport("kernel32.dll")]   private static extern bool SetCommState(    int hFile,  // handle to communications device    ref DCB lpDCB    // device-control block    );   [DllImport("kernel32.dll")]   private static extern bool GetCommTimeouts(    int hFile,                  // handle to comm device    ref COMMTIMEOUTS lpCommTimeouts  // time-out values    );    [DllImport("kernel32.dll")]    private static extern bool SetCommTimeouts(    int hFile,                  // handle to comm device    ref COMMTIMEOUTS lpCommTimeouts  // time-out values    );   [DllImport("kernel32.dll")]   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    );   [DllImport("kernel32.dll")]    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    );   [DllImport("kernel32.dll")]   private static extern bool CloseHandle(    int hObject   // handle to object    );        public void Open()    {          DCB dcbCommPort = new DCB();    COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();                 // OPEN THE COMM PORT.              hComm = CreateFile("COM" + PortNum ,GENERIC_READ | GENERIC_WRITE,0, 0,OPEN_EXISTING,0,0);         // IF THE PORT CANNOT BE OPENED, BAIL OUT.    if(hComm == INVALID_HANDLE_VALUE)     {     throw(new ApplicationException("Comm Port Can Not Be Opened"));    }         // SET THE COMM TIMEOUTS.          GetCommTimeouts(hComm,ref ctoCommPort);    ctoCommPort.ReadTotalTimeoutConstant = ReadTimeout;    ctoCommPort.ReadTotalTimeoutMultiplier = 0;    ctoCommPort.WriteTotalTimeoutMultiplier = 0;    ctoCommPort.WriteTotalTimeoutConstant = 0;      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.         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);           Opened = true;          }        public void Close()    {    if (hComm!=INVALID_HANDLE_VALUE)     {     CloseHandle(hComm);                 Opened=false;    }   }        public byte[] Read(int NumBytes)    {    byte[] BufBytes;    byte[] OutBytes;    BufBytes = new byte[NumBytes];    if (hComm!=INVALID_HANDLE_VALUE)     {     OVERLAPPED ovlCommPort = new OVERLAPPED();     int BytesRead=0;     ReadFile(hComm,BufBytes,NumBytes,ref BytesRead,ref ovlCommPort);     OutBytes = new byte[BytesRead];     Array.Copy(BufBytes,OutBytes,BytesRead);    }     else     {     throw(new ApplicationException("Comm Port Not Open"));    }    return OutBytes;   }        public int Write(byte[] WriteBytes)    {    int BytesWritten = 0;    if (hComm!=INVALID_HANDLE_VALUE)     {     OVERLAPPED ovlCommPort = new OVERLAPPED();     WriteFile(hComm,WriteBytes,WriteBytes.Length,ref BytesWritten,ref ovlCommPort);    }    else     {     throw(new ApplicationException("Comm Port Not Open"));    }      return BytesWritten;   }  } }
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.