Transmitting and Receiving Binary Data with MSComm ControlID: Q151899
|
This article covers some of the most Frequently Asked Questions (FAQs) about using the Visual Basic 4.0 Communications (MSComm) control for receiving and transmitting binary data. You can find this and other FAQ articles by querying on the keyword "FAQ." You can find additional general references in the Microsoft Knowledge Base by searching on "article list."
Q. Can the MSComm control (MSComm16.OCX and MSComm32.OCX) be used to
transmit and receive binary data?
A. Yes. The MSComm control can be used to transmit and receive binary
data provided neither end of the conversation is using a double byte
character set (DBCS). The binary data must be converted one byte at a
time to a character and transmitted. When received, it must be
converted from a character back into binary data one byte at a time.
The code fragment below illustrates this process.
'WARNING: USE OF THE SAMPLE CODE PROVIDED IN THIS ARTICLE IS AT YOUR
'OWN RISK. Microsoft provides this sample code "as is" without
'warranty of any kind, either express or implied, including but not
'limited to the implied warranties of merchantability and/or fitness
'for a particular purpose.
'Transmitter Code.
Dim FileName As String: FileName = "C:\BinFiles\Test.Exe"
Dim Offset As Long
Dim FileData As Byte
Open FileName For Binary Access Read As #1
For Offset = 1 To FileLen(FileName)
Get #1, Offset, FileData
MSComm1.Output = Chr$(FileData)
Next Offset
Close #1
'Receiver Code.
'Assume That File #1 Has Already Been Opened Elsewhere In The Code
'And That ByteCount Has Been Dimmed As Long And Initialized To 0.
Private Sub MSComm1_OnComm()
Dim TmpStr As String
Dim StrLen As Long, I As Long
Dim FileData As Byte
If MSComm1.CommEvent = comEvReceive Then
While MSComm1.InBufferCount > 0
TmpStr = MSComm1.Input
StrLen = Len(TmpStr)
For I = 1 To StrLen
FileData = CByte(Asc(Mid(TmpStr, I, 1)))
ByteCount = ByteCount + 1
Put #1, ByteCount, FileData
Next I
TmpStr = ""
Wend
End If
End Sub
'Assume That File #1 Is Properly Closed When All Of The Data Is
'Received.
Additional query words: 4.00 vb4win vb4all Comm
Keywords : kbcode PrgCtrlsCus
Version : WINDOWS:4.0
Platform : WINDOWS
Issue type :
Last Reviewed: May 12, 1999