问题
There are a lot of examples which shows on how to communicate through the serial port of the pc. But is there a way to configure the pins of rs 232? I just need to set the tx pin for some time and then reset it and so on. is there a way to find the address of the pins of rs 232? Thank you. If there is an address then how can we access the pin or change the state of the pin in that address?
回答1:
Control Pins
For the other pins DTR CTS etc, you will need to use ioctl() to toggle the pin.
Here is a simple example (no error checking) to do that for the DTR line:
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
int f = open( "/dev/ttyS0", O_RDWR | O_NOCTTY);
int pins;
ioctl( f, TIOCMGET, &pins);
pins = pins | TIOCM_DTR;
ioctl( f, TIOCMSET, &pins) // the order you do this depends
sleep(1);
ioctl( f, TIOCMGET, &pins);
pins = pins & ~TIOCM_DTR;
ioctl( f, TIOCMSET, &pins)
The various flags are described in the man page for open and tty_ioctl
Transmit Pin
Using the TX pin is probably a bit tricker; in theory the output is normally 1, but then you can set a 'break' for a period of time which set it to 0. You could probably use the following, but I havent tried it:
ioctl( f, TIOCSBRK)
Caution
Note that in traditional rs232 the levels are notionally +/- 12v ( between +/-3,15V) where negative is 1 and positive is zero, which might be the opposite of what you are expecting. But these days a lot of serials port use TTL or 3v3 levels instead.
I used the above in an application where we used DTR as an output GPIO; remember to use appropriate resistors or other buffering as needed, so you don't blow up your PC serial port.
YMMV with USB serial dongles.
回答2:
If you are not restricted to RS232 only. You have other options
First, if you PC still have parallel port, it would be a better choice over RS232.
Or, you can use some some USB-GPIO modules. Some suggestion:
- FTDI bitbang mode
- 8 CHANNEL USB GPIO MODULE
来源:https://stackoverflow.com/questions/22059104/rs-232-pin-configuration-in-linux-pc