Re: Serial port programming C++ - detect serial ports on linux
- From: Paul Taylor <paul_ng_pls_rem@xxxxxxxxxxxxx>
- Date: Wed, 27 Sep 2006 08:03:28 +0100
On Tue, 26 Sep 2006 23:04:12 +0200, Adib Taraben wrote:
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?
Hi,
I'm not sure if this is the best way to do it, but the code function below
is how I managed to get serial information.
Serial information is stored in the files:
"/proc/tty/driver/serial"
"/proc/tty/driver/usb-serial".
If you parse these files then you can see what you have on your system. A
small gotcha is that on most systems you need root privileges to access
this information (I don't know what the rational is for protecting this
info - I would be interested if anyone can give some reasoning). I believe
suse distros don't need root privileges. In my application, I needed a
small launcher program, so I wrote it suid, to get the contents of
the files, drop root privileges, and then launch my main program.
Regards,
Paul Taylor.
/*
* returns a long long, the lower 32 bits of which represent the serial
* ports of the system. Of the 32 bits, the lower 16 represent conventional
* serial ports and the upper 16 represent USB serial ports. For example,
* 0x00010004 inidcates that a system has one conventional port (ttyS2) and
* one USB port (ttyUSB0).
*
* If both files that contains the the serial port information cannot be
* read then -1 is returned.
*/
long long scan_ports()
{
char buf[256]; // enough to hold one line from serial devices list
char left_digit;
char right_digit;
int port_num, no_ttyS = 0, i;
long long bit_pos;
long long ports = 0;
FILE *f;
/*
* see if we have any traditional ttySx ports available
*/
f = fopen("/proc/tty/driver/serial", "r");
if (f != NULL) {
/* read in each line of the file */
while(fgets(buf, sizeof(buf), f) != NULL) {
/* if the line doesn't start with a number get the next line */
if (buf[0] < '0' || buf[0] > '9')
continue;
/*
* convert digits to an int
*/
left_digit = buf[0];
right_digit = buf[1];
if (right_digit < '0' || right_digit > '9')
port_num = left_digit - '0';
else
port_num = (left_digit - '0') * 10 + right_digit - '0';
/* skip if "unknown" in the string */
if (strstr(buf, "unknown") != NULL)
continue;
/* upper limit of 15 */
if (port_num > 15)
continue;
/*
* convert int to a bit postion
*/
bit_pos = 1;
for (i = 0; i < port_num; i++)
bit_pos = bit_pos << 1;
ports |= bit_pos;
}
fclose(f);
}
else
no_ttyS = 1;
/*
* see if we have any ttyUSBx ports available. Writes -1 if can't open
* any of the two files that hold available serial port information.
*/
f = fopen("/proc/tty/driver/usb-serial", "r");
if (f == NULL)
f = fopen("/proc/tty/driver/usbserial", "r");
if (no_ttyS && f == NULL)
return -1;
if (f != NULL) {
fgets(buf, sizeof(buf), f);
/* read in each line of the file */
while(fgets(buf, sizeof(buf), f) != NULL) {
/* if the line doesn't start with a number get the next line */
if (buf[0] < '0' || buf[0] > '9')
continue;
/*
* convert digits to an int
*/
left_digit = buf[0];
right_digit = buf[1];
if (right_digit < '0' || right_digit > '9')
port_num = left_digit - '0';
else
port_num = (left_digit - '0') * 10 + right_digit - '0';
/* upper limit of 15 */
if (port_num > 15)
continue;
/*
* convert int to a bit postion
*/
bit_pos = 0x10000;
for (i = 0; i < port_num; i++)
bit_pos = bit_pos << 1;
ports |= bit_pos;
}
fclose(f);
}
return ports;
}
.
- References:
- Re: Serial port programming C++
- From: none
- Re: Serial port programming C++ - detect serial ports on linux
- From: Adib Taraben
- Re: Serial port programming C++
- Prev by Date: Re: Serial port programming C++ - detect serial ports on linux
- Next by Date: Re: Passing shell control to another app
- Previous by thread: Re: Serial port programming C++ - detect serial ports on linux
- Next by thread: Passing shell control to another app
- Index(es):
Relevant Pages
|