Re: Serial port programming C++ - detect serial ports on linux
- From: Adib Taraben <taraben.a@xxxxxxxxxxxxxxxxx>
- Date: Tue, 26 Sep 2006 23:04:12 +0200
How can I detect the serial ports on linux system?
On w32 there is a registry setting for that.
Is there something similar on linux? Or do I have to poll all possible devices and see which answers?
Thx,
Adib.
none schrieb:
Hello,.
I have no actual solution, but take a look at this Serial Port (C) library:
http://sourceforge.net/project/showfiles.php?group_id=34004
+
http://www.easysw.com/%7Emike/serial/
Jim wrote:Hi,
I'm new to C++ and am trying to write a program to control a relay board
through ttyS0. The relay board requires a specific baud rate (2400) 8 bits
and no parity so I'm using termios.h to control this, and the code is based
on / taken from the Serial Programming Guide for POSIX Operating Systems.
The board is wired to an LED array so I can clearly see what's going on. I
can open, write arbitrary data, get random LEDs turning on and off, and
close the port fine. It works for arbitrary data as long as I do not change
any termios options (ie comment out the options), using tcgetattr to get
the options is fine and using tcsetattr to set the options is fine provided
I don't try to change the options. However to properly control the relays I
need to send specific data and so I need to set the baud rate, character
size and parity.
If I do change the termios options I can't access the port at all, although
no errors are given. The only way I can get back to sending arbitrary data
is a reboot. The program always seems to complete OK and the data string
checks OK, I just seem to lose my port.
Any ideas anyone? Any help very gratefully received!
Below is the code I'm using:
#include <stdio.h> /* Standard input/output definitions */
#include <iostream> /* JB's own addition */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
// #include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
int serial_port;
int write_port;
int close_port;
char data_string[2];
bool relay[8] = {1,1,0,1,0,0,0,0};
bool addr[8] = {0,1,0,0,0,0,0,1};
int main()
{
// Build character string to send
data_string[0] = (addr[0]*128) + (addr[1]*64) + (addr[2]*32) +
(addr[3]*16) + (addr[4]*8) + (addr[5]*4) + (addr[6]*2) + (addr[7]*1);
data_string[1] = (relay[0]*128) + (relay[1]*64) + (relay[2]*32) +
(relay[3]*16) + (relay[4]) + (relay[5]) + (relay[6]) + (relay[7]);
/* serial_port = File Descriptor for the port */
serial_port = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); /* read/write | not controlling term | don't wait for DCD line signal
state */
if (serial_port == -1) {
/* Can't open port */
std::cout << "Can't open port\n";;
}
else
fcntl(serial_port, F_SETFL, FNDELAY);
std::cout << "opening port\n"; struct termios options, oldoptions;
tcgetattr(serial_port, &oldoptions); /* Save the original options for
the port... */ tcgetattr(serial_port, &options); /* Get the current options for the
port... */
cfsetispeed(&options, B2400); /* Set the baud rates to 2400... */
cfsetospeed(&options, B2400);
options.c_cflag &= ~CSIZE; /* Mask the character size bits */
options.c_cflag |= CS8; /* Select 8 data bits */
options.c_cflag &= ~PARENB /* Set parity to 0, 8 bits per character and
1 stop bit */
options.c_cflag &= ~CSTOPB
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag |= (CLOCAL | CREAD); /* Enable the receiver and set
local mode*/
tcsetattr(serial_port, TCSANOW, &options); /* Set the new options for
the port...*/
std::cout << "\nSent string is: " << data_string << "\n\n";
write_port = write(serial_port, data_string, 2); /* Write string "ATZ\r" to specified serialport , 4 (JBvariable) = no.
chars to write */
if (write_port < 0) { std::cout << "failed to write to port\n";
}
std::cout << "sending data\n";
close_port = close(serial_port);
if (close_port < 0 ) {
std::cout << "failed to close port\n";
}
else std::cout << "port closed\n";
tcsetattr(serial_port, TCSANOW, &oldoptions); /* Restore the old options
for the port...*/ return(0);
}
- Follow-Ups:
- Re: Serial port programming C++ - detect serial ports on linux
- From: Paul Taylor
- Re: Serial port programming C++ - detect serial ports on linux
- From: Lars Rune Nøstdal
- Re: Serial port programming C++ - detect serial ports on linux
- References:
- Re: Serial port programming C++
- From: none
- Re: Serial port programming C++
- Prev by Date: Re: Passing shell control to another app
- Next by Date: Re: scope of linux in the corporates...
- Previous by thread: Re: Serial port programming C++
- Next by thread: Re: Serial port programming C++ - detect serial ports on linux
- Index(es):
Relevant Pages
|