Using system; using system.runtime.interopservices;
Namespace SerialComm {///
When FALSE, the transmitted buffer receives the byte xonchar that represents the buffer has empty and the driver has sent the Xonchar that has been resumed. When Xoff Continues TX Public INTFOTX; //, when the XOFFCHAR is received, the XON / XOFF OUT FLOW Control Public Int Finx; // True will receive the XON / XOFF OUT FLOW Control public int finx; // true, the receiving buffer receives the representative buffer full After Xofflim, XOFFCHAR sends out the receiving buffer to receive Xonlim that represents the empty space, XONCHAR sends out the XON / XOFF IN FLOW Control Public Int FerrorChar; / / This value is specified by ErrorChar member when the fparity is TRUE. When the character enable Error Replacement Public Int Fnull; // ETRUE is received, the RTS is set to the RTS_Control_Disable when receiving the ERROR Replacement Public Int Fnull; // ETRUE. When the RTS_Control_enable, RTS is set to ON RTS_CONTROL_HANDSHAKE, when the received buffer is less than half full, the RTS is ON when the receiving buffer exceeds three-quarters, when the RTS is OFF RTS_CONTROL_TOGGLE, when there is still the remaining byte of the receiving buffer RTS is ON, otherwise the default is OFF * /
Public int foundonerror; // True, there is an error occurs when you interpret your read and write an action abort on error public int fdummy2; // does not use reserved public uint flags; public ushort wreserved; // Not used, must be 0 NOT CURRENTLY USED PUBLIC Ushort xonlim; // Specify the minimum byte number of allowable by XON characters Transmit xon threshold public usort xoflim; / / Specify the number of minimum bytes that can be allowed in this pre-receive buffer in this pre-receive buffer Transmit Xoff Threshold Public Byte Bytesize; / / Specify the currently used data Number of Bits / BYTE, 4-8 Public Byte Parity; // Specify the parity method of the port currently used, possible: EvenParity, MarkParity, Noparity, ODDPARITY 0-4 = NO, ODD, EVEN, MARK, SPACE PUBLIC BYTE STOPBITS; / / Specifies the number of stop bits currently using the port, which may be: OneStopbit, One5stopbits, Twostopbits 0, 1, 2 = 1, 1.5, 2 Public Char Xonchar; / / Specifies the value Tx and Rx Xon Character public char XOFCHAR for sending and receiving character xon; / / Specifying the TX and RX Xoff Character Public Char ErrorChar; / / This character is used instead The value of the received parity error error Error Replacement Character public char eofchar; // When there is no binary mode, This character can be used to indicate the end of the data END OF INPUT CHARACTER PUBLIC CHAR EVTCHAR; / / When this character is received, an event recEIVED Event Character Public Ushort WRESERVED1; // does not use 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, // To open the serial name uint dwdesiredAccess, / / Specify the access method of the serial port, which is typically set to readable writeable int dwsharemode, // Specify the sharing mode of the serial port, the serial port cannot be shared, so set to 0 int LPSecurityAttributes, / / Set the security properties of the serial port, unsupported under Win9x, should be set to null int dwcreationdisposition, // For serial communication, create only the serial port attribute and flag for open_existing int dwflagsandattributes, //, set to file_flag_overlapped (overlap I / O Operation), specifying the serial port in asynchronous manually INT HTEMPLATEFILE / / The serial communication must be set to null); [DLLIMPORT ("kernel32.dll")] private static extern bool getcommstate (int hfile, // communication device handle REF DCB LPDCB // Device Control Block DCB); [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, // communication device handle REF DCB LPDCB // device control block); [DLLIMPORT (" kernel32.dll ")] private static extern bool GetCommTimeouts (int hFile, // handle communication device handle to comm device ref COMMTIMEOUTS lpCommTimeouts // timeout time-out values); [DllImport (" kernel32.dll ")] private static extern Bool setcommtimeouts (int HFile, // Communication Equipment Handle Handle To Comm De "[时 Time-out Values; [DLLIMPORT (" kernel32.dll "
)] Private static extern bool ReadFile (int hFile, // handle communication device handle to file byte [] lpBuffer, // data buffer data buffer int nNumberOfBytesToRead, lpNumberOfBytesRead // wait many bytes read number of bytes to read ref int , // read how many bytes Number of bytes read ref overlapped lpoverlapped // overflow buffer Overlapped buffer; [DLLIMPORT ("kernel32.dll")] Private static extern bool writefile (int HFile, // Communication Equipment handle Handle to file byte [] lpBuffer, // data buffer data buffer int nNumberOfBytesToWrite, // number of bytes written to wait number of bytes to write ref int lpNumberOfBytesWritten, number // how many bytes have been written of bytes written ref OVERLAPPED lpOverlapped // overflow buffer overlapped buffer); [DllImport ( "kernel32.dll")] private static extern bool CloseHandle (int hObject // handle to object); [DllImport ( "kernel32.dll")] private static extern uint GetLastError (); Public void open () {DCB DCBCOMMPORT = New DCB (); CommTIMEOUTS CTOCOMMPORT = New CommTIMEOUTS (); // Open the serial Open the comm. hcomm = createfile (portnum, generic_read | generic_write, 0, 0, open_existing, 0, 0); // Open if the port cannot be opened, open if the serial port is not open Bail out. If (hcomm == invalid_handle_value) {throw ("Illegal operation, unable to open the serial port! "));} // set the communication timeout 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 the serial port set baud rate, Parity, Word Size, And Stop Bits. Getcommstate (hcomm, ref dcbcommport); dcbcommport.baudrate = baudrate; dcbcommport.flags = 0; //dcb.fbinary=1; dcbcommport.flags | = 1; if (Parity> 0) {//dcb.fparity=1 dcbcommport.flags | = 2;} DCBCOMMPORT.PARITY = Parity; dcbcommport.bytesize = bytesize; dcbcommport.stopbits = stopbits; if (! Setcommstate (hcomm, ref DCBCOMMPORT) {// uint errornum = getLastError (); throw ("illegal operation, not open serial port!"));} // unremark to seeing TOOK CORRECTLY / / DCB DCBCMMPORT2 = New DCB (); // GetCommState (hComm, ref dcbCommPort2); Opened = true;} public void Close () {if (hComm = INVALID_HANDLE_VALUE!) {CloseHandle (hComm);}} 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 byte Sread, ref ovlcommport; outbytes = new byte [bytesread]; array.copy (bufbytes, outbytes, bytesread);} else {throw (New ApplicationException ("Serial port is not open! "));} Return OutBytes;} public void Write (byte [] WriteBytes) {if (hComm = INVALID_HANDLE_VALUE) {OVERLAPPED ovlCommPort = new OVERLAPPED ();! Int BytesWritten = 0; WriteFile (hComm, WriteBytes, WriteBytes.Length, Ref byteswritten, ref ovlcommport;} else {throw (New ApplicationException ");}}