Serial communication Justinio.cs

zhaozj2021-02-16  103

Using system; using system.runtime.interopservices;

Namespace Justinio {? class commit {

?? public string portnum; ?? public int baudRate; ?? public byte pouttesize; ?? public byte parity; // 0-4 = no, odd, even, mark, space ?? public byte stopbits; // 0, 1 , 2 = 1, 1.5, 2 ?? public int readyunt; ???? // Comm port win32 file handle ?? private int hcomm = -1; ???? public bool open = 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] ?? public struct dcb {???? t Taken from c struct in platform sdk ??? public int dcblength; ??????????? // sizeof (DCB)? ?? public int baudrate; ??????????? // Specify the current baud rate current baud rate ??? // THESE ARE THE C STRUCT BIT FIELDS, BIT Twiddle Flag To set ??? public int fbinary; ????????? // Specify whether to allow binary mode, in Windows 95 must be the primary True Binary Mode, No Eof CHECK ??? public int fparity; ????????? // designated Whether to allow parity Enable Parity Checking ??? public int coutxctsflow; ????? // Specify whether CTS is used to detect transmission control, when TRUE is CTS Off and send will be suspended. CTS OUTPUT FLOW Control ??? public int squatxdsrflow; ????? // Specify whether CTS is used to detect send control DSR OUTPUT FLOW Control ??? public int fdtrControl; ?????? // DTR_Control_Disable value Put DTR For OFF, DTR_CONTROL_ENABLE value sets DTR to ON, DTR_CONTROL_HANDSHAKE allows DTR "Handshake" DTR Flow Control Type ??? Public Int fdsrsensitivity; ?? // When the value is True, the DSR is ignored by the byte is ignored DSR Sensitivity. ??? public int ftxcontinueonxoff; // Specifies whether the transmission is stopped when the receive buffer is full and the driver has sent an Xoffchar character. When True, the transmitted buffer receives the byte Xofflim that is full of the buffer, and the driver has sent the XoffChar character aborted byte, the transmission continues. 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.

XOFF Continues TX ??? public int foundX; ????????? // true, after receiving Xoffchar, stop sending the XON / XOFF OUT FLOW Control ??? after the XONCHAR will be restarted after XONCHAR When the reception buffer receives the XOxlim that represents the XONLIM that represents the buffer, the XONCHAR sends out XON / / Xoff in flow control ??? public int ferror; ???? // This value is True and fparity is True, the character specified by ErrorChar member replaces the parity error reception character Enable Error Replacement ??? Public INT FNULL When receiving the empty (0 value) byte enable null stripping ???????????????????????????????????????????????????????????????????????????????????????????????????????? ????? / * RTS_Control_disable, RTS is set to OFF ??? ???????? RTS_Control_Nable, RTS is set to ON ?????????? RTS_Control_Handshake, ????? ????? When the receiving buffer is less than half full, RTS is ON ??? ???????? When the receiving buffer exceeds more than four-quarters, the RTS is OFF ????????? • When RTS_Control_TogGle, ?????????? RTS is ON when the residual byte is still the remaining byte, otherwise it is OFF * /

??? Public int foundonerror; ?? // True, there is an error to interrupted and write an operation when you have an error, and write an operation.. ??? public int fdummy2; ??????? // unused 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; ???????????? // specified to send this in Xoff characters The minimum byte number of allowable by receiving buffer TRANSMIT XOFF thRESHOLD ??? public byte bytesize; ?????????? // specify the data bit currently used in the port? Number of Bits / Byte, 4- 8 ??? public Byte Parity; ???????????? // Specify the parity method for the currently used in the port, may be: EvenParity, MarkParity, Noparity, Oddparity? 0-4 = no, ODD , EVEN, MARK, SPACE ??? public byte stopbits; ?????????? // Specify the number of stop bits currently used in the port, may be: OneStopbit, One5Stopbits, Twostopbits? 0, 1, 2 = 1 , 1.5, 2 ??? public char xonchar; ??????????? // Specify the value of the value TX AND RX XON Character ??? public char XOn character ??? public char XOFCHAR; ???? ?????? // Specify for sending and receiving character Xoff value Tx and rx xoff character ??? public char error, ????????? // this character is used instead of the received parity The value of the error when the error occurred error. Time, this character can be used to indicate the end of the end of the data End of Input Character ??? public char evtchar; ??????????? // generate an event ReceiveD Event Character when receiving this character? ?? public ushort wreserved1; ???????? // does not use reserved; do not use} ?? [structLayout (layoutkind.sequential] ?? private struct command commands {? ??? public int tentintervaltimeout ??? public int ; ??? public int  ??? public int WriteTotimeoutMultiPlier; ??? 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 port name ??? uint dwdesiredAccess, ??????????????? ?????? // Specify the way the serial port is accessible, generally set to readable writable ??? 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 attribute of the serial port, does not support Under Win9X, should be set to null ??? Int dwcreationDisposition, ?? ????????????? // For serial port communication, create only open_existing ??? Int dwflagsandattributes, ????????????????? Serial port properties and flags, set to file_flag_overlapped, specify the serial port to communicate asynchronously ??? 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, ????????????????? // communication device handle handle to communication lpcommtimeouts? // timeout time TIME-OUT VALUES ??); ??? [DLLIMPORT ("kernel32.dll")] ??? private static extern bool setcommtimeout (??? Int hfile, ????????????????? // communication device handle Handle to Comm De DEVICE ??? Ref strtimeouts lpcommtimeouts? // timeout time Time-out value ??); ?? [DLLIMPORT ("kernel32.dll"

)] ?? private static extern bool readfile (??? Int hfile, ???????????????? // communication device handle handle to file ??? Byte [] lpbuffer, ??? ????????? // data buffer Data Buffer ??? int nNumberofbytestoread ,? // How many bytes wait for reading Number of bytes to read ??? ref int lpnumberofbytesread, // How many bytes read NUMBER OF BYTES READ ??? REF OVERLAPPED LPOVERLAPED ??? // Overflow buffer Overlapped buffer ??); ?? [DLLIMPORT ("kernel32.dll")] ??? private static extern bool writefile (??? Int Hfile , ????????????????????? // communication device handle handle to file ??? Byte [] lpbuffer, ?????????????? ? // Data buffer Data Buffer ??? int nNumberofbytestowrite, ???? // How many bytes Waiting to write Number of bytes to write ??? ref int lpnumberofbyteswritten ,? // How many bytes have been written Number 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 (); ??????????? c OMMTIMEOUTS CTOCOMMPORT = New CommTimeouts (); ??? ??? // Open the serial Open the Comm port. ??????????? hcomm = createfile (portnum, generic_read | generic_write, 0, 0, open_existing 0, 0); ??? // If the serial port is not open, open if the port cannot be opened, bail out.???if(hcomm == invalid_handle_value) ??? {??? ?? throw (New ApplicationException ("illegal operation, can not 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 (HCMM, 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 erroornum = getLastError (); ?????? throw ("illegal operation, can not open the serial port!")); ???} ?? ? // unremark to see if setting TOOK CORRECTLY ??? // DCB DCBCOMMPORT2 = 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]; ??? f (hcomm! = invalid_handle_value) {???? overlapped ovlcommport = new overlapped ();??? ? INTESREAD = 0; ???? readfile (hcomm, bufbytes, numbytes, r Ef bytesread, ref ovlcommport; ???? Outbytes = new byte [bytesread]; ???? array.copy (bufbytes, outbytes, bytesread); ???} ??? else {???? throw (new ApplicationException ("The 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 ("Serial Port is not open!")); ???} ????}?}

转载请注明原文地址:https://www.9cbs.com/read-10358.html

New Post(0)