1
votes

I have a plugged usb-serial device plugged to my windows and serial mapped it using virtualbox e.g COM1 -> /dev/ttyS0..

Now how will i know which serial port my device is using.. I know right now im using /dev/ttyS0. but what if i don't know?.. Linux Debian is creating this permanent serial port devices on boot time /dev/ttyS0-S3.

How can i make a test that /dev/ttyS0 is the real port im using in c.

Here's my way of testing if it's the right port or not.

devfd=open("/dev/ttyS0",O_WRONLY | O_NOCTTY | O_NDELAY);

        if(s_fd<0) exit(1);

        printf("open\n");    //It will always return true printing open because this device is created on boot time and is always available. so i made another check and that is to write to the port(Assuming i have set the permission to have full access to the serial port). if i can write to the port then it means it is really the port im using.

test=write(devfd,"ATZ",4);

if(test<0) printf("Can't write to port: Maybe not the serial port ur using\n");

printf("Device is avaialable\n");  // returns true because we can write to the port

Can you show me other samples in c of how can Check serial port if there's a device plugged to that serial port?

Or a test in c to the following serial port /dev/ttyS0 - /dev/ttyS3 if the following have devices plugged on them.

Thanks.

2
If only this serial port is actively transmitting (or receiving) and all other ports are quiescent, then that activity should show up in the "tx:" transmitted byte counts for each serial port in the /proc/tty/driver/serial file.sawdust

2 Answers

0
votes

The Standard Serial ports are mapped as /ttyS0,/ttyS1,... as you correctly stated. Generic USB Serial Ports as well as most G3 modems are accessible as /dev/ttyUSB0 through /dev/ttyUSB255

The better way to distinguish if a serial port 'connected' with a modem is to send ATIx commands to the serial port. These are Identity commands that you may use to detect the model name of the device and many other details.

First you try to send ATIx command not changing the baud rate. If you do not receive any valid response (ASCII multi-line text followed by OK<CR><LF> ERROR...<CR><LF> then you may alter the baud rate and retry. It is better to first set the maximum supported by port speed and then decrease it until you find a modem or you end up with 110 baud or other reasonable limit.

There is a way to detect if most likely there is nothing connected to the serial port. The idea behind is to detect the frame error that persists during some reasonable time (tens times to receive a byte at selected baud rate), say 2-3sec. To see how enable/disable frame errors detection look at man termios. Howeever, I am not absolutely sure that in the setup you described this will be possible (from within VM), so you have to try.

Also look at this post How to connect to a terminal to Serial-USB device on Ubuntu 10.10?

0
votes

Be aware, that I found by experience that writing to a serial port to test it can have dire consequences; as my script hung (but only when running on ESXi hypervisor) when trying to test write to a disconnected device

In case it helps, here is a safer way to check

cat /proc/tty/driver/serial | \
    grep -v unknown | \
    sed 's/^/ttyS/g'

and then check output for ttyS0 for example