使用C#打开或关闭Com口

下面是一个类:

SetComPara()方法是设置Com1口的一些参数

OpenCommPort()方法是打开Com1口

CloseComPort()方法是关闭Com1口

ContractedBlock.gif ExpandedBlockStart.gif Code
class ComOperation
ExpandedBlockStart.gifContractedBlock.gif    
{

ContractedSubBlock.gifExpandedSubBlockStart.gif        
结构体和dllimport#region 结构体和dllimport
        
//public string 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 

        
//设置端口
        public string PortNum = "COM1";   //端口 1,2,3,4  

        
//端口设置(默认值,需根据端口情况重新赋值)
        public int BaudRate = 2400;    //1200,2400,4800,9600 每秒位数
        public byte ByteSize = 8;      //8   bits  数据位
        public byte Parity = 0;        //   0-4=no,odd,even,mark,space   奇偶效验
        public byte StopBits = 0;      //   0,1,2   =   1,   1.5,   2   停止位
        public int ReadTimeout = 10;   //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
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//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
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
public int ReadIntervalTimeout;
            
public int ReadTotalTimeoutMultiplier;
            
public int ReadTotalTimeoutConstant;
            
public int WriteTotalTimeoutMultiplier;
            
public int WriteTotalTimeoutConstant;
        }


        [StructLayout(LayoutKind.Sequential)]
        
private struct OVERLAPPED
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
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 
        );
        
#endregion



ContractedSubBlock.gifExpandedSubBlockStart.gif        
设置端口参数#region 设置端口参数
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 设置端口参数
        
/// </summary>
        
/// <param name="parPortNum"></param>
        
/// <param name="parBaudRate"></param>
        
/// <param name="parByteSize"></param>
        
/// <param name="parParity"></param>
        
/// <param name="parStopBits"></param>

        public void SetComPara(string parPortNum, int parBaudRate, byte parByteSize, byte parParity, byte parStopBits)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//设置端口
            this.PortNum = parPortNum;   //端口 1,2,3,4  

            
this.BaudRate = parBaudRate;    //1200,2400,4800,9600 每秒位数
            this.ByteSize = parByteSize;      //8   bits  数据位
            this.Parity = parParity;        //   0-4=no,odd,even,mark,space   奇偶效验
            this.StopBits = parStopBits;      //   0,1,2   =   1,   1.5,   2   停止位      
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
打开Com端口#region 打开Com端口
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 打开Com端口
        
/// </summary>

        public void OpenCommPort()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{

            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                DCB dcbCommPort 
= new DCB();
                COMMTIMEOUTS ctoCommPort 
= new COMMTIMEOUTS();


                
//   OPEN   THE   COMM   PORT. 
                hComm = CreateFile(PortNum, GENERIC_READ | GENERIC_WRITE, 00, OPEN_EXISTING, 00);

                
//   IF   THE   PORT   CANNOT   BE   OPENED,   BAIL   OUT. 
                if (hComm == INVALID_HANDLE_VALUE)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
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);

                
//将设置的端口参数赋给DCB结构体
                dcbCommPort.BaudRate = BaudRate;
                dcbCommPort.Parity 
= Parity;
                dcbCommPort.ByteSize 
= ByteSize;
                dcbCommPort.StopBits 
= StopBits;
                SetCommState(hComm, 
ref   dcbCommPort);

                Opened 
= true;


            }

            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw ex;
            }

        }

        
#endregion



ContractedSubBlock.gifExpandedSubBlockStart.gif        
关闭Com端口#region 关闭Com端口
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 关闭Com端口 
        
/// </summary>

        public void CloseComPort()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (hComm != INVALID_HANDLE_VALUE)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    CloseHandle(hComm);
                    Opened 
= false;
                }

            }

            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw ex;
            }

        }

        
#endregion

 

转载于:https://www.cnblogs.com/gossip/archive/2009/06/16/1504655.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值