Option ExplicitPrivate Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
(ByVal lpFileName As String, ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, ByVal lpSECURITYATTRIBUTES As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As LongPrivate Declare Function ReadFile Lib "kernel32" _
(ByVal hFile As Long, ByRef lpBuffer As Any, _
ByVal nNumberOfBytesToRead As Long, _
ByRef lpNumberOfBytesRead As Long, _
ByVal lpOverlapped As Long) As LongPrivate Declare Function WriteFile Lib "kernel32" _
(ByVal hFile As Long, ByRef lpBuffer As Any, _
ByVal nNumberOfBytesToWrite As Long, _
ByRef lpNumberOfBytesWritten As Long, _
ByVal lpOverlapped As Long) As LongPrivate Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Const GENERIC_WRITE = &H40000000
Private Const GENERIC_READ = &H80000000
Private Const OPEN_EXISTING = 3
Private Const PURGE_RXABORT = &H2
Private Const PURGE_RXCLEAR = &H8
Private Const PURGE_TXABORT = &H1
Private Const PURGE_TXCLEAR = &H4
Private Const INVALID_HANDLE_VALUE = -1Private nHand As Long '句柄Private Const sFile As String = "C:\123.txt"Private Sub Command1_Click() nHand = CreateFile(sFile, GENERIC_WRITE Or GENERIC_READ, _
0&, ByVal 0&, OPEN_EXISTING, 0&, 0&)
If nHand = INVALID_HANDLE_VALUE Then Exit Sub Dim blnErr As Long
Dim sSend As String
Dim lenBuf As Long '存放将要写入串口的数据长度 Dim NumberWritten As Long
Dim Ret&, mem() As Byte
Randomize
sSend = "中华人民共和国" & Rnd
mem = StrConv(sSend, vbFromUnicode)
lenBuf = UBound(mem) + 1
Ret = WriteFile(ByVal nHand, mem(0), lenBuf, NumberWritten, ByVal 0&)
CloseHandle nHand
Debug.Print "Write", nHand, NumberWritten, sSend
End SubPrivate Sub Command2_Click() nHand = CreateFile(sFile, GENERIC_WRITE Or GENERIC_READ, _
0&, ByVal 0&, OPEN_EXISTING, 0&, 0&)
If nHand = INVALID_HANDLE_VALUE Then Exit Sub
Dim sRecv As String
Dim binRecv() As Byte Dim lResult As Long
Dim NumToRead As Long
Dim NumhaveRead As Long NumToRead = 1024
ReDim binRecv(0 To NumToRead - 1)
lResult = ReadFile(nHand, binRecv(0), NumToRead, _
NumhaveRead, ByVal 0&)
CloseHandle nHand
If NumhaveRead = 0 Then Exit Sub
sRecv = StrConv(binRecv, vbUnicode)
sRecv = Left(sRecv, InStr(sRecv, Chr(0)) - 1)
Debug.Print "Read", nHand, NumhaveRead, sRecv
End SubPrivate Sub Form_Load()
If Dir(sFile) = "" Then
Open sFile For Output As #1
Print #1, Now
Close #1
End If
End Sub
(ByVal lpFileName As String, ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, ByVal lpSECURITYATTRIBUTES As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As LongPrivate Declare Function ReadFile Lib "kernel32" _
(ByVal hFile As Long, ByRef lpBuffer As Any, _
ByVal nNumberOfBytesToRead As Long, _
ByRef lpNumberOfBytesRead As Long, _
ByVal lpOverlapped As Long) As LongPrivate Declare Function WriteFile Lib "kernel32" _
(ByVal hFile As Long, ByRef lpBuffer As Any, _
ByVal nNumberOfBytesToWrite As Long, _
ByRef lpNumberOfBytesWritten As Long, _
ByVal lpOverlapped As Long) As LongPrivate Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Const GENERIC_WRITE = &H40000000
Private Const GENERIC_READ = &H80000000
Private Const OPEN_EXISTING = 3
Private Const PURGE_RXABORT = &H2
Private Const PURGE_RXCLEAR = &H8
Private Const PURGE_TXABORT = &H1
Private Const PURGE_TXCLEAR = &H4
Private Const INVALID_HANDLE_VALUE = -1Private nHand As Long '句柄Private Const sFile As String = "C:\123.txt"Private Sub Command1_Click() nHand = CreateFile(sFile, GENERIC_WRITE Or GENERIC_READ, _
0&, ByVal 0&, OPEN_EXISTING, 0&, 0&)
If nHand = INVALID_HANDLE_VALUE Then Exit Sub Dim blnErr As Long
Dim sSend As String
Dim lenBuf As Long '存放将要写入串口的数据长度 Dim NumberWritten As Long
Dim Ret&, mem() As Byte
Randomize
sSend = "中华人民共和国" & Rnd
mem = StrConv(sSend, vbFromUnicode)
lenBuf = UBound(mem) + 1
Ret = WriteFile(ByVal nHand, mem(0), lenBuf, NumberWritten, ByVal 0&)
CloseHandle nHand
Debug.Print "Write", nHand, NumberWritten, sSend
End SubPrivate Sub Command2_Click() nHand = CreateFile(sFile, GENERIC_WRITE Or GENERIC_READ, _
0&, ByVal 0&, OPEN_EXISTING, 0&, 0&)
If nHand = INVALID_HANDLE_VALUE Then Exit Sub
Dim sRecv As String
Dim binRecv() As Byte Dim lResult As Long
Dim NumToRead As Long
Dim NumhaveRead As Long NumToRead = 1024
ReDim binRecv(0 To NumToRead - 1)
lResult = ReadFile(nHand, binRecv(0), NumToRead, _
NumhaveRead, ByVal 0&)
CloseHandle nHand
If NumhaveRead = 0 Then Exit Sub
sRecv = StrConv(binRecv, vbUnicode)
sRecv = Left(sRecv, InStr(sRecv, Chr(0)) - 1)
Debug.Print "Read", nHand, NumhaveRead, sRecv
End SubPrivate Sub Form_Load()
If Dir(sFile) = "" Then
Open sFile For Output As #1
Print #1, Now
Close #1
End If
End Sub
我在ReadFile那里,仍然使用的是Byte数组,需要说明的是,如果NumtoRead为1024的时候,我的程序就不响应了,因此,在这里我让然保留了自己原来的从recComState.cbInQue 获取的值。从 zhuangbx220(星) 的回复里,我对Byte数组又熟悉了一下,尽管以前也明白这个东东。(转换时候vbFromUnicode),使得原来的字节数纷纷扩大了2倍:)
为此,我决定给 zhuangbx220(星) 40分,表示谢意。 对于WriteFile,我没有采用Byte数组的方法,仍然使用字符串,只是我开始忘记了在传递字符串参数的时候写上ByVal了。以前在买的程序员大本营上对zyl910(910:分儿,我来了!) 的回复很有印象,这次又蒙他提醒。为WriteFile函数加上关键字ByVal后,就可以了(否则传递的是指针的指针,呵呵)。谢谢zyl910(910:分儿,我来了!) 。我送上60分给你,表示谢意。 谢谢各位热心的朋友
这算是作弊吗?