Solved

sending a binary file using the MS Comm control

Posted on 2001-09-05
11
370 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
  • 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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 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)

<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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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 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…

747 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

8 Experts available now in Live!

Get 1:1 Help Now