How do I correctly send data to a com port?

I need to send a few bytes of data from the com port of a computer to remotely control a data projector. As my programming skills are stuck firmly in 1987 I used QuickBasic (no jokes please). Here’s the program I used:

10 cp1$=CHR$(2)+CHR$(3)+CHR$(0)+CHR$(0)+CHR$(2)+CHR$(1)+CHR$(1)+CHR$(9)
20 OPEN "com1:9600,n,8,1 FOR OUTPUT AS #1
30 PRINT#1,cp1$
40 CLOSE #1

The problem is that it doesn’t work. I get a timeout error. I’ve run the same program (albeit with a slightly different line 20) on a dusty NEC PC-8201A and it does work, so I must be warm. I thought it might be Windows preventing com port access (I used W98), so I booted to DOS with no success. I’ve also tried another computer. Any polite suggestions?

Is there a typo in your code fragment here? Shouldn’t line 20 be:

20 OPEN “com1:”,9600,n,8,1 FOR OUTPUT AS #1

(Missing close quote and comma.)

Thanks BWB, hadn’t noticed the missing quote, but that might be my transcription of it to the boards (I don’t have the original program to hand right now). Checking the help files in QB I don’t think there’s a comma before the speed number. Couple of things to try anyway.

Is this a one-time thing, or this something you need to be able to do at the click of a button?

A couple of methods to consider that don’t use basic:

Use HyperTerminal and Notepad. They both come with Win98. Use Notepad to create a text file that contains the character string you want to send. Then start HyperTerminal, connect to the com port, and send the file out the port.

or

Use Notepad and DOS commands. Again, use Notepad to create a text file containing the character string you want to send. At the dos prompt, use the “mode” command to set the com port (e.g. mode com1: baud=9600 parity=n data=8 stop=1 ). Then just use the DOS copy command to send the previously created file to the com port (e.g. copy myfile.txt com1 ).

or

Same as above, but put the two DOS commands into a bat file, and put a shortcut to the bat file on the desktop, and voila, send the string at the click of a button.

UARTs can be funky. Maybe try putting a <CR><LF> at the end of the string?

Well it’s been a long time since I last used COM ports directly, however, almost always when I got timeouts it was because of handshaking problems. (Question: did you use the same cable when you connected the NEC PC-8201A? If you used different cables, this might explain, why it works for one, but not for the other. (3 wire vs. full connected) )

You might want to try to disable hardware handshake completely:

OPEN “COM1:9600,N,8,1,CD0,CS0,DS0,OP0,RS” AS #1

(This OPEN is FOR RANDOM access. Which I think is better for COM ports, even if you never read. However, if your data projector answers to the commands you send to him, it is best to read from the com port, as otherwise communication might again get blocked.)

The options above are:
CD0 = Turns off time-out for Data Carrier Detect (DCD) line.
CS0 = Turns off time-out for Clear To Send (CTS) line.
DS0 = Turns off time-out for Data Set Ready (DSR) line.
OP0 = Turns off time-out for a successful OPEN.
RS suppresses detection of Request To Send (RTS).

Please note: If you disable hardware handshaking, then communication might still work, but you won’t get error messages if it does not.

HTH

CRLF is a terminal control sequence. It has nothing to do with UART functions. I don’t know how QBasic is implemented, but using PRINT (instead of OUT) might involve a terminal control, so your point might still be valid if we are talking about flushing IOCTL data buffers. However if QBasic has a FLUSH command it is better to use this or alternatively a CLOSE will also flush pending data.

cu

See remote data projector commands

Are you using a null modem cable or not?

RJKUgly: this is not a one-off thing; I need the data projector to be turned on and off at specified times a couple of times a day. The idea of using QB (apart from the fact that it’s the only programming language I know [apart from Forth, but let’s not go there]) is that it can be compiled to an EXE and then I could get Task Scheduler to run the prog at the specified times. I’ll give the mode/copy method a go.

Eagle: Yes, I used the same cable, which is OK with the PC too as the windows control software that came with the data projector works. That software won’t do what I need though.

Thanks for the suggestions, it’ll be a couple of days before I can try again, so I’ll be back…

Maybe this will work?

Found it here: http://www.petesqbsite.com/sections/tutorials/othertuts.shtml

Under: The BASICS of QB’s Serial Communications