# How do I correctly send data to a com port?

**URL:** <https://boards.straightdope.com/t/how-do-i-correctly-send-data-to-a-com-port/292886>\
**Category:** Factual Questions\
**Created:** [March 4, 2005, 12:59pm UTC](https://boards.straightdope.com/t/how-do-i-correctly-send-data-to-a-com-port/292886 "2005-03-04T12:59:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Schnauzer](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@Schnauzer](https://boards.straightdope.com/u/Schnauzer)\
**Post date:** [March 4, 2005, 12:59pm UTC](https://boards.straightdope.com/t/how-do-i-correctly-send-data-to-a-com-port/292886/1 "2005-03-04T12:59:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Bewildebeest](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@Bewildebeest](https://boards.straightdope.com/u/Bewildebeest)\
**Post date:** [March 4, 2005, 1:20pm UTC](https://boards.straightdope.com/t/how-do-i-correctly-send-data-to-a-com-port/292886/2 "2005-03-04T13:20:45Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![Schnauzer](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@Schnauzer](https://boards.straightdope.com/u/Schnauzer)\
**Post date:** [March 4, 2005, 9:47pm UTC](https://boards.straightdope.com/t/how-do-i-correctly-send-data-to-a-com-port/292886/3 "2005-03-04T21:47:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JimOfAllTrades](https://avatars.discourse-cdn.com/v4/letter/j/ba8739/32.png) [@JimOfAllTrades](https://boards.straightdope.com/u/JimOfAllTrades)\
**Post date:** [March 5, 2005, 3:52am UTC](https://boards.straightdope.com/t/how-do-i-correctly-send-data-to-a-com-port/292886/4 "2005-03-05T03:52:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ccwaterback](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@ccwaterback](https://boards.straightdope.com/u/ccwaterback)\
**Post date:** [March 5, 2005, 4:52am UTC](https://boards.straightdope.com/t/how-do-i-correctly-send-data-to-a-com-port/292886/5 "2005-03-05T04:52:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eagle](https://avatars.discourse-cdn.com/v4/letter/e/ba8739/32.png) [@eagle](https://boards.straightdope.com/u/eagle)\
**Post date:** [March 5, 2005, 10:56am UTC](https://boards.straightdope.com/t/how-do-i-correctly-send-data-to-a-com-port/292886/6 "2005-03-05T10:56:04Z")

</div>

> [@Schnauzer](#):
>
> I get a timeout error. … Any polite suggestions?

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

---

<div class="post-metadata">

**Author:** ![eagle](https://avatars.discourse-cdn.com/v4/letter/e/ba8739/32.png) [@eagle](https://boards.straightdope.com/u/eagle)\
**Post date:** [March 5, 2005, 11:01am UTC](https://boards.straightdope.com/t/how-do-i-correctly-send-data-to-a-com-port/292886/7 "2005-03-05T11:01:55Z")

</div>

> [@ccwaterback](#):
>
> UARTs can be funky. Maybe try putting a \<CR\>\<LF\> at the end of the string?

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

---

<div class="post-metadata">

**Author:** ![astro](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@astro](https://boards.straightdope.com/u/astro)\
**Post date:** [March 5, 2005, 12:44pm UTC](https://boards.straightdope.com/t/how-do-i-correctly-send-data-to-a-com-port/292886/8 "2005-03-05T12:44:27Z")

</div>

See [remote data projector commands](http://global.mitsubishielectric.com/bu/projectors/download/manual/232c_xl8_xl5_sl4s.pdf)

Are you using a null modem cable or not?

---

<div class="post-metadata">

**Author:** ![Schnauzer](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@Schnauzer](https://boards.straightdope.com/u/Schnauzer)\
**Post date:** [March 7, 2005, 12:08pm UTC](https://boards.straightdope.com/t/how-do-i-correctly-send-data-to-a-com-port/292886/9 "2005-03-07T12:08:44Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![ccwaterback](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@ccwaterback](https://boards.straightdope.com/u/ccwaterback)\
**Post date:** [March 8, 2005, 8:21am UTC](https://boards.straightdope.com/t/how-do-i-correctly-send-data-to-a-com-port/292886/10 "2005-03-08T08:21:44Z")

</div>

> [@](#):
>
> ```
> In order to use the modem (or any other serial device) from
> BASIC, you must first OPEN it, treating it as you would treat a file.
> The flexible syntax of the OPEN COM statement is explained well in
> Microsoft's manuals, but I'll give a most common syntax of it:
>  
> OPEN "COM1:2400,N,8,1,OP0,DS0" FOR RANDOM AS #FileNumber% LEN=1024
>  
> The above statement, as you can probably gather from its simple
> syntax, will open communications port number 1 at a most common
> setting of 2400 bps, 8 data bits, 1 stop bit, and no parity. The OP0
> and DS0 parameters signify, respectively, for BASIC not to wait at
> all before communications lines become active, and for BASIC to
> ignore (for simplicity's sake) the DSR (Data Set Ready) line.
> Lastly, the LEN parameter specifies the length of the input buffer.
>  
> Now that you've opened it, output to the modem is easy. To send
> an "AT".. command, just do a PRINT #1, ModemCommand$.
> 
> ```

Maybe this will work?

Found it here: [http://www.petesqbsite.com/sections/tutorials/othertuts.shtml](http://www.petesqbsite.com/sections/tutorials/othertuts.shtml)

Under: The BASICS of QB’s Serial Communications
