0

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    
6
  • has the TS-7200 a TTL Serial interface?
    – Juraj
    Commented Jan 25, 2019 at 19:00
  • Hello Juraj, no, it doesn't have a TTL interface, only have 3 serial interface : ttyAM0 (COM1), ttyAM1 (COM2), ttyAM2 (COM3) .
    – Paulina
    Commented Jan 25, 2019 at 19:13
  • then how do you wire it to TTL Serial pin on Arduino?
    – Juraj
    Commented Jan 25, 2019 at 19:15
  • I have a db9 connector, connected to the serial port COM2 of the SBC, using the r232 pinout. The transmition pin1 of the arduino (TXD) is connected to pin2 (RXD) of the db9 connector and the ground pin5 connected to the ground of the arduino .
    – Paulina
    Commented Jan 25, 2019 at 19:28
  • 2
    RS232 uses +/- 12V (or sometimes +/- 5V), and is not compatible with TTL Serial, which uses 0V/5V. Also +12V in RS232 represents 0 and -12V represents 1; inverted from normal TTL. Commented Jan 25, 2019 at 19:47

1 Answer 1

1

RS232 uses +/- 12V (or sometimes +/- 5V), and is not compatible with TTL Serial, which uses 0V/5V. Also +12V in RS232 represents 0 and -12V represents 1; inverted from normal TTL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.