VB.NET使用readfile和writefile读写串口数据问题

sgmcumt 2016-09-22 04:25:16
如题我引用win32中的读写函数对串口进行读写,总是出现读写错误,如图

相关代码如下
 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

其中最后一项定义的Overlapped结构,定义如下
    <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

运行之后,无法读写数据,如果把overlapped更改为integer就可以,代码如下
    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
...全文
965 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
水哥阿乐 2016-10-02
  • 打赏
  • 举报
回复
先把该教程示例的所有只读属性去掉再调试,如果出现问题,请贴出具体出错行代码,别来这整面的
sgmcumt 2016-09-22
  • 打赏
  • 举报
回复
请自行忽略代码中的<strong> </strong>,本来是想加粗显示,不知道为什么自动添加的<strong> </strong>

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧