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
Solved

How to send binary data using Mscomm

Posted on 1998-08-08
5
256 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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

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 …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

789 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