All of lore.kernel.org
 help / color / mirror / Atom feed
* Trouble reading from my serial device (a microcontroller)
@ 2003-11-20 19:22 Chuck Messenger
  2003-11-20 21:07 ` Chuck Messenger
  0 siblings, 1 reply; 3+ messages in thread
From: Chuck Messenger @ 2003-11-20 19:22 UTC (permalink / raw)
  To: linux-serial

I'm unable to read from a serial device under Linux (Mandrake 9.1, 
2.4.21-0.13mdk kernel), but the device works find in Windows.  I'm 
hoping someone can offer me suggestions.

My device is a Basic Stamp microcontroller, which I've programmed to 
spit out a certain byte continuously -- 0x40 -- at 38400 baud,n,8,1. The 
device loops RTS back to DSR (i.e. it connects pins 6 and 7 of the 9-pin 
serial connector -- DSR and RTS).  Other than that, only Rx and Tx are 
connected (and ground, of course).

I know the hardware works (both controller and PC), because I can boot 
Win2k on the same machine and run a short program I wrote which 
continuously dumps to the console whatever comes in.

Also, I know that things are _basically_ configured right, in my Linux 
boot, since I can run "statserial /dev/tts/0", which shows, in real 
time, the status of the DSR line.  When I unplug from my device, DSR 
goes low; when I plug it back in, DSR goes high.

I tried writing a simple port-dumping program on Linux, based on Peter 
Baumann's sample in his Serial Programming Howto:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>

#define BAUDRATE B38400
#define MODEMDEVICE "/dev/tts/0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */

int
main()
{
     int fd,c, res;
     struct termios oldtio,newtio;

     fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );

     if (fd < 0) {
         perror(MODEMDEVICE);
         exit(-1);
     }

     tcgetattr(fd, &oldtio); /* save current port settings */

     memset(&newtio, 0, sizeof(newtio));

     newtio.c_cflag = BAUDRATE | /*CRTSCTS |*/ CS8 | CLOCAL | CREAD;
     newtio.c_iflag = IGNPAR   // ignore parity errors
                    | IGNBRK;  // ignore BREAK characters
     newtio.c_oflag = 0;

     /* set input mode (non-canonical, no echo,...) */

     newtio.c_lflag = 0;

     newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
     newtio.c_cc[VMIN]     = 1;   /* blocking read until 1 chars received */

     tcflush(fd, TCIFLUSH);       // Flush an data received by not read
     tcsetattr(fd,TCSANOW,&newtio);

     while (1) {       /* loop for input */
         char buf[1];
         res = read(fd,buf,1);   /* returns after 1 chars have been input */
         printf("%02x ", buf[0]);
         fflush(stdout);
     }

     tcsetattr(fd,TCSANOW,&oldtio);

     return 0;
}


However, the program fails to read any bytes -- it hangs at the read().

I've tried running serlook, but that, too, failed to see any bytes.

Any suggestions?

I've tried 9600 baud instead of 38400 -- still no luck (although it 
works under Win2k).

Poking around, I've seen mention of the DCD control line -- could that 
be the problem (i.e. does the Linux serial driver require it, somehow? 
I don't have it hooked up.)


     - Chuck Messenger



^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: Trouble reading from my serial device (a microcontroller)
@ 2003-11-20 20:42 Ed Vance
  0 siblings, 0 replies; 3+ messages in thread
From: Ed Vance @ 2003-11-20 20:42 UTC (permalink / raw)
  To: 'Chuck Messenger'; +Cc: linux-serial

On Thu, Nov 20, 2003 at 11:23 AM, Chuck Messenger wrote:
> I'm unable to read from a serial device under Linux (Mandrake 9.1, 
> 2.4.21-0.13mdk kernel), but the device works find in Windows.  I'm 
> hoping someone can offer me suggestions.
> 
> My device is a Basic Stamp microcontroller, which I've programmed to 
> spit out a certain byte continuously -- 0x40 -- at 38400 
> baud,n,8,1. The 
> device loops RTS back to DSR (i.e. it connects pins 6 and 7 
> of the 9-pin 
> serial connector -- DSR and RTS).  Other than that, only Rx 
> and Tx are 
> connected (and ground, of course).
> 
> I know the hardware works (both controller and PC), because I 
> can boot 
> Win2k on the same machine and run a short program I wrote which 
> continuously dumps to the console whatever comes in.
> 
> Also, I know that things are _basically_ configured right, in 
> my Linux 
> boot, since I can run "statserial /dev/tts/0", which shows, in real 
> time, the status of the DSR line.  When I unplug from my device, DSR 
> goes low; when I plug it back in, DSR goes high.
> 
[snip]
> 
> However, the program fails to read any bytes -- it hangs at 
> the read().
> 
> I've tried running serlook, but that, too, failed to see any bytes.
> 
> Any suggestions?
> 
> I've tried 9600 baud instead of 38400 -- still no luck (although it 
> works under Win2k).
> 
> Poking around, I've seen mention of the DCD control line -- 
> could that 
> be the problem (i.e. does the Linux serial driver require it, 
> somehow? 
> I don't have it hooked up.)
> 
Hi Chuck,

Your program works fine on my Red Hat 7.3 setup. A difference is that all of
my modem status lines are driven. (could not find my breakout box) Try
connecting your serial port's DTR output to its DCD input. Perhaps the
program is hanging on the open instead of the read, because DCD is detected
low and you are not opening with O_NONBLOCK (or O_NDELAY).

cheers,
Ed

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Trouble reading from my serial device (a microcontroller)
  2003-11-20 19:22 Trouble reading from my serial device (a microcontroller) Chuck Messenger
@ 2003-11-20 21:07 ` Chuck Messenger
  0 siblings, 0 replies; 3+ messages in thread
From: Chuck Messenger @ 2003-11-20 21:07 UTC (permalink / raw)
  To: linux-serial

OK, I figured it out.  Yeah, I'm having a conversation with myself.  But 
maybe it'll help someone else with the same problem who finds this 
message on a search.

The answer turned out to be: set DCD high.  DCD is pin 1 on a 9-pin 
serial connector.   That was all there was to it.  The program I 
included below works fine.  But if DCD isn't high (in my case, it wasn't 
connected), then read() will hang indefinitely.

I had read here and there that open() would hang if DCD wasn't high, 
depending on the flags (that is, unless O_NONBLOCKING was used). 
However, I found that open() didn't hang.

To recap:  If you're trying to connect to a simple serial device, and 
all you connect is Tx, Rx and ground, then you may find (as I did) that 
read() will hang.  You also must connect DCD (pin 1) to +12 V (I put it 
through a 2.2k resistor, just in case...).

     - Happy Chuck

Chuck Messenger wrote:
> I'm unable to read from a serial device under Linux (Mandrake 9.1, 
> 2.4.21-0.13mdk kernel), but the device works find in Windows.  I'm 
> hoping someone can offer me suggestions.
> 
> My device is a Basic Stamp microcontroller, which I've programmed to 
> spit out a certain byte continuously -- 0x40 -- at 38400 baud,n,8,1. The 
> device loops RTS back to DSR (i.e. it connects pins 6 and 7 of the 9-pin 
> serial connector -- DSR and RTS).  Other than that, only Rx and Tx are 
> connected (and ground, of course).
> 
> I know the hardware works (both controller and PC), because I can boot 
> Win2k on the same machine and run a short program I wrote which 
> continuously dumps to the console whatever comes in.
> 
> Also, I know that things are _basically_ configured right, in my Linux 
> boot, since I can run "statserial /dev/tts/0", which shows, in real 
> time, the status of the DSR line.  When I unplug from my device, DSR 
> goes low; when I plug it back in, DSR goes high.
> 
> I tried writing a simple port-dumping program on Linux, based on Peter 
> Baumann's sample in his Serial Programming Howto:
> 
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <fcntl.h>
> #include <termios.h>
> #include <stdio.h>
> 
> #define BAUDRATE B38400
> #define MODEMDEVICE "/dev/tts/0"
> #define _POSIX_SOURCE 1 /* POSIX compliant source */
> 
> int
> main()
> {
>     int fd,c, res;
>     struct termios oldtio,newtio;
> 
>     fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
> 
>     if (fd < 0) {
>         perror(MODEMDEVICE);
>         exit(-1);
>     }
> 
>     tcgetattr(fd, &oldtio); /* save current port settings */
> 
>     memset(&newtio, 0, sizeof(newtio));
> 
>     newtio.c_cflag = BAUDRATE | /*CRTSCTS |*/ CS8 | CLOCAL | CREAD;
>     newtio.c_iflag = IGNPAR   // ignore parity errors
>                    | IGNBRK;  // ignore BREAK characters
>     newtio.c_oflag = 0;
> 
>     /* set input mode (non-canonical, no echo,...) */
> 
>     newtio.c_lflag = 0;
> 
>     newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
>     newtio.c_cc[VMIN]     = 1;   /* blocking read until 1 chars received */
> 
>     tcflush(fd, TCIFLUSH);       // Flush an data received by not read
>     tcsetattr(fd,TCSANOW,&newtio);
> 
>     while (1) {       /* loop for input */
>         char buf[1];
>         res = read(fd,buf,1);   /* returns after 1 chars have been input */
>         printf("%02x ", buf[0]);
>         fflush(stdout);
>     }
> 
>     tcsetattr(fd,TCSANOW,&oldtio);
> 
>     return 0;
> }
> 
> 
> However, the program fails to read any bytes -- it hangs at the read().
> 
> I've tried running serlook, but that, too, failed to see any bytes.
> 
> Any suggestions?
> 
> I've tried 9600 baud instead of 38400 -- still no luck (although it 
> works under Win2k).
> 
> Poking around, I've seen mention of the DCD control line -- could that 
> be the problem (i.e. does the Linux serial driver require it, somehow? I 
> don't have it hooked up.)
> 
> 
>     - Chuck Messenger
> 
> 
> -
> To unsubscribe from this list: send the line "unsubscribe linux-serial" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-11-20 20:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-11-20 19:22 Trouble reading from my serial device (a microcontroller) Chuck Messenger
2003-11-20 21:07 ` Chuck Messenger
  -- strict thread matches above, loose matches on Subject: below --
2003-11-20 20:42 Ed Vance

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.