Solved

Reading Binary Data from Serial Port

Posted on 2003-11-24
6
436 Views
Last Modified: 2006-11-17
I need to read binary data from a serial port. The data is in the format of 23 single byte values. After the values are read I need to combine pairs of byte values (high  and low) to get a 16 bit value.

I have read text values from the serial port before but have no experience in binary data. How do I ....

1) read binary data into a variable?
2) convert high and low single byte values into one 16 bit value?

Some simple code examples would be appreciated.
0
Comment
Question by:allsopp
6 Comments
 
LVL 30

Accepted Solution

by:
nmcdermaid earned 500 total points
ID: 9811990
Here is a starting point for you:


Once you have loaded your data into a string from the COM port, you can use the Mid function to extract a single byte at a time.

Therefore to get the third of the 23 bytes out of your string (called sWholeString in this example), use:

sHigh = Mid(sWholeString,3,1)

That is a string. Then you can use Asc(sByte) to get the Ascii value.

iHigh = Asc(sHigh)

Then if your low byte is in the next byte, use this to get your 16 bit value:

sLow = Mid(sWholeString,4,1)

iLow = Asc(sLow)

iTotal = sHigh * 256 + iLow



Try it out and if it works and you want more detail let me know.
0
 

Author Comment

by:allsopp
ID: 9812014
Thanks. The actual equipment I need to talk to is arriving sometime this week. I'll try it out and respond then.
0
 
LVL 1

Expert Comment

by:MsLim
ID: 9834609
thinking
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Expert Comment

by:Steve-Howes
ID: 9845173
the parallel port is much easier to manipulate in vb, if you get dlportio from DriverLINX you can just use a function to return a value for the port,, but if the equiptment you are using will only support the serial port then it will be quite a bit harder to do
0
 

Author Comment

by:allsopp
ID: 9896579
The equipment is a radio tx/rx unit with a receiver that outputs to RS485 serial.
The code example you provided worked perfectly first time so the points are yours
0
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 9896934
Sounds like fun whatever you're doing!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

837 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