C#串口通信编程类(修改版)

这是从网上down下来的一个串口通信类,发现close函数忘记了设置Opened属性为false
还有后面string转byte[]和byte[]转string的函数有错误,索性删掉了
修改后的串口通信类如下:
下一篇将把我的测试程序主程序部分全部代码贴出来
可以坚强勇敢的用来实现串口通信。

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,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.

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://127.0.0.1/wyyxdw.html