Binary Comms

I am trying to read a binary number from comm port 1 on the PC.

This binary number is a 16 bit number but sent in two 8 bit chunks. For example, if the number were 557, the recieved data would be of the form "00000010" "00101101".
I need to convert this into a decimal value and store it in a variable for further manipulation.

Can anybody help??

Daz
[383 byte] By [biker25] at [2007-11-18 13:54:38]
# 1 Re: Binary Comms
If you have a function F that converts binary to decimal, then apply F to the second chunk, store it in a variable, and add to the variable the value of :
F( Val( first chunk & Str(Len(second chunk),"0") ) )
vbUserName at 2007-11-9 23:28:32 >
# 2 Re: Binary Comms
Or you could just read in the first byte, shift it to the left 8 bits, then OR in the second byte:

Dim input_value as Integer

input_value = getByteFromCom() ' however you get the byte
input_value = input_value << 8 ' left-shift 8 bits
input_value = input_value OR getByteFromCom() ' OR with second byte

'NOTE: No decimal conversion necessary
jaraph at 2007-11-9 23:29:28 >
# 3 Re: Binary Comms
That's wiser !! Where to get the << operand ?
vbUserName at 2007-11-9 23:30:35 >
# 4 Re: Binary Comms
Yeah, I forgot the shift operators aren't available in VB 6.0 (just .NET). Sorry about that.

Anywho, shifting is basically multiplying and dividing by 2 (integer division), so for "<<" you can use "* 2" and for ">>" you can use "/ 2".

Thus,

x = x << 8

becomes

For i=1 To 8
x = x * 2
Next i
jaraph at 2007-11-9 23:31:36 >
# 5 Re: Binary Comms
Many thanks for your replies guys, but the part i am having trouble with is getting the data from the comm port into some kind of variable, I am ok with the manipulation of the data once I have it, but I just cant seem to recieve it. I am using VB.NET.

Many thanks

Daz.
biker25 at 2007-11-9 23:32:31 >
# 6 Re: Binary Comms
Originally posted by jaraph

For i=1 To 8
x = x * 2
Next i


what's wrong with (x * 256)

after all, 2^8 = 256
cjard at 2007-11-9 23:33:30 >
# 7 Re: Binary Comms
Originally posted by biker25
Many thanks for your replies guys, but the part i am having trouble with is getting the data from the comm port into some kind of variable, I am ok with the manipulation of the data once I have it, but I just cant seem to recieve it. I am using VB.NET.

Many thanks

Daz.

check out the Events for the MSComm control
cjard at 2007-11-9 23:34:30 >
# 8 Re: Binary Comms
I have tried using the MSComms control but with no joy. I have been told that there is a way to do this using windows API instructions, but cant seem to work this out.

I am a newcommer to the programming world, but if someone with more knowledge and experience than myself could help, I would be greatfull.

Daz.
biker25 at 2007-11-9 23:35:33 >
# 9 Re: Binary Comms
Check Out This ( http://www.dev-archive.com/forum/showthread.php?s=&postid=846504#post846504) Thread. It Might Give You Some Help.
softweng at 2007-11-9 23:36:36 >
# 10 Re: Binary Comms
Thanks kris.

I looked at the other thread, but cant seem to add the files to a project. I am starting to suspect something wrong with my installation as I have just gone from VB6 to VB.net 2002.

I will keep you posted if and when I get it to work.

Daz
biker25 at 2007-11-9 23:37:37 >
# 11 Re: Binary Comms
Thanks for your help guy's

The software is now working and doing what it should, However, I have a problem when trying to distribute the application. I need it to run on a different machine, but copying the .exe file does not work, can anybody advise me on how to distribute this application.

Many thanks.
biker25 at 2007-11-9 23:38:40 >
# 12 Re: Binary Comms
Maybe you need to copy and register the ocx or dlls that you use in the application.
vbUserName at 2007-11-9 23:39:38 >
# 13 Re: Binary Comms
How do I find out which ones I am using, And how do I go about copying and registering them??

Can I make some sort of an install packadge?
biker25 at 2007-11-9 23:40:40 >
# 14 Re: Binary Comms
I know there are applications for that, just as InstallShield; search the forum about package, as the topic as been discussed.

Good luck !
vbUserName at 2007-11-9 23:41:46 >