1

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.

Serge
  • 6,088
  • 17
  • 27
demic0de
  • 1,313
  • 1
  • 15
  • 30
  • 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 Oct 02 '12 at 09:27

2 Answers2

0

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?

Community
  • 1
  • 1
Serge
  • 6,088
  • 17
  • 27
  • Thanks for that Serge. but what if i'm not using modems? e.g like embedded systems that is connected to the said ports how will i know if the serial port is avaialable or not?. Thanks again for answering. – demic0de Oct 02 '12 at 02:00
  • term `embedded` is too wide. Generally speaking if you do not know how the device should respond you can only make an assumption that something is plugged in by analyzing the state of `CD`, `DSR` and `CTS` signals. When there is nothing connected all of them in the same state. However, this is really not reliable. – Serge Oct 02 '12 at 02:09
  • THanks.. Is my previous way of doing it okay?.. opening and writing to the port? if i can write then it means something is plugged to that serial port? – demic0de Oct 02 '12 at 02:11
  • 1
    Your previous way is OK but I would recommend you ATI1,ATI2,e.t.c. I just came back as I recalled one thing. I will update the answer in a couple of minutes – Serge Oct 02 '12 at 02:18
  • 2
    @demic0de: The root of your problem is that ultimately the serial port hardware itself doesn't really know if there's anything plugged into it or not. – caf Oct 02 '12 at 06:42
  • @caf - Yes that's my real problem. – demic0de Oct 03 '12 at 00:33
0

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

Goblinhack
  • 2,859
  • 1
  • 26
  • 26