在C#中实现串口通信的方法

在C#中实现串口通信的方法,第1张

概述在C#中实现串口通信方法

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

    通常,在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      {       /// <summary>       ///        /// </summary>       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,space          public byte StopBits;           // 0,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,// 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,OPEN_EXISTING,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;        }       }      }  

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的在C#中实现串口通信的方法全部内容,希望文章能够帮你解决在C#中实现串口通信的方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1237670.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存