sending a binary file using the MS Comm control

Posted on 2001-09-05
Last Modified: 2007-12-19

I just figured out how I can send binary data through a serial port using the MS Communication Control, but I need to figure out a way how to send (and later receive) a file across a serial port using the MS Communication Control.  Has anyone done this before or have any tips?
Question by:VEngineer
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2

Expert Comment

ID: 6458489
Using the Open statement, you can open the file (wether it is binary or not) and send the data from the file through your existing code:

Open "filename.txt" For Input As #1
Do While Not EOF(1)
     do with the data what you want to do here....
Close #1

There is some more information, also on how to open binary files in the following article:

Hope this helps...


Author Comment

ID: 6458693
This is what I have so far:

Dim filename As String
filename = "c:\my.jpg"
Dim filehandle As Integer
filehandle = FreeFile()
Open filename For Binary As #filehandle
Dim filedata As Variant
Get #filehandle, , filedata
Close #filehandle


Now, the question I have is, does filedata now contain the contents of the entire file?


Expert Comment

ID: 6458714
Since you have figured out how to send data to the serial port reciving data is done as follows.

Add a MSComm control to a form

In the Form Load or another place of your choice add the code

MSComm1.PortOpen = true

In your Form Unload or another place of your choice add the following

MSComm1.PortOpen = false

Create a buffer to hold the data making sure it's the correct size

Dim strBuffer as String

Now in the MSComm_OnComm event

Select Case MSComm1.CommEvent
      Case comEvReceive   ' Received RThreshold # of 'chars.
        strBuffer = strBuffer + MSComm1.Input
      Case comEvEOF   ' An EOF charater was found in the input stream
        'Got a EOF so add this packet to the buffer without the EOF
        strBuffer = strBuffer + Mid(MSComm1.Input, 1, MSComm1.InputLen - 1)
        'Write the file to disk your function
        WriteFileToDisk strBuffer
   End Select

Another way to do this if you are expecting binary data would be to decalare a dynamic byte array and redimension it every time data arrives using the current size of the buffer and the lenght of the incoming data using MSComm1.InputLen. Then write the byte array to your binary file.


What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 6458957
Baltman - thx for the comments on file sending..

Vincent - that looks really close to what I need.  I was trying to figure out when the file started and ended - and you solved that problem for me on the nose.  The trick I guess is to look for that EOF character.

I'm going to work on this tomorrow morning and if it works, points will go all around.

Expert Comment

ID: 6459033
No, wait, loading a binary file is completely different.
Here's how it works:

Dim FileData as Byte()
Redim FileData(0 to LOF(FileHandle)-1)
Get #FileHandle, 1, FileData()

Then FileData() is a byte array containing the contents of the entire file. Converting it to a string is as easy as this:

Dim FileString as String
FileString = FileData()

Good Luck :)

Author Comment

ID: 6460915
Janus, so the main thing that was wrong with my code I posted was the types?

Vince, does VB allow us to read binary data into a string like that?

I did enter your program in Vince, but I keep getting a case value of Receive Buffer Overflow - which keeps skipping me out of the two cases I tried to use (comEvRecv and comEvEOF) I'm not sure how to avoid that.  I'm sending a fairly large file so it's not working quite well.

Do you think I need to use a protocol to send the file over the serial connection - for instance one of those old modem protocols like ZModem?

Expert Comment

ID: 6461155
What size is your Recieve buffer

Are you clearing the Comms buffer each time using
MSComm1.Input ?

LVL 16

Expert Comment

ID: 6461595

Author Comment

ID: 6462496
I've adjusted the buffer between 100 and 30000.  I still get those receive buffer overflow errors even if I clear the buffer every time with MSComm1.Input.  I think the problem is that the comEvRecv event is not processing fast enough to get that buffer cleared enough to get more data in.

I have about 40 KB going across the line, and I can think I can see how there can be overflow conditions if the buffer is small.  Nothing is signaling the sender to wait if the buffer is full.  As soon as the data starts coming in, I've traced through MsgBoxes and printing to files that one iteration goes into comEvRecv.. then the next one is buffer overflow error and it stops right there.

This is why I think using some protocol might be in order.  That way there is some acknowledgement across the line when it is clear to send.

Accepted Solution

VincentLawlor earned 100 total points
ID: 6462623
Try breakin the file into packets of say 2048 bytes.

Here's a simple protocol.

To initiate a file transfer the Client makes a connection to the Server and transmits an ENQ.
i.e. Chr(5)

When the Server recieves an ENQ it should start transmitting the file.

Start each packet transmitted with STX.
End a transmission with ETX.
i.e. Chr(2) and Chr(3)


This means each packet will be 2050 bytes long including the STX and ETX (or just make the maximum packet length 2048 including the STX and ETX)

When there is no data left to transmit terminate the communication with an EOT.
i.e. Chr(4)

After each packet is recieved on the client side the client sends an ACK to the server.
i.e. Chr(6)

If an error occured in the transmission the client sends a NAK to the server.
i.e. Chr(21)

This a simple protocol but should suffice for your needs.

If you wish you can add data integrity checking to the packets. Add a LRC (Linear Redundancy Checksum) to the end of the packet. This ensures that the data transmitted was the same as the data recieved.




Expert Comment

ID: 6462829
That sounds good, I'd try it.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month6 days, 21 hours left to enroll

622 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