2

I'm helping out a friend with his Electrical Engineering project. He is building a device that will use a serial port to communicate to some software. On a windows platform (Win7), how would one read and write directly to a specific pin on the serial port? Is there an API that Windows exposes for this sort of thing?

2 Answers 2

4

Yes, essentially you open a serial port device with a special name, such as COM1, and read and write to it much as you would a file. The pins used will (naturally) be the serial transmit and receive pins.

If you want to control specific pins but not necessarily in a serial way, you might be better off working with a parallel port instead. The parallel port voltages are usually more friendly to TTL level logic and can often be driven directly.

Update: If you just need to toggle one pin as per your comment, you may be able to use the DTR line for this. See the EscapeCommFunction function documentation for how to do this.

Sign up to request clarification or add additional context in comments.

5 Comments

The voltages are not the problem since the breadboard circuit will be able to handle anything from 3 to 25V. Ideally what I would like to do is to send a sustained +3V signal using a pin to the circuit. Can I still do this using a Serial port or will I need to switch to a Parallel port? Also, can you point me to any code reference? Does MSDN have any functionality for this other than CreateFile(...Com1...)?
@user318811: Greg's right: for anything like this, you almost certainly want to use the parallel port. The pins on the serial port are driven almost entirely by the UART, and it does so mostly under a clock, so it's generally impossible to hold a line in a given state for longer than a given period of time (basically, the time to transmit one byte at whatever speed you pick). You can slow the serial port down (e.g., to 300 baud) to get it to hold a state longer, but that will also limit the maximum transition rate (e.g., to ~300/second).
You can use the DTR line as a single bit control, it goes high when you open the port (in the correct mode) then low when you close it
DTR and RTS will work. You need a diode to protect the circuit against the negative voltage it outputs.
In terms of Parallel port programming, could give give me some references (keeping in mind that I would like specific pin control).
1

You can use WaitCommEvent function to monitor a specific pin. Suppose the voltage change triggers CTS signal, it can be like this

hCommn = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
......
 WaitCommEvent(hCommn, EV_CTS, NULL);
......

WaitCommEvent from MSDN

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.