RS232

To write and test RS232

Step1: Create a uart_txrx.c File

Step 2: Write the following code into the File

#include "serial.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
int main (int argc, char* argv[])
{
	int i, rv, len, pid;
	char buffer[100], buffer2[10];
	struct termios my_termios, new_termios;
	char dev[20]="/dev/ttyS3";
	int fd = open(dev, O_RDWR | O_NDELAY | O_NOCTTY);
	tcgetattr(fd, &my_termios);
	my_termios.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR);
	my_termios.c_iflag &= ~(PARMRK | INPCK | ISTRIP | IXON);
	my_termios.c_oflag &= ~(OCRNL | ONLCR | ONLRET | ONOCR);
	my_termios.c_oflag &= ~( OFILL | OLCUC | OPOST);
	my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
	my_termios.c_lflag &= ~(ISIG | ECHOK | ECHOCTL | ECHOPRT);
	my_termios.c_cflag &= ~(CSIZE | PARENB);
	my_termios.c_cflag &= ~CRTSCTS;
	my_termios.c_cflag |= CS8 | CLOCAL;

	my_termios.c_cflag &= ~CBAUD;
	my_termios.c_cflag |= B115200; /* change your baudrate here */
	my_termios.c_cc[VMIN] = 0;
	my_termios.c_cc[VTIME] = 5;
	rv = tcsetattr(fd, TCSANOW, &my_termios);
	tcgetattr(fd, &new_termios);
	while(1)
	{

		printf("enter the string to send \n");
		scanf("%s",buffer);
		len = strlen(buffer)+1;
		write(fd, buffer, len);
		if (read(fd, buffer2, 1) > 0)
			for(i=0;i<len;i++){
				printf("%c\n",buffer2[i]);}
	}
	close(fd);
}

Step 3: Open Terminal and navigate to Toolchain folder.

Step 4: Run the "env.sh" file using . env.sh to enable the toolchain To check if the toolchain is enabled type in "arm" and hit the <TAB> key twice

Step 5: Create a binary name "uartTxRx" from the "uart_txrx" file using arm-linux-gnueabi-gcc uart_txrx.c -o uartTxRx

Step 6: Transfer the compiled binary to the Board using TFTP or SD card

Step 7: Run the binary sudo ./uartTxRxon the Board's terminal.

Last updated