Solved

Visual Basic

Posted on 1998-09-11
10
448 Views
Last Modified: 2013-11-25
CAn anyone tell me how to connect the MSComm32.ocx to access the modem? Like the program is given in the example but is does not say anything abt the MSComm32.ocx being connected to the form to make it work. How to work with VB5 using serial ports something like a modem
0
Comment
Question by:PraKash
  • 5
  • 5
10 Comments
 
LVL 14

Accepted Solution

by:
waty earned 50 total points
ID: 1434295
HOWTO: Use MSCOMM32.OCX to Transfer Data on DBCS Windows
Last reviewed: July 3, 1997
Article ID: Q158008  
The information in this article applies to:
Microsoft Visual Basic Control Creation, Learning, Professional, and Enterprise Editions for Windows, version 5.0
Professional and Enterprise Editions of Microsoft Visual Basic, 16-bit and 32-bit, for Windows, version 4.0


SUMMARY
This article illustrates some techniques using the MSComm32.OCX control for receiving and transmitting binary data on double-byte character set (DBCS) Windows.



MORE INFORMATION
The Communications control, MSCOMM32.OCX, provides serial communications for your application by allowing the transmission and reception of data through a serial port where only a string of characters to the transmit buffer is permitted. This feature limits you to transmitting only text- based files.

The binary data must be converted one byte at a time to a character and then transmitted. When received, the data must be converted from a character back into binary data one byte at a time. See the REFERENCES section below for more information.

On double-byte character set Windows, such as Korean, Japanese, and Chinese Windows, 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 byte for the equivalent ASCII character. To resolve this problem, which is specific to DBCS Windows, you can implement certain encoding algorithms that can convert binary data into text data and vice versa. This article demonstrates how to convert one byte binary data into ASCII size of three characters to preserve the lead character. On the receiving end, these three ASCII characters are converted back into one-byte binary data. For example:


   ASC('a') = 97  ' 97

would be transmitted as three characters:

   '0''9''7'

NOTE: The sample code is reading the input file as binary. It is not necessary to convert each character to ASCII value.
If you use a modem instead of RS-232 cable, you need to add some codes to establish the modem connection. For example:

MSComm1.Output = 'ATDT 999-9999' for Transmitter
and -
MSComm1.Output = 'AT' for Receiver

Once the connection is established, everything should work the same way.
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.



Sample Code - Transmitter

Start Visual Basic. If Visual Basic is already running, from the File menu, choose New Project. Form1 will be created by default.

Place Microsoft Comm Control on Form1. If Microsoft Comm Control cannot be found in the control bar, go to Tools menu, choose Custom Controls, and then check the Microsoft Comm Control on the List Box.

Add the following code to the General Declarations section of Form1:


      ' General Declaration section


       Dim Offset As Long
       Dim FileData As Byte
       Dim FileName As String

Add the following code to the Load event of Form1:


      ' Load event of Form
      ' 28800 baud, no parity, 8 data, and 1 stop bit.
      ' In RS-232, maximum speed of 28800 baud can be used


      Private Sub Form_Load()
          MSComm1.Settings = "28800,N,8,1"
          MSComm1.InputLen = 1
          MSComm1.CommPort = 2
          MSComm1.PortOpen = True

      ' Change the file path to point to the file you want to transfer
          FileName = "d:\sample.hlp"

          Offset = 1
          Open FileName For Binary Access Read As #1
      End Sub


Add the following code to the OnComm event of MSComm1:


      ' ONCOMM event of Comm OLE Control
      ' The following code is needed to make sure that next set of
      ' characters can be transmitted (CommEvent = 2 is triggered from the
      ' receiver side)


      Private Sub MSComm1_OnComm()
          If MSComm1.CommEvent = 2 Then
              temp = MSComm1.Input
              If Offset <= FileLen(FileName) Then
                  Get #1, Offset, FileData
                  q = FileData
                  MSComm1.Output = Format(q, "000")
                  Offset = Offset + 1
              End If
          End If
      End Sub


Add the following code to the UnLoad event of Form1:


      ' UnLoad event of Form


      Private Sub Form_Unload(Cancel As Integer)
          Close #1
      End Sub


Make sure that the Properties of MSComm1 are changed as follows:

      RThreshold = 1     ' triggers when at least one char is on the buffer
      SThreshold = 3

Sample Code - Receiver

Start Visual Basic. If Visual Basic is already running, from the File menu, choose New Project. Form1 will be created by default.

Place Microsoft Comm Control on Form1. If Microsoft Comm Control cannot be found in the control bar, go to Tools menu, choose Custom Controls, and then check the Microsoft Comm Control on the List Box.

Add a CommandButton to Form1. Command1 is created by default.

Add the following code to the General Declarations section of Form1:


      ' General Declaration section


      Dim ByteCount As Long
      Dim FileData As Byte

Add the following code to the Load event of Form1:


      ' Load event of Form
      ' 28800 baud, no parity, 8 data, and 1 stop bit.


      Private Sub Form_Load()
          MSComm1.Settings = "28800,N,8,1"
          MSComm1.InputLen = 3
          MSComm1.CommPort = 1
          MSComm1.PortOpen = True

      ' Change the file path to point to the file you want to store
          Open "c:\sample.txt" For Binary Access Write As #1

      End Sub


Add the following code to the OnComm event of MSComm1:


      ' OnComm event


      Private Sub MSComm1_OnComm()
          If MSComm1.CommEvent = 2 And MSComm1.InBufferCount > 0 Then
              FileData = CInt(MSComm1.Input)
              ByteCount = ByteCount + 1
              Put #1, ByteCount, FileData
              MSComm1.Output = Chr$(26)
          End If
      End Sub


Add the following code to the Click event of Command1:


      ' CLICK event of button


      Private Sub Command1_Click()
          MSComm1.Output = Chr$(26)
      End Sub


Add the following code to the UnLoad event of Form1:


      ' UnLoad even of Form


      Private Sub Form_Unload(Cancel As Integer)
          Close #1
      End Sub


Make sure that the Properties of MSComm1 are changed as follows:


      RThreshold = 3

The Transmitter and Receiver codes should be compiled separately and placed on two separate machines where one machine is the designated transmitter and the other one is the receiver.
The event-driven technique generates an OnComm event when there are characters waiting in the input buffer. Also, the CommEvent property will contain a numeric 2. For the OnComm event to be triggered, you must set the Rthreshold property to a value other than its default of zero. The most common setting for the RThreshold property is 1, meaning that the OnComm event is triggered if a minimum of one character is waiting in the input buffer. In this article, the numeric 3 is being used as a value of RThreshold property because 3 characters at a time are being sent.
 

0
 
LVL 14

Expert Comment

by:waty
ID: 1434296

FAQ:Transmitting and Receiving Binary Data with MSComm Control
Last reviewed: June 5, 1996
Article ID: Q151899  
The information in this article applies to:
Professional and Enterprise Editions of Microsoft Visual Basic, 16-bit and 32-bit, for Windows, version 4.0


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.
 

0
 

Author Comment

by:PraKash
ID: 1434297
Dear waty I tried all this but it says no object declared for MSComm1 it always gives me this 424 object not found error please help me out
0
 
LVL 14

Expert Comment

by:waty
ID: 1434298
Did you add the reference to the OCX MSComm32.ocx?
0
 

Author Comment

by:PraKash
ID: 1434299
No and how do I do that and where?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:PraKash
ID: 1434300
I did that now but still am gettin the same error
424 error please help me
I increased the priority to 5
0
 
LVL 14

Expert Comment

by:waty
ID: 1434301
Does you deigned ocx on your form called MSComm1?
0
 

Author Comment

by:PraKash
ID: 1434302
No I suppose and how do I do that?
Like as u said I added microsoft comm control on the components and also in the reference what else?
Should the form be an activeX form or can be done on a normal form itself?
How do I design an ocx on my form?
0
 
LVL 14

Expert Comment

by:waty
ID: 1434303
Start Visual Basic. If Visual Basic is already running, from the File menu, choose New Project. Form1 will be created by default.

Place Microsoft Comm Control on Form1. If Microsoft Comm Control cannot be found in the control bar, go to Tools menu, choose Custom Controls, and then check the Microsoft Comm Control on the List Box.

Add the following code to the General Declarations section of Form1:


      ' General Declaration section


       Dim Offset As Long
       Dim FileData As Byte
       Dim FileName As String

Add the following code to the Load event of Form1:


      ' Load event of Form
      ' 28800 baud, no parity, 8 data, and 1 stop bit.
      ' In RS-232, maximum speed of 28800 baud can be used


      Private Sub Form_Load()
          MSComm1.Settings = "28800,N,8,1"
          MSComm1.InputLen = 1
          MSComm1.CommPort = 2
          MSComm1.PortOpen = True

      ' Change the file path to point to the file you want to transfer
          FileName = "d:\sample.hlp"

          Offset = 1
          Open FileName For Binary Access Read As #1
      End Sub


Add the following code to the OnComm event of MSComm1:


      ' ONCOMM event of Comm OLE Control
      ' The following code is needed to make sure that next set of
      ' characters can be transmitted (CommEvent = 2 is triggered from the
      ' receiver side)


      Private Sub MSComm1_OnComm()
          If MSComm1.CommEvent = 2 Then
              temp = MSComm1.Input
              If Offset <= FileLen(FileName) Then
                  Get #1, Offset, FileData
                  q = FileData
                  MSComm1.Output = Format(q, "000")
                  Offset = Offset + 1
              End If
          End If
      End Sub


Add the following code to the UnLoad event of Form1:


      ' UnLoad event of Form


      Private Sub Form_Unload(Cancel As Integer)
          Close #1
      End Sub


Make sure that the Properties of MSComm1 are changed as follows:

      RThreshold = 1     ' triggers when at least one char is on the buffer
      SThreshold = 3

Sample Code - Receiver

Start Visual Basic. If Visual Basic is already running, from the File menu, choose New Project. Form1 will be created by default.

Place Microsoft Comm Control on Form1. If Microsoft Comm Control cannot be found in the control bar, go to Tools menu, choose Custom Controls, and then check the Microsoft Comm Control on the List Box.

Add a CommandButton to Form1. Command1 is created by default.

Add the following code to the General Declarations section of Form1:


      ' General Declaration section


      Dim ByteCount As Long
      Dim FileData As Byte

Add the following code to the Load event of Form1:


      ' Load event of Form
      ' 28800 baud, no parity, 8 data, and 1 stop bit.


      Private Sub Form_Load()
          MSComm1.Settings = "28800,N,8,1"
          MSComm1.InputLen = 3
          MSComm1.CommPort = 1
          MSComm1.PortOpen = True

      ' Change the file path to point to the file you want to store
          Open "c:\sample.txt" For Binary Access Write As #1

      End Sub


Add the following code to the OnComm event of MSComm1:


      ' OnComm event


      Private Sub MSComm1_OnComm()
          If MSComm1.CommEvent = 2 And MSComm1.InBufferCount > 0 Then
              FileData = CInt(MSComm1.Input)
              ByteCount = ByteCount + 1
              Put #1, ByteCount, FileData
              MSComm1.Output = Chr$(26)
          End If
      End Sub


Add the following code to the Click event of Command1:


      ' CLICK event of button


      Private Sub Command1_Click()
          MSComm1.Output = Chr$(26)
      End Sub


Add the following code to the UnLoad event of Form1:


      ' UnLoad even of Form


      Private Sub Form_Unload(Cancel As Integer)
          Close #1
      End Sub


Make sure that the Properties of MSComm1 are changed as follows:


      RThreshold = 3

0
 

Author Comment

by:PraKash
ID: 1434304
Am usin VB5 and so there is no custom control in the tool bar so added it on the references column and still not getttin the output it still gives me the 424 error on mscomm1 please help me.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now