Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Communications > MSComm Questions on Sending Files


Reply
 
Thread Tools Display Modes
  #1  
Old 04-17-2008, 08:56 AM
jermore jermore is offline
Newcomer
 
Join Date: Apr 2008
Posts: 15
Default MSComm Questions on Sending Files


When sending an integer using MScomm's .Output function I'm assuming it sends the entire length of the data type, is that correct?

For example,

Dim X As Integer
Dim Y As Long

X = 3
Y = 3

comPort.Output = X 'sends 00000000 00000011
comPort.Output = Y 'sends 00000000 00000000 00000000 00000011

Or is the value truncated? I am trying to read in a binary file and send it using the code below, but I need to send the file size first and the receiver end will be expecting 4 bytes regardless of whether the file size is 1 or 10,000 bytes. Any suggestions? Feel free to critique the code below in regards to sending the file.

Dim FF As Integer
Dim strOpen As String
Dim strBuff As String

dlgOpenFile.ShowOpen
strOpen = dlgOpenFile.FileName
FF = FreeFile()

Open strOpen For Binary As FF

strBuff = String(LOF(FF), " ")

Get FF, , strBuff
frmMain.comPort.Output = strBuff

Close FF
Reply With Quote
  #2  
Old 04-25-2008, 04:03 AM
mkaras's Avatar
mkaras mkaras is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Mar 2004
Location: Beaverton, OR
Posts: 1,874
Default

Code:
Dim X As Integer
comPort.Output = X 'sends 00000000 00000011
X = 3

Dim Y As Long
Y = 3
comPort.Output = Y 'sends 00000000 00000000 00000000 00000011
Using either of these sequences is NOT the way to send the length of the file. The comPort.Output function accepts a String or a Byte Array as its output value. So one of the assignments that you show is not allowed.

In order to get a string value and to work out getting the converted number value into a string that is always the same length you could consider something like this:

Code:
Dim Y As Long
Dim S As String
Y = <some value of some sort like a file size>
S = Format(Y, "000000000000")
comPort.Output = S
Here I show how you intentionally convert the Y variable value to a string that has a length of 12. So you know you will send 12 characters out the comm port.

By the way, if you were to range check the value of Y to be less than the full range of the value stored in a Long you could use a shorter template string in the Format statement. For example if you made sure that 0 <= Y <= 255 then the template string could be "000" with length 3 and you would know you send 3 bytes.

There are ways to manipulate sending the actual binary value of the Long variable. It involves separating the 32-bits of the variable into four bytes and then sending each of those bytes as a string byte to the port. However I would recommend the method shown here here for a couple of reasons. First off it is easy be able to read the file length in transmitted data sequence if it has been formatted to human readable format. Secondly, you only send the value once so there is little or no performance issue if you send four bytes or twelve. And on the receiving end it is very easy to accumulate the first 12 bytes into a string and convert that back into a long value using the CLng() function.
Reply With Quote
  #3  
Old 05-01-2008, 09:41 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,722
Default

If you have a binary file you need to send, you wouldn't normally read it into a string array, as it will be converted into unicode (two bytes in the string for each byte read from the file). When transmitted out the serial port, it will probably be serialized as ASCII (one byte per char), but I haven't worked with string data in or out of a serial port in a long time, only binary data structures, so can't say definitively.
It is much faster (and half the memory), to read the file into a binary array, and send that through the port. The CopyMemory API call is what I usually use to copy structures (user defined types) to and from byte arrays.
It can also copy a Long, to the first 4 bytes of a byte array, to be sent out the port.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->