Hi,
I am working on creating [eventually] a nice graphical GUI to interface with my arduino board through the serial port created by the board. The program is for my linux platform, and the goal is to be able to read messages printed by the board, and be able to run the C++ program and input data that will be sent to and stored on the arduino board.
In the beginning of my setup() function on the board I have:
Serial.println("Press leftSwitch to begin!");
Serial.println("READY");
And when I run my GUI, it receives the information (I believe), however it is sent back to the GUI from the board in weird unreadable characters and I'm not entirely sure why. This is the program I am working with for my GUI right now.
Thanks in advance for any input.
#include <termios.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
main(){
int pid, serial_fd; //process ID info for forking and file descriptor for serial stream
struct termios port_config; //sets up termios configuration structure for the serial port
char c; //charater for printing from serial port
char input[10];
const char *device = "/dev/ttyACM0"; //sets where the serial port is plugged in
serial_fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY); //opens serial port in device slot
if(serial_fd == -1)
{ //-1 is the error message for failed open port
fprintf(stdout, "failed to open port\n");
}
tcgetattr(serial_fd, &port_config);
cfsetispeed(&port_config, B9600); //set baud input to 9600 (might be wrong?)
cfsetospeed(&port_config, B9600); //set baud output to 9600 (might be wrong?)
port_config.c_iflag = 0; //input flags
port_config.c_iflag &= (IXON | IXOFF |INLCR); //input flags (XON/XOFF software flow control, no NL to CR translation)
port_config.c_oflag = 0; //output flags
port_config.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG); //local flags (no line processing, echo off, echo newline off, canonical mode off, extended input processing off, signal chars off)
port_config.c_cflag = 0; //control flags
port_config.c_cflag &= (CLOCAL | CREAD | CS8); //control flags (local connection, enable receivingt characters, force 8 bit input)
port_config.c_cc[VMIN] = 1;
port_config.c_cc[VTIME] = 0;
tcsetattr(serial_fd, TCSAFLUSH, &port_config); //Sets the termios struct of the file handle fd from the options defined in options. TCSAFLUSH performs the change as soon as possible.
pid = fork(); //splits process for recieving and sending
if(pid == 0)
{ //should continually receive
fprintf(stdout, "Receiving Process Started\n");
while(1){
if (read(serial_fd, &c, 1)>0)
{ //if there's something to be read from the serial port
fprintf(stdout, "%c", c);
}
}
}
else{
fprintf(stdout, "Sending Process Started\n");
while(1){
scanf("%s", input);
input[strlen(input)] = '\r';
send(serial_fd, input, strlen(input), 0);
fprintf(stdout, "%s\n", input);
}
}
close(serial_fd); //close serial stream
fprintf(stdout, "complete");
return 0;
}