ok, caveat - this looks like c++ and I don't do c++, so I might out on a
limb here.
On Mon, 11 Oct 2010, shusshinchi wrote:
> As mentioned in the subject. I am not doing anything complicated at all.
> I merely connected a peripheral device to com2 (/dev/ttyAM1). My program
> then sends out a series of bytes out on com2 to the peripheral device
> and the device responds. However, my program gets nothing. "cat
> /dev/ttyAM1" shows nothing either. Below is a sample code that I have
> used to produce this problem. Is there anything that I need to set
> anywhere to enable the OS to receive data on the serial comm link?
>
> int main ( int argc, char** argv )
> {
> for ( short c = 0 ; c < argc ; ++c )
> cout << "[" << c << "] " << argv[c] << endl ;
>
> int fd = open ( argv[1], O_RDWR | O_NOCTTY | O_NDELAY ) ;
first off, ALWAYS do error checking - do you get a valid file descriptor?
You don't check.
> termios port_settings ;
Dunno what this does, my C code usually has
struct termios port_settings ;
>
> cout << "fd = " << fd << endl ;
>
> memset ( &port_settings, 0, sizeof port_settings ) ;
I don't usually zero everything. I read the current settings, then twiddle
what I need.
> cfsetispeed ( &port_settings, B9600 ) ;
> cfsetospeed ( &port_settings, B9600 ) ;
>
> port_settings.c_cc[VMIN] = 0 ;
> port_settings.c_cc[VTIME] = 10 ;
>
> port_settings.c_iflag &= ~ICRNL ;
>
> port_settings.c_lflag &= ~ICANON ;
I use the function cfmakeraw() to set raw mode
> port_settings.c_cflag &= ~PARENB ;
> port_settings.c_cflag &= ~CSTOPB ;
> port_settings.c_cflag &= ~CSIZE ;
> port_settings.c_cflag &= ~CRTSCTS ;
> port_settings.c_cflag |= CS8 ;
> port_settings.c_cflag |= CREAD ;
> port_settings.c_cflag |= CLOCAL ;
>
> tcsetattr ( fd, TCSANOW, &port_settings ) ;
> tcflush( f d, TCIOFLUSH ) ;
>
> char output[ ] = { 0x06, 0x01, 0x00, 0x00, 0x00, 0x10, 0x3c, 0x71 }
> ;
> short charCount = 0 ;
> char c ;
> char cA[8] ;
> int r ;
>
> memset ( cA, 0, 8 ) ;
>
> cA[7] = '\0' ;
>
> tcflush ( fd, TCIOFLUSH ) ;
> write ( fd, output, 8 ) ;
>
> usleep ( 500000 ) ;
>
> cout << "Bytes read = " << ( r = read ( fd, cA, 7 ) ) << endl ; //
> this line fails to read anything
>
> tcflush ( fd, TCIOFLUSH ) ;
>
> if ( r == -1 )
> {
> switch ( errno )
> {
> case EAGAIN : cout << "eagain" << endl ; sleep ( 1 ) ;
> break ;
> case EBADF : cout << "ebadf" << endl ; break ;
> case EFAULT : cout << "efault" << endl ; break ;
> case EINTR : cout << "eintr" << endl ; break ;
> case EINVAL : cout << "einval" << endl ; break ;
> case EIO : cout << "eio" << endl ; break ;
> case EISDIR : cout << "eisdir" << endl ; break ;
> default : cout << "errno = " << errno ;
> }
> }
> }
Do you get eagain?
If so, why don't you try reading again?
Once the port in is nonblocking mode, a read will return immediately with
an error (errno=EAGAIN). Usually I do a sleep for a time and re-read, etc
until either I've got the required data, or a timeout period has pased and
I give up.
Oh and you get no marks for program structure :-) But maybe that's just me.
Jim
------------------------------------
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/ts-7000/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/ts-7000/join
(Yahoo! ID required)
<*> To change settings via email:
<*> To unsubscribe from this group, send an email to:
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|