Looking for basic documentation on Cygwin and Serial Ports

View: New views
14 Messages — Rating Filter:   Alert me  

Looking for basic documentation on Cygwin and Serial Ports

by Nefastor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi everyone,

I've been Google-ing like crazy to find some information which I believe deserves its own HOWTO or FAQ, ultimately I registered here to ask. I apologize if it's something obvious or that has been asked so many times everyone is bored, if that's the case I only need a pointer, and you'll have my eternal gratitude.

Here goes :

I want to write simple programs which use the serial port (for example, a program that will trap and log activity on a serial port, nothing fancy). I know how to do that under Linux, and under DOS as well. The problem is I don't know which of Cygwin's /dev/tty device corresponds to which serial port on my PC (that is, I only know the COM port number). I can "sort of guess" /dev/tty0 is COM1, but that's a guess, and I'd prefer some certainty.

It gets complicated : I'd like my programs to work with any kind of RS-232 port, especially USB adapters (FTDI chips) and USB Bluetooth dongles. While Google-ing around I found mention of exotic devices such as /dev/ttyUSB0 or even /dev/usb/tty, and what I COULDN'T find was a clear explanation of which exist and which work in Cygwin.

Unless it's already been done, can someone explain the COM to /dev/tty matching, as well as any steps that a user might have to take to make a serial port available in /dev ?

Thank you very much :-)

Nefastor

(P.S. : I'm French so if you have any doubt about what exactly I'm asking, don't hesitate to ask me : my English is a bit lacking.)

Re: Looking for basic documentation on Cygwin and Serial Ports

by Brian Dessent :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Nefastor wrote:

> I want to write simple programs which use the serial port (for example, a
> program that will trap and log activity on a serial port, nothing fancy). I
> know how to do that under Linux, and under DOS as well. The problem is I
> don't know which of Cygwin's /dev/tty device corresponds to which serial
> port on my PC (that is, I only know the COM port number). I can "sort of
> guess" /dev/tty0 is COM1, but that's a guess, and I'd prefer some certainty.

Cygwin works the same as Linux, /dev/ttyS0 is the first serial device,
/dev/ttyS1 is the second, etc.  Please consult the users guide:
<http://cygwin.com/cygwin-ug-net/using-specialnames.html>.

> It gets complicated : I'd like my programs to work with any kind of RS-232
> port, especially USB adapters (FTDI chips) and USB Bluetooth dongles. While
> Google-ing around I found mention of exotic devices such as /dev/ttyUSB0 or
> even /dev/usb/tty, and what I COULDN'T find was a clear explanation of which
> exist and which work in Cygwin.

It will still be /dev/ttyS<number> for USB devices, just with a number
like 9 or whatever, corresponding to whatever emulated serial device it
is configured as (e.g. COM10 in this case.)

Brian

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Re: Looking for basic documentation on Cygwin and Serial Ports

by Nefastor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Brian Dessent wrote:
Nefastor wrote:

> (...) I don't know which of Cygwin's /dev/tty device corresponds to which serial
> port on my PC (that is, I only know the COM port number). I can "sort of
> guess" /dev/tty0 is COM1, but that's a guess, and I'd prefer some certainty.

Cygwin works the same as Linux, /dev/ttyS0 is the first serial device,
/dev/ttyS1 is the second, etc.  Please consult the users guide:
<http://cygwin.com/cygwin-ug-net/using-specialnames.html>.
That I have, Brian. What itches me is that I'm facing some kind of special case : My machine has two serial port, one on the MoBo and a USB-serial adapter, yet Windows calls them COM1 and COM3. I've searched the whole Device Manager but couldn't find any device that would be COM2. And I can rename COM1 to COM2.

Speaking of Windows' DM, it's rather quirky on the serial ports : I noticed that if I unplug my USB cable (COM3) and try to change the name of COM1, in the drop down box COM3 is listed as "in use"  .

So, I have a machine with two serial ports but no COM2, AND I can change the COM port number to whatever I want, plus Windows seems to have a bad case of "phantom limb syndrome". Getting back to your answer : how can know for sure what Cygwin calls "the first port", or "the second port" ?

Trial and error is obviously not what I'm after, rather I need a way for my program to know for sure what port it's dealing with. To help me write that program I'd need to know :

- is there a fixed relationship between COM port number "X" and dev/ttyS number "Y", such as Y = X - 1 ? For instance, if I change my COM port number from 3 to 7, will the dev/ttyS number go from 2 to 6, or will it remain unchanged ?

- if it remains unchanged, I'm assuming Cygwin does some sort of port enumeration : is that process deterministic ?

Additionally, can you point me to documentation on how to retrieve device / driver info from my program ? That way, worst case scenario, I can have the program look for the correct port on its own.

Thank you for your time, I know I'm asking for a lot of stuff but I've got the feeling the answers would be useful to many.

Nefastor

Re: Looking for basic documentation on Cygwin and Serial Ports

by NightStrike :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 4/24/08, Nefastor <nefastor@...> wrote:

>
>
> Brian Dessent wrote:
> >
> > Nefastor wrote:
> >
> >> (...) I don't know which of Cygwin's /dev/tty device corresponds to which
> >> serial
> >> port on my PC (that is, I only know the COM port number). I can "sort of
> >> guess" /dev/tty0 is COM1, but that's a guess, and I'd prefer some
> >> certainty.
> >
> > Cygwin works the same as Linux, /dev/ttyS0 is the first serial device,
> > /dev/ttyS1 is the second, etc.  Please consult the users guide:
> > <http://cygwin.com/cygwin-ug-net/using-specialnames.html>.
> >
> >
>
> That I have, Brian. What itches me is that I'm facing some kind of special
> case : My machine has two serial port, one on the MoBo and a USB-serial
> adapter, yet Windows calls them COM1 and COM3. I've searched the whole
> Device Manager but couldn't find any device that would be COM2. And I can
> rename COM1 to COM2.
>
> Speaking of Windows' DM, it's rather quirky on the serial ports : I noticed
> that if I unplug my USB cable (COM3) and try to change the name of COM1, in
> the drop down box COM3 is listed as "in use" :confused: %-|.
>
> So, I have a machine with two serial ports but no COM2, AND I can change the
> COM port number to whatever I want, plus Windows seems to have a bad case of
> "phantom limb syndrome". Getting back to your answer : how can know for sure
> what Cygwin calls "the first port", or "the second port" ?
>
> Trial and error is obviously not what I'm after, rather I need a way for my
> program to know for sure what port it's dealing with. To help me write that
> program I'd need to know :
>
> - is there a fixed relationship between COM port number "X" and dev/ttyS
> number "Y", such as Y = X - 1 ? For instance, if I change my COM port number
> from 3 to 7, will the dev/ttyS number go from 2 to 6, or will it remain
> unchanged ?
>
> - if it remains unchanged, I'm assuming Cygwin does some sort of port
> enumeration : is that process deterministic ?
>
> Additionally, can you point me to documentation on how to retrieve device /
> driver info from my program ? That way, worst case scenario, I can have the
> program look for the correct port on its own.
>
> Thank you for your time, I know I'm asking for a lot of stuff but I've got
> the feeling the answers would be useful to many.:-)

What motherboard do you have?  It's always possible that there is a
riser pinout for a serial port somewhere in the front of the board for
connection to a front panel mount.

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


RE: Looking for basic documentation on Cygwin and Serial Ports

by Dave Korn :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Nefastor wrote on 24 April 2008 23:08:

> case : My machine has two serial port, one on the MoBo and a USB-serial
> adapter, yet Windows calls them COM1 and COM3.

  Or /dev/ttyS0 and /dev/ttyS2, as they'll be known to cygwin.

> Speaking of Windows' DM, it's rather quirky on the serial
> ports : I noticed
> that if I unplug my USB cable (COM3) and try to change the
> name of COM1, in
> the drop down box COM3 is listed as "in use" :confused: %-|.

  AAAARGH[*]!  FTDIchip are the bane of my life at the moment.  Buggy drivers.
I hate them so much <spit>.

> - is there a fixed relationship between COM port number "X"
> and dev/ttyS
> number "Y", such as Y = X - 1 ?

  Yes.

> For instance, if I change my
> COM port number
> from 3 to 7, will the dev/ttyS number go from 2 to 6,

  Yes.

> Additionally, can you point me to documentation on how to
> retrieve device /
> driver info from my program ? That way, worst case scenario,
> I can have the
> program look for the correct port on its own.

  Don't understand what kind of "device/driver info" you mean here.  You might
be able to use the win32 pnp api functions.

    cheers,
      DaveK

[*] - I just got back from rebooting a testrig that locked up due to buggy
ftdichip drivers at about the twentytwo-hour point into a twentyseven-hour
testrun.  I am not happy.
--
Can't think of a witty .sigline today....



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Re: Looking for basic documentation on Cygwin and Serial Ports

by Nefastor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

NightStrike wrote:
What motherboard do you have?  It's always possible that there is a
riser pinout for a serial port somewhere in the front of the board for
connection to a front panel mount.
No, I checked for that. Besides, this port would appear in Windows' device manager even if it wasn't brought out of the board. I also explored the possibility of a serial port used internally to connect to some type of monitoring chip, but nothing there either. The board is a fairly old micro-ATX with fairly limited connectivity (hence the USB-to-serial cable).

Nefastor

RE: Looking for basic documentation on Cygwin and Serial Ports

by Nefastor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Dave, I kinda suspected the FTDI drivers for the COM3 issue (I've seen worse : once, the COM port number would be incremented every time the cable was unplugged and replugged). Unfortunately I'm stuck with FTDI chips as they are, AFAIK, the only USB-to-serial adapters I can buy as chips and solder myself.

The device and/or driver info I was asking about is just that : whatever system variable(s) hold the information about the hardware present on the PC. I'm no Windows programmer (though not for lack of training), I suppose I can find my way from the win32 pnp API. Thank you for the pointer.

The program I'm trying to write is for an autonomous machine. Normally I'd design my own single-board computer for maximum reliability but this time I need the functionality of an OS like Windows or Linux. I'm trying to make the behavior of that OS as predictable as possible, because the end-product will have to operate away from someone who could "CTRL-ALT-DEL" it (and it would really be overkill to install VNC on my application )

Thanks for all the info, everyone . I'm gonna try a few things and get back to you.

Nefastor

RE: Looking for basic documentation on Cygwin and Serial Ports

by Dave Korn :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Nefastor wrote on 25 April 2008 17:26:

> Thanks Dave, I kinda suspected the FTDI drivers for the COM3 issue

  One tip I've learned is never, ever, ever, unplug them while you're still
running your terminal software (or whatever) and it's got the port open.
Disconnect, or exit the application, or you're screwed until you reboot.

  However, discussion of their many and varied flaws is getting fairly far
off-topic because it's not cygwin-specific; if you'd like to pursue that
angle, we should TITTTL.

    cheers,
      DaveK
--
Can't think of a witty .sigline today....


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Re: Looking for basic documentation on Cygwin and Serial Ports

by Charles Wilson-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dave Korn wrote:

>   AAAARGH[*]!  FTDIchip are the bane of my life at the moment.  Buggy drivers.
> I hate them so much <spit>.
>
> [*] - I just got back from rebooting a testrig that locked up due to buggy
> ftdichip drivers at about the twentytwo-hour point into a twentyseven-hour
> testrun.  I am not happy.

Wow. I've been using them too, but have had no problems at all
(WinXPsp2) from native consoles (no cygwin involved, if that matters).
Then again, I wasn't trying to run 27 hour tests.

--
Chuck



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


RE: Looking for basic documentation on Cygwin and Serial Ports

by Nefastor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Nefastor wrote:
Thanks for all the info, everyone . I'm gonna try a few things and get back to you.
So I've tried a few things, and obviously all hell broke loose, sort of. I've written a very simple "Hello World" program, which I'll paste at the end of this message. The program is based 90% on code copy-pasted directed from the serial programming HOWTO. Here's what it does :

- Open COM3
- Get its attribute
- Modify them to my desired baudrate and such
- Send out the string "Hello World"
- Close COM3

Opening and closing COM3 works. The rest exhibits faulty but consistent behavior :

- The baudrate never changes and I can't seem to figure out what it is, but the terminal I plugged on COM3 always gets the same garbled characters instead of "Hello World". Also, the terminal flags me a buffer overrun. This suggested that tcsetattr() didn't work.

- So I tested tcsetattr() by omitting the CRTSCTS flag in my parameters. With it, the program would not send data until the terminal was ready to receive, without it, the program would send the data no matter what. So tcsetattr() appears to work.

My terminal is an ultra-reliable PSION Series 3A I've been using for almost a decade. I've tested it again with Windows' Hyperterminal and COM3 to verify that it wasn't a hardware problem.

My conclusion would be that either the FTDI drivers are broken or Cygwin has trouble interfacing with them. Dave, since you're an FTDI user too, does any of this sound familiar ?

Here's the program I've written :

#include <stdlib.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>

int main (int argc, char* argv[])
{
  struct termios params;  
  int tty;              

  tty = open ("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NONBLOCK);
 
  if (tty < 0)
   {
      printf ("Unable to open /dev/ttyS2\n");
      exit (1);
   }
   
  tcgetattr (tty,¶ms);    // get the current port settings
  params.c_cflag = B19200 | CRTSCTS | CS8 | CLOCAL | CREAD;
  params.c_iflag = IGNPAR;
  params.c_oflag = 0;
  params.c_lflag = ICANON;
  tcflush (tty, TCIFLUSH);
  tcsetattr (tty,TCSANOW,¶ms);
   
  write (tty,"Hello World",11);
 
  close (tty);

  return 0;
}


Re: Looking for basic documentation on Cygwin and Serial Ports

by Gary Johnson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 2008-05-01, Nefastor wrote:

> Nefastor wrote:
> >
> > Thanks for all the info, everyone :-D. I'm gonna try a few things and get
> > back to you.
> >
>
> So I've tried a few things, and obviously all hell broke loose, sort of.
> I've written a very simple "Hello World" program, which I'll paste at the
> end of this message. The program is based 90% on code copy-pasted directed
> from the serial programming HOWTO. Here's what it does :
>
> - Open COM3
> - Get its attribute
> - Modify them to my desired baudrate and such
> - Send out the string "Hello World"
> - Close COM3
>
> Opening and closing COM3 works. The rest exhibits faulty but consistent
> behavior :
>
> - The baudrate never changes and I can't seem to figure out what it is, but
> the terminal I plugged on COM3 always gets the same garbled characters
> instead of "Hello World". Also, the terminal flags me a buffer overrun. This
> suggested that tcsetattr() didn't work.

I ported a program using a serial port from HP-UX to Cygwin a while
ago and ran into similar problems:  everything seemed to work OK
except for the speed settings.  I don't remember the issues exactly,
but I finally got it to work by setting the speeds using
cfsetispeed() and cfsetospeed() instead of setting the speed bits of
c_cflag directly.

Applying this to your program,

>   tcgetattr (tty,¶ms);    // get the current port settings
>   params.c_cflag = B19200 | CRTSCTS | CS8 | CLOCAL | CREAD;
>   params.c_iflag = IGNPAR;
>   params.c_oflag = 0;
>   params.c_lflag = ICANON;
>   tcflush (tty, TCIFLUSH);
>   tcsetattr (tty,TCSANOW,¶ms);

would become this:

    tcgetattr (tty,¶ms);    // get the current port settings
    params.c_cflag = CRTSCTS | CS8 | CLOCAL | CREAD;
    params.c_iflag = IGNPAR;
    params.c_oflag = 0;
    params.c_lflag = ICANON;
    cfsetispeed(¶ms, B19200);
    cfsetospeed(¶ms, B19200);
    tcflush (tty, TCIFLUSH);
    tcsetattr (tty,TCSANOW,¶ms);

HTH,
Gary


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Re: Looking for basic documentation on Cygwin and Serial Ports

by Nefastor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Your trick worked as advertised, and on the first try, thank you very much

Anyone know where this problem with tcsetattr() comes from ? Is it documented or specific to Cygwin ? (or both ?)

Nefastor

Re: Looking for basic documentation on Cygwin and Serial Ports

by Corinna Vinschen-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On May  2 20:27, Nefastor wrote:
>
> Your trick worked as advertised, and on the first try, thank you very much
> :handshake:
>
> Anyone know where this problem with tcsetattr() comes from ? Is it
> documented or specific to Cygwin ? (or both ?)

The reason is probably:  Nobody ever implemented it.  If you like to
change that, feel free to contribute.  See http://cygwin.com/contrib.html


Corinna

--
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          cygwin AT cygwin DOT com
Red Hat

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Re: Looking for basic documentation on Cygwin and Serial Ports

by Christopher Faylor-8 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, May 07, 2008 at 12:03:19PM +0200, Corinna Vinschen wrote:

>On May  2 20:27, Nefastor wrote:
>>
>> Your trick worked as advertised, and on the first try, thank you very much
>> :handshake:
>>
>> Anyone know where this problem with tcsetattr() comes from ? Is it
>> documented or specific to Cygwin ? (or both ?)
>
>The reason is probably:  Nobody ever implemented it.  If you like to
>change that, feel free to contribute.  See http://cygwin.com/contrib.html

I actually do look at this whenever it comes up and IIRC it isn't extremely
easy to implement.

cgf

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/