Comm port

brettrigby
09-15-2000, 02:47 PM
Hello everybody!

Is it possible to control data to and from the Comm ports on a PC? If so, could you tell me how to go about sorting it out?

Thanks

Brett

BillSoo
09-15-2000, 03:03 PM
This is a pretty big topic so you'll have to be a bit more specific about what you want to do.

For starters though, you could use the MSComm control.
You would put the control on your form and use it something like this:

mscomm1.commport = 1 'serial port 1
mscomm1.settings = "9600,n,8,1"
'9600 baud, no parity, 8 data bits, 1 stop bit
mscomm1.portopen = true 'open the port
mscomm1.output = "test string" 'send some stuff out the port
s$ = mscomm1.input 'receive some stuff from the port
mscomm1.portopen = false 'close the port

You can get data in a simple polling loop like:
do
doevents
if mscomm1.inbuffercount then s$=s$ & mscomm1.input
loop

or you could set the control to trigger an event whenever data comes in. This is the preferred way nowadays.

MScomm1.rthreshold = 1
...
Private Sub MSComm1_OnComm()
if mscomm1.commevent = comEVReceive then s$=s$ & mscomm1.input
end sub

There are also various properties for controlling specific lines, such as RTS, CTS, etc.


"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder

brettrigby
09-15-2000, 04:39 PM
Cheers BillSoo.

I wasn't sure how to go about it, but your reply was a help.
More specific? What I want to do is at the click of a button read from (or write to) the comm port. I see what your saying about the trigger, so maybe I'd like to know more about that route, if possible?

Thanks again!

Brett

BillSoo
09-15-2000, 05:01 PM
Using the button event to send info makes sense since you have control over what you are sending. However, using the button to get what you are receiving poses a problem since you don't know what you are receiving or when. The control only sets up a limited input buffer. I think the default is only 1K. If you don't clear the buffer before more stuff comes in, you get a buffer overrun error. So having the user have to click a button to clear the buffer is not a good solution (note: accessing the input property like s$=mscomm1.input will clear the buffer).

The OnComm event is the way to go. This event will fire anytime certain events occur. By default though, incoming data will *NOT* fire this event. By setting the RThreshold property from 0 to 1, you tell the control to fire the ONCOMM event anytime you get a byte of data (if you knew that the data was only coming in blocks of, say, 100 bytes, you could set the rthreshold property to 100 instead). Since all kinds of conditions trigger the oncomm event, you need to check the commevent property to see which condition triggered the event. If it was a received data condition, then you clear the buffer.

"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder

brettrigby
09-15-2000, 05:38 PM
Hi BillSoo,

Sorry to sound stupid, but I can't get VB6 to recognise the mscomm1 command. Is there an add in I have to install first?

Brett

BillSoo
09-15-2000, 06:24 PM
Yes,

Go "Project", "Components", "Microsoft Comm Control 6.0"

This will add an icon that looks like a telephone to your controls toolbox. Add this control to your form. The default name for the first control is mscomm1 but you can change that to something more meaningful if you wish.

As for sounding stupid...nobody is born knowing this stuff.

"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder

brettrigby
09-16-2000, 05:56 AM
Ha Ha, now we're talking! I kept getting error messages because of that add-in.

Thanks a lot, I've give that a go and I'm sure you'll hear from me if it all goes wrong.

Cheers BillSoo! You're tops.
Brett

baddy
09-17-2000, 10:45 PM
To begin with, start by adding the microsoft comm control in the project->control. Then add you will have a mscomm control which enables u to work with the comm port.
The 1st thing to do is to set the configuration.
eg:
'set to port 1, baud rate 9600,no parity,8 data bits, 1 stop bit
mscomm1.commport = 1
mscomm1.settings = "9600,n,8,1"
'open port
mscomm1.portopen = true

After this u need to determine whether u want to transmit or receive data, using mscomm1.input or mscomm1.output

Finally, close the port
mscomm1.portopen=false

brettrigby
09-18-2000, 04:34 PM
Cheers baddy buddy.

baddy
09-19-2000, 01:13 AM
u welcome :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum