# Making my serial port do what I want.

**URL:** <https://boards.straightdope.com/t/making-my-serial-port-do-what-i-want/451422>\
**Category:** Factual Questions\
**Created:** [June 2, 2008, 1:42am UTC](https://boards.straightdope.com/t/making-my-serial-port-do-what-i-want/451422 "2008-06-02T01:42:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tripler](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/tripler/32/21807_2.png) [@Tripler](https://boards.straightdope.com/u/Tripler)\
**Post date:** [June 2, 2008, 1:42am UTC](https://boards.straightdope.com/t/making-my-serial-port-do-what-i-want/451422/1 "2008-06-02T01:42:55Z")

</div>

Question: Is there an easy way to make my serial port input or output a signal?

For example, say I have an exceedingly geeky day, and make a breadboard shortwave receiver. For an oscillator, I decide I want to pull a signal off of my clock on my desktop. I can easily fabricate a wire to an old serial port plug to solder onto my breadboard, but my question is about available software: is there existing Windows software that lets me route a particular signal (be it pulse, sine wave, sawtooth wave, etc.) to a particular pin on my serial port?

Tripler  
Now, where’d I leave my wire strippers?

---

<div class="post-metadata">

**Author:** ![beowulff](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/beowulff/32/542_2.png) [@beowulff](https://boards.straightdope.com/u/beowulff)\
**Post date:** [June 2, 2008, 1:49am UTC](https://boards.straightdope.com/t/making-my-serial-port-do-what-i-want/451422/2 "2008-06-02T01:49:04Z")

</div>

No.

Serial ports are controlled directly by a UART, so the best you can do is to send digital signals out the serial port. No analog, no sine waves.

---

<div class="post-metadata">

**Author:** ![Q.E.D](https://avatars.discourse-cdn.com/v4/letter/q/51bf81/32.png) [@Q.E.D](https://boards.straightdope.com/u/Q.E.D)\
**Post date:** [June 2, 2008, 1:56am UTC](https://boards.straightdope.com/t/making-my-serial-port-do-what-i-want/451422/3 "2008-06-02T01:56:27Z")

</div>

Nope, square wave only, although the duty cycle is variable. And you’d be limited by the baud rate of the UART chip driving it, which isn’t going to anywhere near the mainboard clock frequency. You could conceivably get whatever waveform (or at least a close approximation thereof) you want by sending the appropriate data to your sound card, but that’s going to be an even lower frequency. The appropriate frequency multipliers might get you in the ballpark. Maybe.

---

<div class="post-metadata">

**Author:** ![Tripler](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/tripler/32/21807_2.png) [@Tripler](https://boards.straightdope.com/u/Tripler)\
**Post date:** [June 2, 2008, 2:13am UTC](https://boards.straightdope.com/t/making-my-serial-port-do-what-i-want/451422/4 "2008-06-02T02:13:58Z")

</div>

[QUOTE=Q.E.D.]  
Nope, square wave only, although the duty cycle is variable. And you’d be limited by the baud rate of the UART chip driving it, which isn’t going to anywhere near the mainboard clock frequency. You could conceivably get whatever waveform (or at least a close approximation thereof) you want by sending the appropriate data to your sound card, but that’s going to be an even lower frequency. The appropriate frequency multipliers might get you in the ballpark. Maybe.  
[/QUOTE]

With this and **beowulff** ’s comment, _in theory_ I could build another breadboard to convert square wave into . . . well, whatever for my own purposes.

Is there a program that I can access the UART (and what does that stand for BTW?), or can I do so through DOS?

Tripler  
Thanks for the help already, guys!

---

<div class="post-metadata">

**Author:** ![Q.E.D](https://avatars.discourse-cdn.com/v4/letter/q/51bf81/32.png) [@Q.E.D](https://boards.straightdope.com/u/Q.E.D)\
**Post date:** [June 2, 2008, 2:18am UTC](https://boards.straightdope.com/t/making-my-serial-port-do-what-i-want/451422/5 "2008-06-02T02:18:54Z")

</div>

UART stands for Universal Asynchronous Receiver/Transmitter. You access it by opening and sending data through whatever serial port (COM1, COM2, COM3, etc.) you wish to use. You don’t actually address the UART directly, the OS does it for you anytime you send data to a COM port.

---

<div class="post-metadata">

**Author:** ![beowulff](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/beowulff/32/542_2.png) [@beowulff](https://boards.straightdope.com/u/beowulff)\
**Post date:** [June 2, 2008, 5:18am UTC](https://boards.straightdope.com/t/making-my-serial-port-do-what-i-want/451422/6 "2008-06-02T05:18:37Z")

</div>

For an inexpensive arbitrary-waveform generator, you can use your sound card…

---

<div class="post-metadata">

**Author:** ![Mangetout](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/mangetout/32/19_2.png) [@Mangetout](https://boards.straightdope.com/u/Mangetout)\
**Post date:** [June 2, 2008, 7:11am UTC](https://boards.straightdope.com/t/making-my-serial-port-do-what-i-want/451422/7 "2008-06-02T07:11:52Z")

</div>

You can do this a little more readily with a _parallel_ port (if you have one) - that is, you can explicitly set and unset bits on it, causing any of multiple I/O lines to be high or low - hook up a simple D to A converter to that and with a little more work, you can generate a stepped approximation of a sinewave.

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [June 2, 2008, 7:47am UTC](https://boards.straightdope.com/t/making-my-serial-port-do-what-i-want/451422/8 "2008-06-02T07:47:18Z")

</div>

[The Universal Software Radio Peripheral uses a USB connection and a few D2A converters, an FPGA, and some glue logic.](http://en.wikipedia.org/wiki/Universal_Software_Radio_Peripheral) It’s designed for software-controlled radio, as part of the [GNU Radio Project](http://en.wikipedia.org/wiki/GNU_Radio).

[http://gnuradio.org/trac/wiki](http://gnuradio.org/trac/wiki)

---

<div class="post-metadata">

**Author:** ![cormac262](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@cormac262](https://boards.straightdope.com/u/cormac262)\
**Post date:** [June 2, 2008, 6:00pm UTC](https://boards.straightdope.com/t/making-my-serial-port-do-what-i-want/451422/9 "2008-06-02T18:00:49Z")

</div>

[QUOTE=Mangetout]  
You can do this a little more readily with a _parallel_ port (if you have one) - that is, you can explicitly set and unset bits on it, causing any of multiple I/O lines to be high or low - hook up a simple D to A converter to that and with a little more work, you can generate a stepped approximation of a sinewave.  
[/QUOTE]

Using a parallel port to generate a square wave would definitely be better than using a UART (COM) port.

You will probably want to talk to the (memory mapped) hardware registers directly. Depending on the version of Windows you’re running, this may be easy, need a work-around, or be very difficult. The advantage to writing to the registers directly is that you can create an infinite loop to toggle your bits. If you are using standard “write” calls, each block of data (containing the toggling bits) will have to be of some finite length. So this will cause “gaps” in your clock between successive calls.

However, using your PC (and parallel port) for a clock source is going to have some problems. There will likely be lots of jitter, and depending on the O/S and what else is going on, there may even be some significant delays in the cycles. The problem will be in effectively monopolizing the CPU so that you can produce consistent timing. Your program can likely grab the CPU for a burst, but not continue for a predictable amount of time.

If you think your application can tolerate such inconsistencies in a clock, then this link [http://www.lvr.com/parport.htm](http://www.lvr.com/parport.htm) can get you started on how to use the parallel port.

---

<div class="post-metadata">

**Author:** ![Q.E.D](https://avatars.discourse-cdn.com/v4/letter/q/51bf81/32.png) [@Q.E.D](https://boards.straightdope.com/u/Q.E.D)\
**Post date:** [June 2, 2008, 6:57pm UTC](https://boards.straightdope.com/t/making-my-serial-port-do-what-i-want/451422/10 "2008-06-02T18:57:21Z")

</div>

If you really want a cheap, easy and flexible source of square waves or digitally-synthesized arbitrary waveforms, get yourself a PIC programmer and some PIC chips. They’re cheap, reliable and easy to program and work with. I recently used one as a MOSFET gate driver for a DC-DC inverter project (Geiger-Mueller tube PS, if you’re interested.)

---

<div class="post-metadata">

**Author:** ![Precambrianmollusc](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/precambrianmollusc/32/63_2.png) [@Precambrianmollusc](https://boards.straightdope.com/u/Precambrianmollusc)\
**Post date:** [June 2, 2008, 7:08pm UTC](https://boards.straightdope.com/t/making-my-serial-port-do-what-i-want/451422/11 "2008-06-02T19:08:48Z")

</div>

Not too sure about the delights of UART, but if you can get a square wave of variable duty cycle on the serial pins, could this be used to control an IGBT or several IGBT strung together using a pulse width modulation scheme from the serial port. IGBT controlled through a PWM scheme are used to covert DC to variable frequency AC output for all manner of electric motors.
