Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 392
  • Last Modified:

sending a binary file using the MS Comm control


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
VEngineer
Asked:
VEngineer
  • 4
  • 3
  • 2
  • +2
1 Solution
 
baltmanCommented:
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
 
VEngineerAuthor Commented:
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
 
VincentLawlorCommented:
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
Technology Partners: 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!

 
VEngineerAuthor Commented:
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
 
JanusFuryCommented:
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
 
VEngineerAuthor Commented:
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
 
VincentLawlorCommented:
What size is your Recieve buffer
MSComm1.RThreshold

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

Vin.
 
0
 
Richie_SimonettiIT OperationsCommented:
Hearing...
0
 
VEngineerAuthor Commented:
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
 
VincentLawlorCommented:
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
 
JanusFuryCommented:
That sounds good, I'd try it.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now