通过MS Comm VB6将数据发送到设备

时间:2015-02-19 19:28:52

标签: vb6

我需要将文件发送到某个电子设备并执行它。

我无法在线找到有关MS Comms的任何信息,而且我在Microsoft(https://msdn.microsoft.com/en-us/library/aa231237(v=vs.60).aspx)上找不到任何有用的文档:

' Send Byte array data
MSComm1.Output = Out

如果你们能给我一些指导并帮助我解决我的问题,那会很棒。我遇到的问题是Loop Until MSComm1.OutBufferCount = 0的无限循环,当我返回" MSComm1.OutBufferCount"在Do和Loop之间" MSComm1.OutBufferCount"是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

1 个答案:

答案 0 :(得分:0)

查看RThreshold和SThreshold属性

以下是一个简单的示例项目:

'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_Click中,我从Text1向命令添加回车符,因为大多数设备都要求命令完成

在MSComm1_OnComm中,我只是将接收的数据打印到标签,但您可能希望将接收的数据添加到全局变量,然后处理该变量的内容,因为可能不会立即接收所有数据