Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sending a binary file using the MS Comm control

Posted on 2001-09-05
11
Medium Priority
?
384 Views
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?
0
Comment
Question by:VEngineer
[X]
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
11 Comments
 
LVL 1

Expert Comment

by:baltman
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....
Loop
Close #1

There is some more information, also on how to open binary files in the following article:
http://support.microsoft.com/support/kb/articles/Q209/2/31.ASP

Hope this helps...

BA
0
 
LVL 2

Author Comment

by:VEngineer
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?

0
 
LVL 4

Expert Comment

by:VincentLawlor
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.

Vin.









Vin.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Author Comment

by:VEngineer
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.
0
 
LVL 2

Expert Comment

by:JanusFury
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 :)
Janus
0
 
LVL 2

Author Comment

by:VEngineer
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?
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6461155
What size is your Recieve buffer
MSComm1.RThreshold

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

Vin.
 
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6461595
Hearing...
0
 
LVL 2

Author Comment

by:VEngineer
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.
0
 
LVL 4

Accepted Solution

by:
VincentLawlor earned 400 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)

<STX>YourDatapacket<ETX>

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.

Vin.





 



 
0
 
LVL 2

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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 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…
Suggested Courses

730 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