Re: Interesting problem with Serial Port

From: Chuck Dillon (spam_at_nimblegen.com)
Date: 01/28/05


Date: Fri, 28 Jan 2005 08:26:54 -0600

Kazman wrote:
> I have an interesting problem I have come across recently. So I am
> running NetBSD, and I have written a TCP/IP server which takes commands
> from a client PC, passes it on to a radio receiver, and then parses the
> results from the receiver.
>
> So first I wrote the code to hook up with the serial port and
> communicate with the radio. It worked just fine. So then I added the
> code to do the TCP/IP communications. Now I can't use the serial port.
> I can open it up, but the data I write never goes out the serial port.
> I can change settings with no problem. My write operations return
> saying the bytes were written, but hooking the port up to a hex
> terminal shows nothing goes out. And all of my read functions return
> -1.

What does errno say when read() returns -1? My guess is that code
elsewhere is doing something to your open file handle. Could there be
initialization code elsewhere that is closing open files. Just a guess
but I'd say something like that is happening and the descriptior you
have is now associated with a write only stream.

Use a system call tracer (truss, strace don't know what it's called on
NetBSD) and see what is going on.

-- ced

>
> So I made a new program with just the serial port communications code.
> Sure enough, all alone without the TCP/IP code, I was able to write and
> receive data on the serial port.
>
> Any ideas why this would be happening? I'm simply blown away by this
> problem. I've never seen anything like it before. The code in the
> stand alone serial port program is exactly the same as the code in the
> server program. Yet one works and the other doesn't.
>
> Also, I can't seem to get the read command to block for a long time(10
> seconds). I shall post my settings below. Is there anything I'm doing
> wrong? Any help or advice would be greatly appreciated! Thanks for
> your time!
>
> /* Set up the serial port */
> portfd = open(portname, O_RDWR | O_NOCTTY);
> if (portfd < 0)
> {
> fprintf(stderr, "open_rs485: failed to open device %s", portname);
> exit(-1);
> }
> settings.c_cflag=0;
> cfsetispeed(&settings, B9600);
> cfsetospeed(&settings, B9600);
> settings.c_cflag |= (CREAD | CLOCAL);
> settings.c_cflag &= ~PARENB;
> settings.c_cflag &= ~CSTOPB;
> settings.c_cflag &= ~CSIZE;
> settings.c_cflag |= CS8;
> settings.c_cflag &= ~CRTSCTS;
> settings.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
> settings.c_oflag &= ~(IXON | IXOFF | IXANY);
> settings.c_oflag &= ~OPOST;
> settings.c_cc[VMIN]=0;
> settings.c_cc[VTIME]=100;
> tcflush(portfd, TCIFLUSH);
> tcflush(portfd, TCOFLUSH);
> tcsetattr(portfd,TCSANOW,&settings);
>

-- 
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.


Relevant Pages

  • Re: Linux "print server"
    ... porting Ghostscrip to a C64 is ... so a Linux server seemed possible. ... "> I already have TCP/IP communication using ppp between the C64 and the ... "> DTP app I use can send data to a serial port, ...
    (comp.os.linux.misc)
  • Re: Adding second NIC
    ... Not sure what you mean by "unneeded overhead". ... Note that to function as a keyboard, ... to do reliable serial port transmission at 115200bps, ... TCP/IP works really well at distances. ...
    (microsoft.public.vc.mfc)
  • Interesting problem with Serial Port
    ... and I have written a TCP/IP server which takes commands ... code to do the TCP/IP communications. ... Now I can't use the serial port. ...
    (comp.unix.programmer)
  • Re: Linux "print server"
    ... so a Linux server seemed possible. ... I already have TCP/IP communication using ppp between the C64 and the ... What you describe here is a TCP/IP network. ... DTP app I use can send data to a serial port, ...
    (comp.os.linux.misc)