Solved

How to send binary data using Mscomm

Posted on 1998-08-08
5
270 Views
Last Modified: 2011-09-20
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
Comment
Question by:khchan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
Dalin earned 50 total points
ID: 1468364
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

by:khchan
ID: 1468365
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
 
LVL 9

Expert Comment

by:Dalin
ID: 1468366
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

by:khchan
ID: 1468367
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
 
LVL 9

Expert Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

733 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