Solved

# How to send binary data using Mscomm

Posted on 1998-08-08
223 Views
i need to send 16 bits binary data using rs232 (mscomm.output) .but i don't know how . please help !!!
i have search for the solution for days but seems that nobody know how
to do it except the same thing in vb help that is using a byte array ??
but what do that means ???
0
Question by:khchan
• 3
• 2

LVL 9

Accepted Solution

Dalin earned 50 total points
khchan,
As you know, 8 bits is a byte.  Each piece of your 16 bits data need to bytes to hold it.  Assume you need to send 5 piece of data, that means you need 10 bytes. So you would have an array with data type of byte (instead of integer, string) and size of 10. so you need to do
Dim myBinData (1 to 10) as Byte
Now fill the array with your data and you should be able do it.
Let me know if you have more questions.
Regards
Dalin
0

Author Comment

Thanks Dalin .I know what you say and  i have try with the method that you mention above but it just don't work . let me explain in more detail about what i want.

The data that i need to send is actually an integer from -2047 to +2047 . this need 12 bit for each data but since that we can just send binary by byte then i need 16 bit (2 bytes).i have sucessfully separate my data to highbyte and lowbyte .
below is a portion of my code

dim value(i) as integer 'so that i can key in the data
dim lowbyte,highbyte as byte

For i = 1 To 9
value(i)= 200  ' example of the value that i send

If Value(i) > 256 Then
lowbyte = Value(i) Mod 256
highbyte = Int(Value(i) / 256)
Else
lowbyte = Value(i)
highbyte = 0
End If
MSComm1.PortOpen = True

MSComm1.Output = highbyte
MSComm1.Output = lowbyte
MSComm1.PortOpen = False
next i

the error that came out is
invalid property value.

i have even try this
dim portionbyte(1 to 2) as byte

For i = 1 To 9
If Value(i) > 256 Then
portionbyte(1) = Value(i) Mod 256
portionbyte(2) = Int(Value(i) / 256)
Else
portionbyte(1) = Value(i)
portionbyte(2) = 0
End If

MSComm1.PortOpen = True

MSComm1.Output = portionbyte(1)
MSComm1.Output = portionbyte(2)
MSComm1.PortOpen = False

Next i

just for the sake of the byte array that mscomm needed to send binary data.but the error still there .

Thanks again
0

LVL 9

Expert Comment

khchan,

dim portionbyte(1 to 2)
For i = 1 To 9
If Value(i) > 256 Then
portionbyte(1) = Value(i) Mod 256
portionbyte(2) = Int(Value(i) / 256)
Else
portionbyte(1) = Value(i)
portionbyte(2) = 0
End If

MSComm1.Output = portionbyte
Next i
0

Author Comment

BUT I STILL CAN'T GET IT RIGHT.
AND I HAVE FOUND OUT A METHOD . THAT IS USING

CHR\$(PORTIONBYTE(1)) TO CONVERT TO ASCII AND SEND IT THROUGH COM PORT.

0

LVL 9

Expert Comment

Well, If it did not work, you should not accept the answer.
I am sorry. but it works for me.
0

## Featured Post

### Suggested Solutions

Mysql vs Oracle 10 119
Replace Formatted Numbers with text 9 50
Updates not working for MS Windows 7 12 122
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…