Transmitting and Receiving Binary Data with MSComm Control

ID: Q151899


The information in this article applies to:


SUMMARY

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."


MORE INFORMATION

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. 



Q. Why can't machines using a double byte character set (DBCS) use the MSComm control (MSComm16.OCX and MSComm32.OCX) to transmit and receive binary data?

A. On DBCS machines (computers running operating system software that uses one of the double byte character sets), binary data will be corrupted if one of the binary values matches a DBCS lead character. The MSComm control will interpret this byte and the following byte as one double byte character and return only one (now incorrect) byte for the "equivalent" ASCII character. For example, lead byte characters, such as &H81 with no trailing byte, are translated to ANSI 0. While this limitation of the MSComm control does not allow binary data to be transmitted and received on DBCS systems, it does allow a standard "text" stream to be transferred.

Q. Does the MSComm control (MSComm16.OCX and MSComm32.OCX) implement any of the popular binary transfer protocols?

A. No. The MSComm control does not implement any binary transfer protocols like XModem/YModem, ZModem, or Compuserve B, for example. You would have to code these protocols yourself or purchase a third-party communication control that implements the desired protocols.

Q. Can the MSComm control (MSComm16.OCX and MSComm32.OCX) accept byte arrays?

A. The MSComm control does not accept byte arrays. This feature is under review and will be considered for inclusion in a future release. The MSComm control's Input and Output properties only work with Strings.

Q. Can Unicode/ANSI conversion be disabled in the MSComm control (MSComm32.OCX)?

A. Unicode/ANSI conversion can not be disabled in the MSComm control. This feature is under review and will be considered for inclusion in a future release. However, this feature would be unnecessary if the MSComm control would allow the use of byte arrays to transmit and receive binary data. (See question 4 above.)


Q. Are the ComInput and ComOutput functions still available in the Visual Basic 4.0 MSComm control (MSComm16.OCX and MSComm32.OCX)?

A. No. The ComInput and ComOutput functions are no longer available in the Visual Basic 4.0 MSComm control. Since NULLS can be transmitted or received properly, as long as the NullDiscard property is set to FALSE, and C programmers can access all of the properties and methods of the .OCX controls, there is no longer a need for these functions in the MSComm control.


Q. Can the MSComm control (MSComm16.OCX and MSComm32.OCX) send binary data at a baud rate greater than 9600 baud?

A. Yes. The MSComm control is capable of using baud rates up to and including 28,800 baud.

Additional query words: 4.00 vb4win vb4all Comm


Keywords          : kbcode PrgCtrlsCus 
Version           : WINDOWS:4.0
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: May 12, 1999