0

I'm working with a Serial communication interface; a SBC (TS-7200) running Linux Kernel 2.6.36 sends data via RS232 to a PC (running Windows). The data is encapsulated in packets (byte per byte) and sent. The issue occurs when the TS tries to send an integer (uint8_t)(10), which, notably, is the ASCII for \n, the termination char (0xA); not random right?!

The strange things is that, if I use an Arduino Uno to do the same exact operations, an integer 10 will be sent and received without a problem! So my conclusion would be that Linux takes 0b00001010 like a \n, flushes the serial buffer and doesn't send 10 over, while Arduino doesn't. Searching online I found that a file can be opened in binary mode (O_BINARY) or file mode but apparently this ins't available in Linux; is that right? Is there a way to make Linux behave like Arduino?

Thank you, Federico

PS: I forgot to mention I'm working with C

Federico Celi
  • 33
  • 1
  • 6
  • How do you send an integer with a value, say, `1000`? – Weather Vane Sep 19 '16 at 15:35
  • 1
    You don't need `O_BINARY` on Linux because, like virtually every sane operating system, it doesn't do any conversion of line ending characters. It's only Windows with its bizarre CR+LF line endings where you need `O_BINARY`. – Paul R Sep 19 '16 at 15:38
  • 1
    Please see [this previous question](http://stackoverflow.com/questions/2266992/no-o-binary-and-o-text-flags-in-linux) – Weather Vane Sep 19 '16 at 15:39
  • I open the serial communication with: `COM2 = open("/dev/ttyAM1", O_RDWR | O_NONBLOCK);` and to write I use `bytes_writed=write(COM2, number, 1);` – Federico Celi Sep 19 '16 at 15:40
  • @WeatherVane I already saw that post, but I'd say the Windows counterpart works properly (LabVIEW does it by default, I guess(?)), otherwise how would you explain that it's working with Arduino and not with the TS? – Federico Celi Sep 19 '16 at 15:47

1 Answers1

0

You have to set the serial port in "raw" mode:

struct termios  tty;
unsigned int    sfd;

/* Open the serial port */
sfd = open("/dev/ttyS1", O_RDWR | O_NOCTTY);

/* Get current port config. */
tcgetattr(sfd, &tty);

/* Set youd speed/parity, etc. */
/* ... */

/*
Set raw mode, equivalent to:
tty->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
            | INLCR | IGNCR | ICRNL | IXON);
tty->c_oflag &= ~OPOST;
tty->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tty->c_cflag &= ~(CSIZE | PARENB);
tty->c_cflag |= CS8;
*/
cfmakeraw(&tty);

tcsetattr(sfd, TCSANOW, &tty);

/* Do read/write */

Be careful to de-activate even software flow control (XON/XOFF) in order to send all the data in the packet.

Please check the article How to read a binary data over serial terminal in C program?

Community
  • 1
  • 1
Carlo Banfi
  • 186
  • 1
  • 7