16,554
社区成员
发帖
与我相关
我的任务
分享
Public Declare Auto Function WriteFile Lib "kernel32.dll" (ByVal hFile As Integer, ByVal Buffer As Byte(), ByVal nNumberOfBytesToWrite As Integer, ByRef lpNumberOfBytesWritten As Integer, ByRef lpOverlapped As Overlapped) As Integer
Public Declare Auto Function ReadFile Lib "kernel32.dll" (ByVal hFile As Integer, ByVal Buffer As Byte(), ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer, ByRef lpOverlapped As Overlapped) As Integer
<StructLayout(LayoutKind.Sequential, Pack:=1)> Public Structure OVERLAPPED
Public Internal As Integer
Public InternalHigh As Integer
Public Offset As Integer
Public OffsetHigh As Integer
Public hEvent As Integer
End Structure
'******************************************
'将字节数据传送出去
'重载函数之一,需传入字节数组
'******************************************
Public Sub Write(ByVal Buffer As Byte())
Dim iBytesWritten, iRc As Integer
Dim WriteOverlapped As OVERLAPPED
' WriteOverlapped.Internal = 0
'---------------------------------
If mhRS = -1 Then
Throw New ApplicationException("必须先打开通信端口,才能作输出。")
Else
Try
iRc = WriteFile(mhRS, Buffer, Buffer.Length, iBytesWritten, WriteOverlapped) '输出数组
If iRc = 0 Then
Throw New ApplicationException("传输失败 已传输" & iBytesWritten.ToString & " Bytes/传输" & Buffer.Length.ToString & "Bytes")
End If
Catch Ex As Exception
MsgBox(Ex.ToString)
End Try
End If
End Sub
'******************************************
'将字节数据传送出去
'重载函数之一,需传入字符串
'首先将字符串分解成数组,以便WriteFile函数可以使用
'******************************************
Public Sub Write(ByVal Buffer As String)
Dim oEncoder As New System.Text.UTF8Encoding
Dim aByte() As Byte = oEncoder.GetBytes(Buffer)
Write(aByte) '调用另一个传输的重载函数
End Sub
'******************************************
'将字节数据接收进来
'重载函数之一,第一个参数是读取的字节数,第二个参数是返回的字节数组
'如果传入的参数使用ByVal,原始的数据将不会被更改,故在此需用ByRef
'在调用的地方才能得到此函数所在返回的结果
'******************************************
Public Sub Read(ByRef Bytes2Read As Integer, ByRef InputByte() As Byte)
Dim iReadChars, iRc, lpErrors As Integer
Dim mabtRxBuf As Byte() '接收缓冲区
Dim ReadOverlapped As OVERLAPPED
'设置读取数据的长度
If mhRS = -1 Then
Throw New ApplicationException("尚未打开通信端口,不能读取数据!")
Else
'读取字节
Try
Dim cs As COMSTAT
ClearCommError(mhRS, lpErrors, cs) '取得缓冲区的字节数
iReadChars = cs.cbInQue
ReDim mabtRxBuf(iReadChars - 1)
iRc = ReadFile(mhRS, mabtRxBuf, cs.cbInQue, iReadChars, ReadOverlapped )
If iRc = 0 Then
'读取错误
Throw New ApplicationException("读取错误: " & iRc.ToString)
Else
Bytes2Read = iReadChars
InputByte = mabtRxBuf
End If
Catch Ex As Exception
'其他的读取错误状态
Throw New ApplicationException("读取错误: " & Ex.Message, Ex)
End Try
End If
End Sub
Public Declare Auto Function WriteFile Lib "kernel32.dll" (ByVal hFile As Integer, ByVal Buffer As Byte(), ByVal nNumberOfBytesToWrite As Integer, ByRef lpNumberOfBytesWritten As Integer, ByRef lpOverlapped As Integer) As Integer
Public Declare Auto Function ReadFile Lib "kernel32.dll" (ByVal hFile As Integer, ByVal Buffer As Byte(), ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer, ByRef lpOverlapped As Integer) As Integer
'******************************************
'将字节数据传送出去
'重载函数之一,需传入字节数组
'******************************************
Public Sub Write(ByVal Buffer As Byte())
Dim iBytesWritten, iRc As Integer
Dim WriteOverlapped As OVERLAPPED
' WriteOverlapped.Internal = 0
'---------------------------------
If mhRS = -1 Then
Throw New ApplicationException("必须先打开通信端口,才能作输出。")
Else
Try
iRc = WriteFile(mhRS, Buffer, Buffer.Length, iBytesWritten, Nothing) '输出数组
If iRc = 0 Then
Throw New ApplicationException("传输失败 已传输" & iBytesWritten.ToString & " Bytes/传输" & Buffer.Length.ToString & "Bytes")
End If
Catch Ex As Exception
MsgBox(Ex.ToString)
End Try
End If
End Sub
'******************************************
'将字节数据传送出去
'重载函数之一,需传入字符串
'首先将字符串分解成数组,以便WriteFile函数可以使用
'******************************************
Public Sub Write(ByVal Buffer As String)
Dim oEncoder As New System.Text.UTF8Encoding
Dim aByte() As Byte = oEncoder.GetBytes(Buffer)
Write(aByte) '调用另一个传输的重载函数
End Sub
'******************************************
'将字节数据接收进来
'重载函数之一,第一个参数是读取的字节数,第二个参数是返回的字节数组
'如果传入的参数使用ByVal,原始的数据将不会被更改,故在此需用ByRef
'在调用的地方才能得到此函数所在返回的结果
'******************************************
Public Sub Read(ByRef Bytes2Read As Integer, ByRef InputByte() As Byte)
Dim iReadChars, iRc, lpErrors As Integer
Dim mabtRxBuf As Byte() '接收缓冲区
'设置读取数据的长度
If mhRS = -1 Then
Throw New ApplicationException("尚未打开通信端口,不能读取数据!")
Else
'读取字节
Try
Dim cs As COMSTAT
ClearCommError(mhRS, lpErrors, cs) '取得缓冲区的字节数
iReadChars = cs.cbInQue
ReDim mabtRxBuf(iReadChars - 1)
iRc = ReadFile(mhRS, mabtRxBuf, cs.cbInQue, iReadChars, 0)
If iRc = 0 Then
'读取错误
Throw New ApplicationException("读取错误: " & iRc.ToString)
Else
Bytes2Read = iReadChars
InputByte = mabtRxBuf
End If
Catch Ex As Exception
'其他的读取错误状态
Throw New ApplicationException("读取错误: " & Ex.Message, Ex)
End Try
End If
End Sub
'******************************************
'将字符串数据接收进来
'重载函数之一,第一个参数是读取的字节数,第二个参数是返回的字符串
'如果传入的参数使用ByVal,原始的数据将不会被更改,故在此需用ByRef
'在调用的地方才能得到此函数所返回的结果
'******************************************
Public Sub Read(ByRef Char2Read As Integer, ByRef InputString As String)
Dim iReadChars, iRc, lpErrors As Integer
Dim mabtRxBuf As Byte() '接收缓冲区
'设置读取数据的长度
If mhRS = -1 Then
Throw New ApplicationException("尚未打开通信端口,不能读取数据!")
Else
'读取字节
Try
Dim cs As COMSTAT
ClearCommError(mhRS, lpErrors, cs) '取得缓冲区内的字节数
iReadChars = cs.cbInQue
ReDim mabtRxBuf(iReadChars - 1)
iRc = ReadFile(mhRS, mabtRxBuf, cs.cbInQue, iReadChars, Nothing)
If iRc = 0 Then
'读取错误
Throw New ApplicationException("读取错误:" & iRc.ToString)
Else
Char2Read = iReadChars
'转换字节数组为字符串
Dim oEncoder As New System.Text.UTF8Encoding
InputString = oEncoder.GetString(mabtRxBuf)
End If
Catch Ex As Exception
'其他的读取错误状态
Throw New ApplicationException("读取错误:" & Ex.Message, Ex)
End Try
End If
End Sub