Skip navigation.
Home
The QNX Community Portal

View topic - Problem reading ASCII characters via serial port

Problem reading ASCII characters via serial port

Discussion about the QNX6 OS.

Problem reading ASCII characters via serial port

Postby jucaross » Tue Sep 27, 2011 12:56 pm

Hi everybody.

I'm having problems reading the first 32 characters of the ASCII table through the serial port and with the log file generation of them.

I've tried to use the commands read(), read_cond() and modem_read().

I've seen that it gets only the 0, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 and 24 character.

I also did a test with HyperTerminal to Qtalk. It receives only the characters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 and 31.

This is my code:

Class to configure the port:

Code: Select all
int ASerialPortConfig::Configuration(int fd, int speed) {

   int ret = 0;
   struct termios tp;
   
   /* Getting the current terminal control settings. */
   ret = tcgetattr(fd, &tp);
   if (ret == -1){
      perror("Error getting the termios attribute");
      return -1;
   }
   
   /* Setting the input baud rate in a termios structure. */
   ret = cfsetispeed(&tp, speed);
   if (ret == -1){
      perror("Error editing termios input speed");
      return -1;
   }
   
   /* Setting the output baud rate in a termios structure. */
   ret = cfsetospeed(&tp, speed);
   if (ret == -1){
      perror("Error editing termios output speed");
      return -1;
   }
   
   /* Disable ECHO. */
   tp.c_lflag &= ~ECHO;
   
   /* QNX extensions to POSIX are enabled. */
   tp.c_lflag |= IEXTEN;

   /* Enable signals. */
   tp.c_lflag |= ISIG;

   /* Disable hardware input/output flow control. */
   tp.c_cflag &= ~IHFLOW;
   tp.c_cflag &= ~OHFLOW;

   /* Disable software input/output flow control. */
   tp.c_iflag &= ~IXOFF;
   tp.c_iflag &= ~IXON;
   
   /* Changing the terminal control settings for a device. */
   ret = tcsetattr(fd, TCSANOW, &tp);
   if (ret == -1){
      perror("Error setting termios options");
      return -1;
   }
   else
      return ret;
}


Openning the port:

Code: Select all
open("/dev/ser1", NULL)


Capturing the serial port content:

Code: Select all
   unsigned char itsBuffer;
   ofstream tempFile;

   tempFile.open("/home/juliano/QNXSerialReceiver.txt");

   while (read(this->getFileDescriptor(), &itsBuffer, sizeof(itsBuffer)) > 0) {
      tempFile << itsBuffer;
      tempFile.flush();
   }



Regards.
jucaross
Active Member
 
Posts: 39
Joined: Thu Nov 04, 2010 6:37 pm

Re: Problem reading ASCII characters via serial port

Postby denkelly » Tue Sep 27, 2011 1:58 pm

Maybe I missed it, but you don't seem to be canceling "line discipline" processing. Try this setup...

Code: Select all
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
int fd;
int speed;
struct termios raw;
char buf[256];
char big[1024];
int bigcount = 0;
int count;

   // Open the port
   if ((fd = open ("/dev/ser1", O_RDWR)) == -1)
   {
          fprintf(stderr, "Error with open() on /dev/ser1. Make sure exists.\n");
          perror (NULL);
          exit(EXIT_FAILURE);
   }
   
   // Get the attributes
   if (tcgetattr( fd, &raw))
   {
          close( fd );
          return -1;
        }
   
        // Set input baud rate
   speed = 57600;
        cfsetispeed(&raw, speed);
   cfsetospeed(&raw, speed);
   
   raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON );
   
   raw.c_oflag &= ~(OPOST);
   
        raw.c_cflag &= ~CSIZE;
        raw.c_cflag |= CS8;
   raw.c_cflag &= ~CSTOPB;
   raw.c_cflag &= ~PARENB;
   
   raw.c_lflag &= ~(ECHO | ICANON | ISIG | ECHOE | ECHOK | ECHONL | IEXTEN);
   
   raw.c_cc[VMIN] =  1;
   raw.c_cc[VTIME] = 0;
   
   if ( tcsetattr( fd, TCSADRAIN, &raw ) == -1 )   // CHECK FOR -1 failure here - this is the actual "set" and it must succeed or nothing may have changed!
   {
          fprintf(stderr, "Error with tcsetattr() on /dev/ser1.\n");
          close( fd );
          perror (NULL);
          exit(EXIT_FAILURE);
   }
   
    sprintf( buf, "waiting for data..." );
    write( fd, buf, strlen(buf) );
    buf[0] = 0;
    while ( buf[0] != 0x03 )  // end loop on ^C
    {
       count = read( fd, buf, sizeof(buf)-1 );
       while ( count-- > 0 )
       {
          big[bigcount++] = buf[count];
       }
    }
    big[bigcount] = 0;
    printf( "%s\n", big );
   
    close( fd );
    return EXIT_SUCCESS;
}

denkelly
Senior Member
 
Posts: 160
Joined: Sat Aug 02, 2008 3:27 pm


Return to QNX6 - OS

Who is online

Users browsing this forum: Google [Bot] and 2 guests