blaurier
Posts: 3
Joined: Sun Aug 02, 2020 4:26 am

Serial Input requires LF to show

Sun Aug 02, 2020 4:31 am

I'm trying to read data from a serial port which only contains binary data. I copied/wrote some code but it only outputs data if there is a LF(0x0a) at the end of the message. I tried everything but don't have a clue how to sort this. Hope someone here can get me in the right direction.


Code: Select all

   	#include <stdio.h>
    	#include <fcntl.h>   /* File Control Definitions           */
    	#include <termios.h> /* POSIX Terminal Control Definitions */
    	#include <unistd.h>  /* UNIX Standard Definitions 	   */ 
    	#include <errno.h>   /* ERROR Number Definitions           */
        #include <sys/ioctl.h>	
	int main()
    	{
        	int fd;/*File Descriptor*/
		
		printf("\n +----------------------------------+");
		printf("\n |        Serial Port Read          |");
		printf("\n +----------------------------------+");

		/*------------------------------- Opening the Serial Port -------------------------------*/

		/* Change /dev/ttyAMA0 to the one corresponding to your system */

        	fd = open("/dev/ttyAMA0",O_RDWR | O_NOCTTY | O_NDELAY | O_SYNC | O_NONBLOCK);	/* ttyUSB0 is the FT232 based USB2SERIAL Converter   */
			   					/* O_RDWR   - Read/Write access to serial port       */
								/* O_NOCTTY - No terminal will control the process   */
								/* Open in blocking mode,read will wait              */
									
									                                        
									
        	if(fd == -1)						/* Error Checking */
            	   printf("\n  Error! in Opening ttyAMA0  ");
        	else
            	   printf("\n  ttyAMA0 Opened Successfully ");

	
		/*---------- Setting the Attributes of the serial port using termios structure --------- */
		
		struct termios SerialPortSettings;	/* Create the structure                          */

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

		/* Setting the Baud rate */
		cfsetispeed(&SerialPortSettings,B115200); /* Set Read  Speed as 9600                       */
		cfsetospeed(&SerialPortSettings,B115200); /* Set Write Speed as 9600                       */

		/* 8N1 Mode */
		SerialPortSettings.c_cflag &= ~PARENB;   /* Disables the Parity Enable bit(PARENB),So No Parity   */
		SerialPortSettings.c_cflag &= ~CSTOPB;   /* CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit */
		SerialPortSettings.c_cflag &= ~CSIZE;	 /* Clears the mask for setting the data size             */
		SerialPortSettings.c_cflag |=  CS8;      /* Set the data bits = 8                                 */
		
		SerialPortSettings.c_cflag &= ~CRTSCTS;       /* No Hardware flow Control                         */
		SerialPortSettings.c_cflag |= CREAD | CLOCAL; /* Enable receiver,Ignore Modem Control lines       */ 
		
		
		SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY);          /* Disable XON/XOFF flow control both i/p and o/p */
		SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG | IEXTEN);  /* Non Cannonical mode*/
		SerialPortSettings.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); // Disable any special handling of received bytes                            */

		SerialPortSettings.c_oflag &= ~OPOST;/*No Output Processing*/
		
		/* Setting Time outs */
		SerialPortSettings.c_cc[VMIN] =  1; /* Read at least 10 characters */
		SerialPortSettings.c_cc[VTIME] = 0; /* Wait indefinetly   */


		if((tcsetattr(fd,TCSANOW,&SerialPortSettings)) != 0) /* Set the attributes to the termios structure*/
		    printf("\n  ERROR ! in Setting attributes");
		else
                    printf("\n  BaudRate = 115200 \n  StopBits = 1 \n  Parity   = none\n");
			
	        /*------------------------------- Read data from serial port -----------------------------*/

		tcflush(fd, TCIFLUSH);   /* Discards old data in the rx buffer            */

		unsigned char read_buffer[256];   /* Buffer to store the data received              */
		unsigned char write_buffer[]= "\x84\x00\x00\x11";
		int  bytes_read = 0;    /* Number of bytes read by the read() system call */
 		int i = 0;
		int bytes = 0;

		while (1) 
		{
			bytes_read = read(fd,&read_buffer,256); /* Read the data                   */
			if (bytes_read > 0) 
			{
				printf("Read %i bytes. Received message: %s\n", bytes_read , read_buffer);
				for (i=0; i<bytes_read; i++)
				{
					printf("%02x",read_buffer[i]);
				}	
				printf ("\n");
			}
		
//		int  bytes_written  =  0 ;   
                                                          
//		bytes_written = write(fd,write_buffer,sizeof(write_buffer)-1);
//		printf("Write %i bytes written\n", bytes_written);
//		sleep(1);

		}
		printf("\n +----------------------------------+\n\n\n");
		
		close(fd); /* Close the serial port */

    	}

blaurier
Posts: 3
Joined: Sun Aug 02, 2020 4:26 am

Re: Serial Input requires LF to show

Mon Aug 03, 2020 12:32 am

After struggling for a few more hours i solved the problem.

SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG | IEXTEN);

should be

SerialPortSettings.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | IEXTEN);

:cry: :cry: :cry: :cry:

User avatar
PeterO
Posts: 5951
Joined: Sun Jul 22, 2012 4:14 pm

Re: Serial Input requires LF to show

Mon Aug 03, 2020 5:20 am

Those two lines are the same ! Cut & Paste error ?
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

blaurier
Posts: 3
Joined: Sun Aug 02, 2020 4:26 am

Re: Serial Input requires LF to show

Mon Aug 03, 2020 5:39 am

no they are not : in one there is an i :

SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG | IEXTEN);

and in the other an l

SerialPortSettings.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | IEXTEN);

User avatar
PeterO
Posts: 5951
Joined: Sun Jul 22, 2012 4:14 pm

Re: Serial Input requires LF to show

Mon Aug 03, 2020 6:01 am

Ah, I was concentrating on the value not the variable. Plus it's only one pixel different !
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

cleverca22
Posts: 843
Joined: Sat Aug 18, 2012 2:33 pm

Re: Serial Input requires LF to show

Mon Aug 03, 2020 7:28 am

lililililililili

Code: Select all

lililililililili
code blocks use a different font, which make the difference much more obvious

User avatar
PeterO
Posts: 5951
Joined: Sun Jul 22, 2012 4:14 pm

Re: Serial Input requires LF to show

Mon Aug 03, 2020 8:42 am

cleverca22 wrote:
Mon Aug 03, 2020 7:28 am
lililililililili

Code: Select all

lililililililili
code blocks use a different font, which make the difference much more obvious
Good Point !

Code: Select all

SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG | IEXTEN);
should be

Code: Select all

SerialPortSettings.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | IEXTEN);
Even I can see it now :lol:
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Return to “C/C++”