I am trying to receive data through the serial port from my SBC (a TS-7200), i am working with linux and handle serial communication using C/C++.To send data to the SBC i'am using an arduino uno.
So basically the arduino is printing ASCII text data through Pin1 (Hardware Serial) in a loop with a 3ms delay.
From the SBC i read raw data in no canonical mode, ( in the code below are the parameters and configurations for the serial port) i'am trying to read one characters at a time, following the example from this page : https:/ from this page/chrisheydrick.com/2012/06/17/how-to-read-serial-data-from-an-arduino-in-linux-with-c-part-3/.
but when i run the code it print the number 2 as 3, and the number 3 as f, I am not able to read ASCII characters.
I have tried several things but I have not been able to find the problem, Hope someone can help me.
Here is the Arduino code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2,3); // RX, TX
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() {
Serial.print(2);
delay(3000);
Serial.print(3);
delay(3000);
}
The c code from the linux side:
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
char *portname = "/dev/ttyAM1";
char buf[256];
int main(int argc, char *argv[])
{
int fd,stop = 0;
/* Open the file descriptor in non-blocking mode */
fd = open(portname, O_RDWR | O_NOCTTY);
/* Set up the control structure */
struct termios toptions;
/* Get currently set options for the tty */
tcgetattr(fd, &toptions);
/* Set custom options */
/* 9600 baud */
cfsetispeed(&toptions, B9600);
cfsetospeed(&toptions, B9600);
/* 8 bits, no parity, no stop bits */
toptions.c_cflag &= ~PARENB;
toptions.c_cflag &= ~CSTOPB;
toptions.c_cflag &= ~CSIZE;
toptions.c_cflag |= CS8;
/* no hardware flow control */
toptions.c_cflag &= ~CRTSCTS;
/* enable receiver, ignore status lines */
toptions.c_cflag |= CREAD | CLOCAL;
/* disable input/output flow control, disable restart chars */
toptions.c_iflag &= ~(IXON | IXOFF | IXANY);
/* disable canonical input, disable echo,
disable visually erase chars,
disable terminal-generated signals */
toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
/* disable output processing */
toptions.c_oflag &= ~OPOST;
/* wait for 1 character to come in before read returns */
/* WARNING! THIS CAUSES THE read() TO BLOCK UNTIL ALL */
/* CHARACTERS HAVE COME IN! */
toptions.c_cc[VMIN] = 1;
/* no minimum time to wait before read returns */
toptions.c_cc[VTIME] = 0;
/* commit the options */
tcsetattr(fd, TCSANOW, &toptions);
/* Wait for the Arduino to reset */
usleep(1000*1000);
/* Flush anything already in the serial buffer */
tcflush(fd, TCIFLUSH);
while(stop == 0){
/* read up to 128 bytes from the fd */
int n = read(fd, &buf, 128);
usleep(3000*1000);
/* print how many bytes read */
printf("%i bytes got read...\n", n);
/* print what's in the buffer */
printf("Buffer contains...\n%s\n", buf);
}
return 0;
}
This is what i see:
2 bytes got read...
Buffer contains...
f
2 bytes got read...
Buffer contains...
3
2 bytes got read...
Buffer contains...
f
2 bytes got read...
Buffer contains...
3
0
and -12V represents1
; inverted from normal TTL.