View topic - ASCII output in QNX Momentics

ASCII output in QNX Momentics

anything that doesn't fit to other groups.

ASCII output in QNX Momentics

Postby doncmh » Tue May 22, 2012 3:40 pm

I have tried the following code to run an Audrino board via JAVA. It gives me the correct ASCII output.
However when I apply the code into QNX Momentics (which controls a target system PC104), the ASCII output are not correct. (e.g. original output are numbers it output as a letter or a little square.)

Which part of my code goes wrong?

Code: Select all
###################################################################
//Arduino Library Test Application - Run on PC104

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

char buffer[100];
int arduino;

int main(int argc, char *argv[]) {
   printf("ARDUINO TEST\n");

   arduino = open_arduino();
   ssize_t rssize;

   tcflush(arduino,TCIFLUSH);

   while(1){

      rssize = readcond(arduino,buffer,1,1,0,0); //reads one byte at a time - adjusted as needed
      printf("%s\n",buffer);

      sleep(0.05);
   }

   return EXIT_SUCCESS;
}


//##################################################################
//arduino_commands.h


#ifndef IMU_ARDUINO_H_
#define IMU_ARDUINO_H_

int open_arduino(void);
int arduino_port(int fdard);

#endif /* ARDUINO_COMMANDS_H_ */


//##################################################################
//arduino_commands.cc

/*Core Libraries*/
#include <stdio.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <arduino_commands.h>

#define BAUD_RATE B9600
/*OPEN SERIAL PORT AND SET UP*/
int open_arduino(void) {
   int fdard; /* File descriptor for the port */
   struct termios termios_p; /*termios structure*/
   fdard = open("/dev/ser1", O_RDONLY | O_NOCTTY);
   if (fdard == -1) {
      perror("open_port: Unable to open ARDUINO PORT /dev/ser1 - ");
      return 0;
   } else
      printf("ARDUINO port opened\n");
   tcgetattr(fdard, &termios_p); /*read in the current settings*/
   cfsetispeed(&termios_p, BAUD_RATE); /*set input baud*/
   cfsetospeed(&termios_p, BAUD_RATE); //set output baud
   termios_p.c_cflag &= ~PARENB; //no parity bit (error checking bit)
   termios_p.c_cflag &= ~CSIZE;
   termios_p.c_cflag |= CS8; //set number of bits to 8
   termios_p.c_cflag &= ~IHFLOW; //disable input h/w flow control
   termios_p.c_cflag &= ~OHFLOW; //disable output h/w flow control
   tcsetattr(fdard, TCSANOW, &termios_p);
   return (fdard);
}
/*CLOSE SERIAL PORT*/
int arduino_port(int fdard) {
   close(fdard);
   printf("port closed \n");
   return 0;
}


Thank you very much
doncmh
Active Member
 
Posts: 16
Joined: Thu May 10, 2012 7:12 am

Re: ASCII output in QNX Momentics

Postby Tim » Tue May 22, 2012 5:02 pm

For one thing your buffer[100] isn't initialized to all 0's. So trying to read 1 byte into this buffer and then printing it as a string (and expecting to have a string terminator - 0 character) there is suspect logic.

That's probably not your display problem though. My guess is that what you are getting from the readcond (serial port) is not what you expect. Can I assume you are sending a 1 character from the other side and expecting to see a '1' character printed on the screen? The problem is that 1 ASCII isn't the same as a '1' character in a text string. 1 ASCI will be a funny character like what you see printed to the screen.

I suggest you print out the numerical value of the single character you get back and compare that with what you send and then compare with an ASCII table.

Tim
Tim
Senior Member
 
Posts: 1466
Joined: Wed Mar 10, 2004 12:28 am

Re: ASCII output in QNX Momentics

Postby doncmh » Wed Jun 13, 2012 1:30 am

I finally figured out the problem.
it is because the voltage between arduino and PC/104 is different.
So the 1/0 signals are inverted.
Thanks Tim
doncmh
Active Member
 
Posts: 16
Joined: Thu May 10, 2012 7:12 am


Return to General Programming

Who is online

Users browsing this forum: No registered users and 4 guests