Imports Systemimports System.Runtime.InteropServices
Namespace JustinIO Class CommPort Public PortNum As String Public BaudRate As Integer Public ByteSize As Byte Public Parity As Byte '// 0-4 = no, odd, even, mark, space Public StopBits As Byte' // 0,1,2 = 1 , 1.5, 2 Public ReadTimeout As Integer '// comm port win32 file handle Private hComm As Integer = -1 Public Opened As Boolean = False' // win32 api constants Private Const GENERIC_READ As Int64 = & H80000000 Private Const GENERIC_WRITE As Int64 = & H40000000 Private Const open_existing asset = 3 private const invalid_handle_value as integer = -1
#Region "Struct"
When Xoff Continues TX Public Foutx As INTEGER '// True, the received buffer receives the representative buffer when the XON / XOFF OUT FLOW Control public finx as integer' // true is stopped after receiving XOONCHAR. After the Xofflim of the zone, XOFCCHAR sends out the reception buffer to receive Xonlim that represents the empty space, XONCHAR sends out the XON / XOFF IN FLOW Control public ferrorchar as integer '// This value is TRUE and fparity is True, using ErrorChar Members Specify the Character Enable Error Replacement Public Fnull As INTEGER '// ETRUE in the parity error error, receive the empty (0 value) byte Enable Null Stripping Public Flow Control AS INTEGER' // RTS Flow Control '/ * RTS_CONTROL_DISABLE, RTS is set to OFF '* RTS_CONTROL_ENABLE, when RTS is set to ON' * RTS_CONTROL_HANDSHAKE, '* When the receiving buffer is less than half full, RTS is ON' * When the receive buffer exceeds more than four-quarters of RTS When Off '* RTS_CONTROL_TOGGLE,' * When the remaining byte remains in the receiving buffer, RTS is ON, otherwise, when the default is OFF * / PUBLIC FABORTONERROR AS INTEGER '// True, there is an error to inter-read and write Operation ABORT ON ERROR PUBLIC FDUMMY2 AS INTEGER '// Not Used Reserved Public Flags AS INT64 PUBLIC WRESERVED AS UINT16' // Not Used, must be 0 NOT CURRENTLY USED PUBLIC XONLIM AS uint16 '// Specify the front reception buffer in XON characters The minimum byte of the area in the area Transmit Xon Threshold Public Xofflim as uint16 '// Specifies that the minimum byte number of allowable minimum bytes in this pre-receiving buffer transmit Xoff threshold public Bytesize as byte' // Specify port current Data Bit Number Of Bits / Byte, 4-8 Public Parity As Byte '// Specifies the parity method for the currently used in the port, which may be: EVENPARITY, MarkParity, Noparity, Oddparity 0-4 =
NO, ODD, EVEN, MARK, SPACE PUBLIC STOPBITS AS BYTE '/ / Specifies the number of stop bits currently used by ports, may be: onestopbit, one5stopbits, twostopbits 0, 1, 2 = 1, 1.5, 2 public xonchar as char' / / Specifies the value of the value TX and RX XON Character public Xoffchar as char 'to send and receive characters xon specified to send and receive character xoff values Tx and rx xoff character public errorchar as char' // this character is used instead of reception When the parity error occurred, the value of Error Replacement Character Public Error AS Char '// This character can be used to indicate the end of the data end of the data to indicate the end of infut character public evtchar as char' //. when the character, generates an event received event character Public wReserved1 As UInt16 '// unused reserved; do not use End Structure
#Region "Windows API"
_ Private Shared Function WriteFile (ByVal hFile As Integer, ByVal lpBuffer () As Byte, _ ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesWritten As Integer, _ ByRef lpOverlapped As OVERLAPPED) As Boolean End Function 'parameters introduced' hFile communication device handles handle to file how many bytes 'lpBuffer data buffer data buffer' nNumberOfBytesToWrite waiting write number of bytes to write 'lpNumberOfBytesWritten number of bytes already written number of bytes written' lpOverlapped buffer overflow overlapped buffer
0 SetCommTimeouts (hComm, ctoCommPort) 'ref' // set the serial SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS. GetCommState (hComm, dcbCommPort) 'ref dcbCommPort.BaudRate = BaudRate dcbCommPort.flags = 0' // dcb. fBinary = 1; dcbCommPort.flags = dcbCommPort.flags Or (1) If Parity> 0 Then '//dcb.fParity=1 dcbCommPort.flags = dcbCommPort.flags Or (2) End If dcbCommPort.Parity = Parity dcbCommPort.ByteSize = ByteSize dcbCommPort.StopBits = StopBits If SetCommState (hComm, dcbCommPort) = False Then '// uint ErrorNum = GetLastError (); Throw (New ApplicationException ( "! illegal operation, can not open the serial port")) End If' // unremark to see IF setting took correctly '// DCB DCBCOMMPORT2 = New DCB ();' // getcommstate (hcomm, ref dcbcommport2); Opened = true end sub public SUB C lose () If hComm <> INVALID_HANDLE_VALUE Then CloseHandle (hComm) End If End Sub Public Function Read (ByVal NumBytes As Integer) As Byte () Dim BufBytes (NumBytes) As Byte Dim OutBytes (0) As Byte If (hComm <> INVALID_HANDLE_VALUE ) Then Dim ovlCommPort As New OVERLAPPED Dim BytesRead As Integer = 0 ReadFile (hComm, BufBytes, NumBytes, BytesRead, ovlCommPort) Array.Copy (BufBytes, OutBytes, BytesRead) Else Throw (New ApplicationException ( "
The serial port is not open! ")) End If Return OutBytes End FunctionPublic Sub Write (ByVal WriteBytes As Byte ()) If (hComm <> INVALID_HANDLE_VALUE) Then Dim ovlCommPort As New OVERLAPPED Dim BytesWritten As Integer WriteFile (hComm, WriteBytes, WriteBytes.Length, BytesWritten, ovlCommPort) Else Throw ("Serial Port Unmopted!")) End if End Sub End Classend Namespace