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


ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!


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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
to transfer string from C lanaguage to VBA 4 66
message box in access 4 46
Automatic Email Reminder 4 38
VBA - If Bookmark = "XXBOOKMARKXX" then 15 29
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

810 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