⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serialcommiopacket.cs

📁 该项目实现在.net 2003上
💻 CS
📖 第 1 页 / 共 2 页
字号:
#define DEBUG
#define COMPACT_FRAMEWORK
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;

namespace ctd.E1Analyzer.SerialCommIOPacket 
{
	
	public class CommPort 
	{
		public CommPort(int nComNo,int nBaudRate,int nParity)
		{
			PortNum  = nComNo;
			BaudRate = nBaudRate;
			Parity       = (byte)nParity;     // 0-4=no,odd,even,mark,space 
			StopBits     = 0;                 // 0,1,2 = 1, 1.5, 2 
			ReadTimeout  = 1000;
			
		}
		public CommPort(int nComNo,int nBaudRate)
	    {
		    PortNum  = nComNo;
            BaudRate = nBaudRate;
            Parity       = 1;          // 0-4=no,odd,even,mark,space 
		    StopBits     = 0;          // 0,1,2 = 1, 1.5, 2 
		    ReadTimeout  = 1000;
			
	    }
		public CommPort(int nComNo)
		{
			PortNum = nComNo;
		}
		public int  PortNum      = 1; 
		public int  BaudRate     = 19200;
		public byte ByteSize     = 8;
		public byte Parity       = 1;          // 0-4=no,odd,even,mark,space 
		public byte StopBits     = 0;          // 0,1,2 = 1, 1.5, 2 
		public int  ReadTimeout  = 1000;

		
		//comm port win32 file handle
		private int hComm = 0;
		
		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;
		private const long ERROR_IO_PENDING      = 997L;
		private const int  MAXBLOCK              = 4096;
		//
		private const uint PURGE_RXCLEAR         = 0x0008;
		private const uint PURGE_TXCLEAR         = 0x0004;
		private const uint PURGE_RXABORT         = 0x0002;
		private const uint PURGE_TXABORT         = 0x0001;
		
		//
		/*
		[StructLayout(LayoutKind.Sequential)]
		public struct DCB 
		{
			public int DCBlength;           
			public int BaudRate;            
			
			public uint   flags;
			public ushort wReserved;         
			public ushort XonLim;            
			public ushort XoffLim;          
			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 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; 

		}
		*/
        [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 COMPACT_FRAMEWORK
		[DllImport("coredll",SetLastError=true)]
		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("kernel32.dll",SetLastError=true)]
		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 COMPACT_FRAMEWORK
		[DllImport("coredll",SetLastError=true)]
		private static extern bool GetCommState(
		  int hFile,       // handle to communications device
		  ref DCB lpDCB    // device-control block
		);
#else
        [DllImport("kernel32.dll",SetLastError=true)]
		private static extern bool GetCommState(
		  int hFile,       // handle to communications device
		  ref DCB lpDCB    // device-control block
		);
#endif
#if COMPACT_FRAMEWORK
        [DllImport("coredll",SetLastError=true)]
		private static extern bool BuildCommDCB(
		  string lpDef,     // device-control string
		  ref DCB lpDCB     // device-control block
		);
#else
        [DllImport("kernel32.dll",SetLastError=true)]
		private static extern bool BuildCommDCB(
		  string lpDef,     // device-control string
		  ref DCB lpDCB     // device-control block
		);
#endif
#if COMPACT_FRAMEWORK
		[DllImport("coredll",SetLastError=true)]
		private static extern bool SetCommState(
		  int hFile,       // handle to communications device
		  ref DCB lpDCB    // device-control block
		);
#else
       	[DllImport("kernel32.dll",SetLastError=true)]
		private static extern bool SetCommState(
		  int hFile,       // handle to communications device
		  ref DCB lpDCB    // device-control block
		);
#endif
#if COMPACT_FRAMEWORK
		[DllImport("coredll",SetLastError=true)]
		private static extern bool GetCommTimeouts(
		  int hFile,                       // handle to comm device
		  ref COMMTIMEOUTS lpCommTimeouts  // time-out values
		);	
#else
        [DllImport("kernel32.dll",SetLastError=true)]
		private static extern bool GetCommTimeouts(
		  int hFile,                       // handle to comm device
		  ref COMMTIMEOUTS lpCommTimeouts  // time-out values
		);	
#endif
#if COMPACT_FRAMEWORK
		[DllImport("coredll",SetLastError=true)]	
		private static extern bool SetCommTimeouts(
		  int hFile,                       // handle to comm device
		  ref COMMTIMEOUTS lpCommTimeouts  // time-out values
		);
#else
       [DllImport("kernel32.dll",SetLastError=true)]	
		private static extern bool SetCommTimeouts(
		  int hFile,                       // handle to comm device
		  ref COMMTIMEOUTS lpCommTimeouts  // time-out values
		);
#endif
#if COMPACT_FRAMEWORK
		[DllImport("coredll",SetLastError=true)]
		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("kernel32.dll",SetLastError=true)]
		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 COMPACT_FRAMEWORK
		[DllImport("coredll",SetLastError=true)]
		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("kernel32.dll",SetLastError=true)]	
		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 COMPACT_FRAMEWORK
		[DllImport("coredll",SetLastError=true)]
		private static extern bool CloseHandle(
		  int hObject                      // handle to object
		);
#else
       [DllImport("kernel32.dll",SetLastError=true)]

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -