Re: Serial port programming C++ - detect serial ports on linux



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;
}

.



Relevant Pages

  • Re: 1-wire to USB converter that can use 1820s directly
    ... than to need to add another PCI card to support serial ports. ... how someone could finally say "not one more f'ing wire!" ...
    (comp.home.automation)
  • Re: remote access KVM switch
    ... ->>seen so many Lightwave KVM switches around in server rooms. ... Some console servers even allow ssh access. ... -> very sophisticated, but it has 30 serial ports, telnet ...
    (comp.sys.sun.admin)
  • Re: How 2 connect old computer db9 port to new laptop with db15 or usb
    ... All serial ports are male (the pins ... has a DB9 male port and my new laptop has a DB15 male port and usb ports. ... Support for Flash drives and USB connected hard drives in Windows 98 ...
    (microsoft.public.windowsxp.hardware)
  • Re: Sun Microsystems Enterprise 3500 server
    ... are 5 ethernet ports to choose from. ... the same board that has the serial ports A & B. And there is another ... bottom of the front panel are an MII connector (ethernet which I have ... possibly Fibre Channel intterfaces called OLC j0 and OLC 1). ...
    (comp.sys.sun.hardware)
  • Re: Late to the party: Whats Rosetta?
    ... will have 5 full duplex serial ports, two TTL ports and one pin that can be ... The ZX-24 firmware can be updated over the serial link. ... The other four serial ports are 9600 MAX and will understand the ... communications protocols for the CM11A, CPU/XA, Ocelot, Leopard, Smarthome ...
    (comp.home.automation)