JustinIO的vb.NET版本!(串口操作类)

Imports System
Imports 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 As Integer = 3
Private Const INVALID_HANDLE_VALUE As Integer = -1

#Region "struct"

  1<structlayout(layoutkind.sequential)> _   
  2Public Structure DCB   
  3'//taken from c struct in platform sdk   
  4Public DCBlength As Integer '// sizeof(DCB)   
  5Public BaudRate As Integer '// 指定当前波特率 current baud rate   
  6'// these are the c struct bit fields, bit twiddle flag to set   
  7Public fBinary As Integer '// 指定是否允许二进制模式,在windows95中必须主TRUE binary mode, no EOF check   
  8Public fParity As Integer '// 指定是否允许奇偶校验 enable parity checking   
  9Public fOutxCtsFlow As Integer '// 指定CTS是否用于检测发送控制,当为TRUE是CTS为OFF,发送将被挂起。 CTS output flow control   
 10Public fOutxDsrFlow As Integer '// 指定CTS是否用于检测发送控制 DSR output flow control   
 11Public fDtrControl As Integer '// DTR_CONTROL_DISABLE值将DTR置为OFF, DTR_CONTROL_ENABLE值将DTR置为ON, DTR_CONTROL_HANDSHAKE允许DTR"握手" DTR flow control type   
 12Public fDsrSensitivity As Integer '// 当该值为TRUE时DSR为OFF时接收的字节被忽略 DSR sensitivity   
 13Public fTXContinueOnXoff As Integer '// 指定当接收缓冲区已满,并且驱动程序已经发送出XoffChar字符时发送是否停止。TRUE时,在接收缓冲区接收到缓冲区已满的字节XoffLim且驱动程序已经发送出XoffChar字符中止接收字节之后,发送继续进行。 FALSE时,在接收缓冲区接收到代表缓冲区已空的字节XonChar且驱动程序已经发送出恢复发送的XonChar之后,发送继续进行。XOFF continues Tx   
 14Public fOutX As Integer '// TRUE时,接收到XoffChar之后便停止发送接收到XonChar之后将重新开始 XON/XOFF out flow control   
 15Public fInX As Integer '// TRUE时,接收缓冲区接收到代表缓冲区满的XoffLim之后,XoffChar发送出去接收缓冲区接收到代表缓冲区空的XonLim之后,XonChar发送出去 XON/XOFF in flow control   
 16Public fErrorChar As Integer '// 该值为TRUE且fParity为TRUE时,用ErrorChar 成员指定的字符代替奇偶校验错误的接收字符 enable error replacement   
 17Public fNull As Integer '// eTRUE时,接收时去掉空(0值)字节 enable null stripping   
 18Public fRtsControl As Integer '// RTS flow control   
 19'/*RTS_CONTROL_DISABLE时,RTS置为OFF   
 20' * RTS_CONTROL_ENABLE时, RTS置为ON   
 21' * RTS_CONTROL_HANDSHAKE时,   
 22' * 当接收缓冲区小于半满时RTS为ON   
 23' * 当接收缓冲区超过四分之三满时RTS为OFF   
 24' * RTS_CONTROL_TOGGLE时,   
 25' * 当接收缓冲区仍有剩余字节时RTS为ON ,否则缺省为OFF*/   
 26Public fAbortOnError As Integer '// TRUE时,有错误发生时中止读和写操作 abort on error   
 27Public fDummy2 As Integer '// 未使用 reserved   
 28Public flags As Int64   
 29Public wReserved As UInt16 '// 未使用,必须为0 not currently used   
 30Public XonLim As UInt16 '// 指定在XON字符发送这前接收缓冲区中可允许的最小字节数 transmit XON threshold   
 31Public XoffLim As UInt16 '// 指定在XOFF字符发送这前接收缓冲区中可允许的最小字节数 transmit XOFF threshold   
 32Public ByteSize As Byte '// 指定端口当前使用的数据位 number of bits/byte, 4-8   
 33Public Parity As Byte '// 指定端口当前使用的奇偶校验方法,可能为:EVENPARITY,MARKPARITY,NOPARITY,ODDPARITY 0-4=no,odd,even,mark,space   
 34Public StopBits As Byte '// 指定端口当前使用的停止位数,可能为:ONESTOPBIT,ONE5STOPBITS,TWOSTOPBITS 0,1,2 = 1, 1.5, 2   
 35Public XonChar As Char '// 指定用于发送和接收字符XON的值 Tx and Rx XON character   
 36Public XoffChar As Char '// 指定用于发送和接收字符XOFF值 Tx and Rx XOFF character   
 37Public ErrorChar As Char '// 本字符用来代替接收到的奇偶校验发生错误时的值 error replacement character   
 38Public EofChar As Char '// 当没有使用二进制模式时,本字符可用来指示数据的结束 end of input character   
 39Public EvtChar As Char '// 当接收到此字符时,会产生一个事件 received event character   
 40Public wReserved1 As UInt16 '// 未使用 reserved; do not use   
 41End Structure   
 42<structlayout(layoutkind.sequential)> _   
 43Private Structure COMMTIMEOUTS   
 44Public ReadIntervalTimeout As Integer   
 45Public ReadTotalTimeoutMultiplier As Integer   
 46Public ReadTotalTimeoutConstant As Integer   
 47Public WriteTotalTimeoutMultiplier As Integer   
 48Public WriteTotalTimeoutConstant As Integer   
 49End Structure   
 50<structlayout(layoutkind.sequential)> _   
 51Private Structure OVERLAPPED   
 52Public Internal As Integer   
 53Public InternalHigh As Integer   
 54Public Offset As Integer   
 55Public OffsetHigh As Integer   
 56Public hEvent As Integer   
 57End Structure 
 58
 59#End Region 
 60
 61#Region "Windows API"   
 62<dllimport("kernel32.dll", charset:="CharSet.Auto)"> _   
 63Private Shared Function CreateFile( _   
 64ByVal lpFileName As String, _   
 65ByVal dwDesiredAccess As UInt32, _   
 66ByVal dwShareMode As Integer, _   
 67ByVal lpSecurityAttributes As Integer, _   
 68ByVal dwCreationDisposition As Integer, _   
 69ByVal dwFlagsAndAttributes As Integer, _   
 70ByVal hTemplateFile As Integer) As Integer   
 71End Function   
 72'参数介绍:   
 73'lpFileName 要打开的串口名称   
 74'dwDesiredAccess 指定串口的访问方式,一般设置为可读可写方式   
 75'dwShareMode 指定串口的共享模式,串口不能共享,所以设置为0   
 76'lpSecurityAttributes 设置串口的安全属性,WIN9X下不支持,应设为NULL   
 77'dwCreationDisposition 对于串口通信,创建方式只能为OPEN_EXISTING   
 78'dwFlagsAndAttributes 指定串口属性与标志,设置为FILE_FLAG_OVERLAPPED(重叠I/O操作),指定串口以异步方式通信   
 79'hTemplateFile 对于串口通信必须设置为NULL   
 80<dllimport("kernel32.dll")> _   
 81Private Shared Function GetCommState(ByVal hFile As Integer, ByRef lpDCB As DCB) As Boolean   
 82End Function   
 83'参数介绍   
 84'hFile 通信设备句柄   
 85'DCB lpDCB 设备控制块DCB 
 86
 87<dllimport("kernel32.dll")> _   
 88Private Shared Function BuildCommDCB(ByVal lpDef As String, ByRef lpDCB As DCB) As Boolean   
 89End Function   
 90'参数介绍   
 91'lpDef 设备控制字符串   
 92'lpDCB 设备控制块DCB   
 93<dllimport("kernel32.dll")> _   
 94Private Shared Function SetCommState(ByVal hfile As Integer, ByRef lpDCB As DCB) As Boolean   
 95End Function   
 96'参数说明   
 97'hFile 通信设备句柄   
 98'lpDCB 设备控制块   
 99<dllimport("kernel32.dll")> _   
100Private Shared Function GetCommTimeouts(ByVal hfile As Integer, ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean   
101End Function   
102'参数说明   
103'hFile 通信设备句柄 handle to comm device   
104'lpCommTimeouts 超时时间 time-out values   
105<dllimport("kernel32.dll")> _   
106Private Shared Function SetCommTimeouts(ByVal hfile As Integer, ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean   
107End Function   
108'参数说明   
109'hFile 通信设备句柄 handle to comm device   
110'lpCommTimeouts 超时时间 time-out values   
111<dllimport("kernel32.dll")> _   
112Private Shared Function ReadFile(ByVal hFile As Integer, ByVal lpBuffer() As Byte, _   
113ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer, _   
114ByRef lpOverlapped As OVERLAPPED) As Boolean   
115End Function   
116'参数说明   
117'hFile 通信设备句柄 handle to file   
118'lpBuffer 数据缓冲区 data buffer   
119'nNumberOfBytesToRead 多少字节等待读取 number of bytes to read   
120'lpNumberOfBytesRead 读取多少字节 number of bytes read   
121'lpOverlapped 溢出缓冲区 overlapped buffer   
122<dllimport("kernel32.dll")> _   
123Private Shared Function WriteFile(ByVal hFile As Integer, ByVal lpBuffer() As Byte, _   
124ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesWritten As Integer, _   
125ByRef lpOverlapped As OVERLAPPED) As Boolean   
126End Function   
127'参数介绍   
128'hFile 通信设备句柄 handle to file   
129'lpBuffer 数据缓冲区 data buffer   
130'nNumberOfBytesToWrite 多少字节等待写入 number of bytes to write   
131'lpNumberOfBytesWritten 已经写入多少字节 number of bytes written   
132'lpOverlapped 溢出缓冲区 overlapped buffer   
133<dllimport("kernel32.dll")> _   
134Private Shared Function CloseHandle(ByVal hObject As Integer) As Boolean   
135End Function   
136'参数hObject 要被关闭的句柄 handle to object   
137<dllimport("kernel32.dll")> _   
138Private Shared Function GetLastError() As UInt32   
139End Function   
140#End Region   
141Public Sub open()   
142Dim dcbCommPort As New DCB   
143Dim ctoCommPort As New COMMTIMEOUTS   
144'// 打开串口 OPEN THE COMM PORT.   
145hComm = CreateFile(PortNum, UInt32.Parse(GENERIC_READ Or GENERIC_WRITE), 0, 0, OPEN_EXISTING, 0, 0)   
146'// 如果串口没有打开,就打开 IF THE PORT CANNOT BE OPENED, BAIL OUT.   
147If hComm = INVALID_HANDLE_VALUE Then   
148Throw (New ApplicationException("非法操作,不能打开串口!"))   
149End If   
150'// 设置通信超时时间 SET THE COMM TIMEOUTS.   
151GetCommTimeouts(hComm, ctoCommPort) 'ref   
152ctoCommPort.ReadTotalTimeoutConstant = ReadTimeout   
153ctoCommPort.ReadTotalTimeoutMultiplier = 0   
154ctoCommPort.WriteTotalTimeoutMultiplier = 0   
155ctoCommPort.WriteTotalTimeoutConstant = 0   
156SetCommTimeouts(hComm, ctoCommPort) 'ref   
157'// 设置串口 SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.   
158GetCommState(hComm, dcbCommPort) 'ref   
159dcbCommPort.BaudRate = BaudRate   
160dcbCommPort.flags = 0   
161'//dcb.fBinary=1;   
162dcbCommPort.flags = dcbCommPort.flags Or (1)   
163If Parity &gt; 0 Then   
164'//dcb.fParity=1   
165dcbCommPort.flags = dcbCommPort.flags Or (2)   
166End If   
167dcbCommPort.Parity = Parity   
168dcbCommPort.ByteSize = ByteSize   
169dcbCommPort.StopBits = StopBits   
170If SetCommState(hComm, dcbCommPort) = False Then   
171'//uint ErrorNum=GetLastError();   
172Throw (New ApplicationException("非法操作,不能打开串口!"))   
173End If   
174'//unremark to see if setting took correctly   
175'//DCB dcbCommPort2 = new DCB();   
176'//GetCommState(hComm, ref dcbCommPort2);   
177Opened = True   
178End Sub   
179Public Sub Close()   
180If hComm &lt;&gt; INVALID_HANDLE_VALUE Then   
181CloseHandle(hComm)   
182End If   
183End Sub   
184Public Function Read(ByVal NumBytes As Integer) As Byte()   
185Dim BufBytes(NumBytes) As Byte   
186Dim OutBytes(0) As Byte   
187If (hComm &lt;&gt; INVALID_HANDLE_VALUE) Then   
188Dim ovlCommPort As New OVERLAPPED   
189Dim BytesRead As Integer = 0   
190ReadFile(hComm, BufBytes, NumBytes, BytesRead, ovlCommPort)   
191Array.Copy(BufBytes, OutBytes, BytesRead)   
192Else   
193Throw (New ApplicationException("串口未打开!"))   
194End If   
195Return OutBytes   
196End Function 
197
198Public Sub Write(ByVal WriteBytes As Byte())   
199If (hComm &lt;&gt; INVALID_HANDLE_VALUE) Then   
200Dim ovlCommPort As New OVERLAPPED   
201Dim BytesWritten As Integer   
202WriteFile(hComm, WriteBytes, WriteBytes.Length, BytesWritten, ovlCommPort)   
203Else   
204Throw (New ApplicationException("串口未打开!"))   
205End If   
206End Sub   
207End Class   
208End Namespace</dllimport("kernel32.dll")></dllimport("kernel32.dll")></dllimport("kernel32.dll")></dllimport("kernel32.dll")></dllimport("kernel32.dll")></dllimport("kernel32.dll")></dllimport("kernel32.dll")></dllimport("kernel32.dll")></dllimport("kernel32.dll")></dllimport("kernel32.dll",></structlayout(layoutkind.sequential)></structlayout(layoutkind.sequential)></structlayout(layoutkind.sequential)>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus