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]###################################################################
//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;
}[/code]
Thank you very much