[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 394
  • Last Modified:

How to send binary data using Mscomm

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
khchan
Asked:
khchan
  • 3
  • 2
1 Solution
 
DalinCommented:
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
 
khchanAuthor Commented:
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 .

please help!!!

Thanks again
0
 
DalinCommented:
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
 
khchanAuthor Commented:
THANKS FOR YOUR HELP .
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
 
DalinCommented:
Well, If it did not work, you should not accept the answer.
I am sorry. but it works for me.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now