通过MS Comm VB6向设备发送数据
我需要将文件发送到某个电子设备并执行它 我在网上找不到有关MS Comms的任何信息,也找不到有关Microsoft Comms的任何有用文档:通过MS Comm VB6向设备发送数据,vb6,Vb6,我需要将文件发送到某个电子设备并执行它 我在网上找不到有关MS Comms的任何信息,也找不到有关Microsoft Comms的任何有用文档: ' Send Byte array data MSComm1.Output = Out 如果你们能给我一些建议,帮我解决问题,那就太好了。我遇到的问题是循环中的无限循环,直到MSComm1.burghfercount=0,当我返回Do和循环MSComm1.burghfercount之间的MSComm1.burghfercount时,MSComm1.b
' Send Byte array data
MSComm1.Output = Out
如果你们能给我一些建议,帮我解决问题,那就太好了。我遇到的问题是循环中的无限循环,直到MSComm1.burghfercount=0,当我返回Do和循环MSComm1.burghfercount之间的MSComm1.burghfercount时,MSComm1.burghfercount为0,文件似乎不会发送到设备
我目前得到的最接近的函数如下:
Function SendFile(tmp$)
Dim temp$
Dim hsend, bsize, LF&
' Open file
Open tmp$ For Binary Access Read As #2
' Check size on Mscomm1 OutBuffer
bsize = MSComm1.OutBufferSize
' Check file length
LF& = LOF(2)
' This code makes tiny pieces of data (Buffer sized)
' And send's it
Do Until EOF(2)
If LF& - Loc(2) <= bsize Then
bsize = LF& - Loc(2) + 1
End If
' Make room for some data
temp$ = Space$(bsize)
' Put the data piece in the Temp$ string
Get #2, , temp$
MSComm1.Output = temp$
Do
' Wait until the buffer is empty
Loop Until MSComm1.OutBufferCount = 0
Loop
' close file
Close #2
End Function
请查看RThreshold和Streshold属性 下面是一个简单的示例项目:
'1 form with :
' 1 label control : name=Label1
' 1 textbox control : name=Text1
' 1 command button : name=Command1
Option Explicit
Private Sub Command1_Click()
'send command
MSComm1.Output = Text1.Text & vbCr
End Sub
Private Sub Form_Load()
'config mscomm control and open connection
With MSComm1
.Settings = "9600,N,8,1"
.RThreshold = 1
.SThreshold = 0
.CommPort = 1
.PortOpen = True
End With 'MSComm1
End Sub
Private Sub Form_Resize()
'position controls
Dim sngWidth As Single, sngHeight As Single
Dim sngCmdWidth As Single, sngCmdHeight As Single
Dim sngTxtWidth As Single
Dim sngLblHeight As Single
sngWidth = ScaleWidth
sngHeight = ScaleHeight
sngCmdWidth = 1215
sngCmdHeight = 495
sngLblHeight = sngHeight - sngCmdHeight
sngTxtWidth = sngWidth - sngCmdWidth
Label1.Move 0, 0, sngWidth, sngLblHeight
Text1.Move 0, sngLblHeight, sngTxtWidth, sngCmdHeight
Command1.Move sngTxtWidth, sngLblHeight, sngCmdWidth, sngCmdHeight
End Sub
Private Sub MSComm1_OnComm()
'process received data
Dim strInput As String
Select Case MSComm1.CommEvent
Case comEvReceive
strInput = MSComm1.Input
Label1.Caption = Label1.Caption & strInput
End Select
End Sub
在Command1_中,单击我添加了一个回车符到Text1中的命令,因为大多数设备都要求该命令由Text1完成
在MSComm1_OnComm中,我只是将接收到的数据打印到标签上,但您可能希望将接收到的数据添加到全局变量中,然后处理该变量的内容,因为可能不会一次接收到所有数据您看到了什么样的问题?我不知道如何查看我的命令是否已执行。。。因为我的应用程序在运行发送文件功能时超时,如果在循环中添加一个DoEvents直到MSComm1.exputfferCount=0,会发生什么情况?我仍然在循环中有ExputfferCount返回0和无限循环。无限循环可能是EOF2始终为假?我已尝试添加。Settings=9600,N,8,1.RThreshold=1.SThreshold=0,我想一些数据正在发送过来。我注意到我的屏幕冻结了,大约5-10分钟后什么都没有发生,我已经停止了与通信无关的应用程序,但这是由代码中的一个紧循环引起的。。。我建议你用上面的代码做一个新项目,然后从那里开始构建。。。。首先尝试使用no循环,看看是否有效。。然后开始添加循环也尝试在循环中添加一些DoEvents,并在某处显示循环的进度,例如在表单的标题中打印一个计数器值,以便查看循环是否仍在运行