How to read a binary data over UART?

I am testing UART with the code in your website. I communicate with iMX6ULL through UART and send binary data over serial terminal. If the data is not an ordinary letter, it is not receiving it. For example, if there is 0x00 among the data, it can be read only before that. I have to receive all binary data and also send binary data.

The following is my code. Please let me know how to send and receive all binary data.

#include <stdio.h>   /* I/O Definitions                    */
#include <unistd.h>  /* Standard Symbolic Constants        */
#include <fcntl.h>   /* File Control Definitions           */
#include <termios.h> /* POSIX Terminal Control Definitions */
#include <string.h>  /* String Manipulation Definitions    */
#include <errno.h>   /* Error Code Definitions             */
#include <stdlib.h>

typedef unsigned long  u32;
typedef unsigned short u16;
typedef unsigned char  u8;

#define BUFFER_SIZE 100

int main(int argc, char *argv[])
{
	u8 buf_rx[BUFFER_SIZE];
	u8 buf_tx[BUFFER_SIZE];
	const char *device = "/dev/ttymxc1";
	struct termios tty;
	int fd, len;
	int i=0;
	int flags = O_RDWR | O_NOCTTY | O_NDELAY; 

	/*------------------------------- Opening the Serial Port -------------------------------*/
	fd = open(device, flags);

	if(fd == -1)
	{
		printf("\n Failed to open port! ");
		return -1;
	}

	tcgetattr(fd, &tty);    /* Get the current attributes of the Serial port */

	cfsetispeed(&tty, B9600); /* Set read speed as 9600 baud                       */
	cfsetospeed(&tty, B9600); /* Set write speed as 9600 baud                      */

	tty.c_cflag &= ~PARENB;   /* Disables the Parity Enable bit(PARENB)  */
	tty.c_cflag &= ~CSTOPB;   /* Clear CSTOPB, configuring 1 stop bit    */
	tty.c_cflag &= ~CSIZE;    /* Using mask to clear data size setting   */
	tty.c_cflag |=  CS8;      /* Set 8 data bits                         */
	tty.c_cflag &= ~CRTSCTS;  /* Disable Hardware Flow Control           */

	if((tcsetattr(fd, TCSANOW, &tty)) != 0)
	{
		/* Write the configuration to the termios structure*/
		printf("Error! Can't set attributes.\n");
		return -1;
	}
	else
	{
		printf("All set! \n");
	}

	tcflush(fd, TCIFLUSH);
	memset(buf_rx, 0, BUFFER_SIZE);

	while(1)
	{
		if(read(fd, buf_rx, sizeof(buf_rx)) != -1)
		{
			len = strlen(buf_rx);
#if 1
			for(i=0; i<len; i++)
			{
				printf("%02X ", buf_rx[i]);
			}
			printf("\n");
#endif

			if(buf_rx[0]==01)// && buf_rx[1]==0x01)
			{
				int send_len;

				memset(buf_tx, 0, sizeof(buf_tx));
				send_len = 4;

				buf_tx[0]=0x01;
				buf_tx[1] = 0x00;
				buf_tx[2] = 0x03;
				buf_tx[3] = 0x0D;

				tcflush(fd, TCOFLUSH);
				int result = write(fd, buf_tx, send_len);
				if(result == -1)
				{
					printf("Error>>: %s\n",strerror(errno));
				}
			}

			tcflush(fd, TCIOFLUSH);
			memset(buf_rx, 0, BUFFER_SIZE);
		}

		usleep(1000);
	}

	close(fd);
}

alt text

If I send the following binary data, then read only ‘01’.

01 00 76 98 99 77 FF FF E6 6F 0D ==> Read only 01

01 01 76 98 99 77 FF FF E6 6F 0D ==> Read correctly.

02 00 76 98 99 77 FF FF E6 6F 0D ==> Read only 02

02 01 02 C8 1C 62 5B 01 95 CF 0D ==> Read correctly.

41 54 2B 43 43 4C 4B 3F 0D 0A ==> Read correctly.

Hi @HansKim72

Thanks for writing to the Community!

You have to set the serial port to raw mode as explained in the Documentation.

Best regards,
Jaski

I already tried to test raw mode through Google a few hours ago, but I couldn’t find a solution. Please let me know if you know more details. Maybe someone else asked you like this question, bur I couldn’t find it in Toradex community.

Could you share the code, how you set up the raw mode? Thanks.

I resolved it through the following URL.

http://www.cs.uleth.ca/~holzmann/C/system/ttyraw.c

Perfect that the issue is solved. Thanks for your feedback.