Solved

MsComm control Pass byte data to output

Posted on 1997-11-09
4
548 Views
Last Modified: 2006-11-17
My question comes from the MsComm control.

I am trying to write a test program to control a microcontoler that I have programed.  The Microcontroler has  12 commands that it receivers from and rs232 interface programed into the chip.  What I am trying to do is to build an operations interface for the microcontroler.  What I am trying to do.

When I click on a button I want to send out a byte string.
Say command 1, I want to send eight bits that are "00000001".  I am trying to use this code

Private Sub Command1_Click()
Dim Buffer As Variant
Dim Channel1 As Byte
Channel1 = 1
Buffer = Channel1
MSComm1.CommPort = 3
MSComm1.PortOpen = True

MSComm1.Output = Channel1

Now the VB Books on line tells me that the type BYTE is

"Byte variables are stored as single, unsigned, 8-bit (1-byte) numbers ranging in value from 0–255"

Well this is what I want to output
But when I use my code I get an error saying type mismatch.

The Vb books online say this

To send binary data, you must pass a Variant which contains a byte array to the Output property.


What am I doing wrong.

0
Comment
Question by:jaalex
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:alamo
ID: 1440326
I don't know why the documentation claims you need to use a byte array for binary data, except that a binary array is slightly easier to work with than a string. Passing it through a variant probably should convert it to a string for the MSComm control, but I experience the same results as your do.

The MSComm control needs a string, so give it a string - who cares if the data is binary:

Dim strBuffer As String
strBuffer = Chr$(1)
MSComm1.CommPort = 3
MSComm1.PortOpen = True
MSComm1.Output = strBuffer

For binary data don't forget to set Handshaking to None or RTS/CTS, and NullDiscard to False. Good luck!
0
 

Author Comment

by:jaalex
ID: 1440327
Ok so I can pass a string.    But using the chr$ function will I get the byte information.

The micro will only understand  

Start bit + eight data bits + stop

The commands can only be

Start + "00000000" + stop
Start + "00000001" + stop

Anyway you get the picture.  

I read in the doc that if I do

Dim buffer as String

buffer = Chr(67)  

I will return an A value.   Is this the same as a Chr$

If you feel you need more points I will add them
0
 
LVL 6

Accepted Solution

by:
alamo earned 150 total points
ID: 1440328
Yes, Chr and Chr$ are basically the same, VB is trying to get away from $ to denote strings but I use it out of habit. Actually, I just looked the the help and they have changed Chr so it deals "properly" with double-byte character sets; ChrB is guaranteed to return just 1 character, used for the sort of thing you need.

So 'Start + "00000000" + stop' would be ChrB(0), 'Start + "00000001" + stop' would be ChrB(1).

(Actually, Chr(65) is "A", not Chr(67), which is "C").

Let me know how it works... the points are fine. By the way, you can leave a comment without rejecting the answer if you need more information or clarification; when you leave a comment I get an email notifying me so I can check.
0
 

Author Comment

by:jaalex
ID: 1440329
OK cool  I will check it out.

Thanks for the info
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now