Enumerating serial ports
Posted: Sat Feb 09, 2008 5:35 pm
I have an application, originally written in VisualBasic that I have about 90% ported to PB so I can release it for both Windows and Linux users. It communicates with an embedded device using RS232.
Under Windows, there is an API function (EnumPorts) which returns a list of all the printer ports. Since all the serial ports are a subset of the printer ports, all I have to do is filter out those that are not COM ports, are already in use (see Note below), or cannot be opened. I present the list of available ports in a menu for the user to select a port. This has proven to be reliable and (thanks to ABB's help) it's working under PB/Windows.
As I want to keep the look and feel as similar as possible on all platforms, I would like to do something similar under Linux. From a terminal, I can manually get information about the ports with 'dmesg | fgrep tty' and can pipe the output to a file, if necessary. I would like to find a way to do this automatically when my application starts. So far, I have not found a way to use RunProgram and either catch its output or pipe it to a file which I can then parse to find the ports. I'm a complete novice when it comes to Linux and am not even sure whether 'fgrep' is a commandline parameter or is a separate program.
Is there a way to do this under Linux using RunProgram or any other method?
Note: While testing serial comms under Linux, I accidentally discovered that more than one program can open and control a port at the same time. As neat as this might be in certain circumstances, I want to put a stop to such fraternizing. How do I block other apps from accessing a port once it's opened? The test code is in my first post to the thread Serial Communications via API http://www.purebasic.fr/english/viewtopic.php?t=16847 in this forum. If you run two instances, both toggle DTR.
Under Windows, there is an API function (EnumPorts) which returns a list of all the printer ports. Since all the serial ports are a subset of the printer ports, all I have to do is filter out those that are not COM ports, are already in use (see Note below), or cannot be opened. I present the list of available ports in a menu for the user to select a port. This has proven to be reliable and (thanks to ABB's help) it's working under PB/Windows.
As I want to keep the look and feel as similar as possible on all platforms, I would like to do something similar under Linux. From a terminal, I can manually get information about the ports with 'dmesg | fgrep tty' and can pipe the output to a file, if necessary. I would like to find a way to do this automatically when my application starts. So far, I have not found a way to use RunProgram and either catch its output or pipe it to a file which I can then parse to find the ports. I'm a complete novice when it comes to Linux and am not even sure whether 'fgrep' is a commandline parameter or is a separate program.
Is there a way to do this under Linux using RunProgram or any other method?
Note: While testing serial comms under Linux, I accidentally discovered that more than one program can open and control a port at the same time. As neat as this might be in certain circumstances, I want to put a stop to such fraternizing. How do I block other apps from accessing a port once it's opened? The test code is in my first post to the thread Serial Communications via API http://www.purebasic.fr/english/viewtopic.php?t=16847 in this forum. If you run two instances, both toggle DTR.