Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

How to read RS232 ?

Altera_Forum
Honored Contributor II
1,877 Views

Hello all, 

 

I have problem with UART, when i want to read serial data (hex byte message - something like bytes: 02,05,01,02,05 after this 10ms space and 03,05,xx,yy,zz etc.).  

 

I have development kit DK-DSP-EP2S60 and when i connect the board, which send me bytes to the NIOS2 design with uart and in uClinux i use: 

 

fd = open(S0_232, O_RDWR | O_NDELAY | O_NONBLOCK );  

fd = open(S0_232, O_RDONLY | O_NOCTTY | O_NDELAY ); 

 

and when the program perform this command, the data from RxD RS232 are sended to the output TxD. I dont understand why ? (why it create loopback on RS232) 

 

How is possible to read data from RS232 without loopback ? (how to set RS232 for reading only ?) 

 

Thank you very much. 

 

Jan Naceradsky, Czech Republic
0 Kudos
7 Replies
Altera_Forum
Honored Contributor II
317 Views

I forgot: 

SO_232 is device "/dev/ttyS0" 

# define S0_232 "/dev/ttyS0" 

 

Jan Naceradsky
0 Kudos
Altera_Forum
Honored Contributor II
317 Views

Because the linux driver will be assuming you want tty semantics. 

You need to change the tty mode - see 'man termios' (at a guess).
0 Kudos
Altera_Forum
Honored Contributor II
317 Views

Thank you. 

 

I finally solved it with my collegue which advice me to add command 

 

stty -F /dev/ttyS3 sane -echo -icanon 

 

to rc file. This command stop the echo of input characters.  

And after open command in program i use: 

 

void init_rs232(void)  

struct termios options;  

 

fd = open(S0_232, O_RDWR | O_NOCTTY | O_NDELAY ); 

if (fd > 0)  

{ fcntl(fd, F_SETFL, O_NONBLOCK); 

tcgetattr(fd, &options); 

options.c_cflag |= (CLOCAL | CREAD | CS8 ); 

options.c_cc[VMIN] = 1; 

options.c_cc[VTIME] = 0; 

options.c_lflag = FLUSHO; 

tcsetattr(fd, TCSANOW, &options);  

printf("Init RS232 OK.\n");  

}  

 

and now it works well. 

 

 

Jan Naceradsky
0 Kudos
Altera_Forum
Honored Contributor II
317 Views

The 'stty' comand probably does nothing useful - I'm not even sure the values persist once the device is closed. 

You are already disabling echo and icanon when you set c_lflag.
0 Kudos
Altera_Forum
Honored Contributor II
317 Views

Yes, you are right, i cancel stty command in linux and use  

--------------------------------------------------- 

options.c_lflag &= ~(ICANON | ECHO | ISIG ); 

 

in open function: 

 

fd = open(S0_232, O_RDWR | O_NOCTTY | O_NDELAY ); 

// fd = open(S0_232, O_RDWR); // | O_NOCTTY | O_NDELAY ); 

if (fd > 0)  

 

fcntl(fd, F_SETFL, FNDELAY);  

tcgetattr(fd, &options); 

options.c_cflag |= (CLOCAL | CREAD ); 

options.c_cflag &= ~PARENB; // set no parity, stop bits, data bits 

options.c_cflag &= ~CSTOPB; 

options.c_cflag &= ~CSIZE; 

options.c_cflag |= CS8;  

options.c_cc[VMIN] = 1; 

options.c_cc[VTIME] = 0; 

options.c_lflag &= ~(ICANON | ECHO | ISIG ); 

tcsetattr(fd, TCSANOW, &options);  

 

 

But when i read hex message (from PC to Altera NIOS2 system) 

16,11,01,00,00,00,00,80,00,7B 

 

the linux read  

16,01,00,00,00,00,80,00,7B 

and the second byte doesn't receive .... i read the termios on: 

http://slackware.osuosl.org/slackware-3.3/docs/mini/serial-port-programming 

 

and they say that 0x11 is DC1 which mean VSTART for XON and XOFF and i am not able to disable this software flow control. 

 

When i use setting 

options.c_cflag &= ~(IXON | IXOFF | IXANY); // no flow control 

 

or 

 

options.c_cflag &= ~CRTSCTS;  

 

it doesnt work. Read again:16,01,00,00,00,00,80,00,7B (and the 0x11 is missing). 

 

Do you have somebody program, which can read EVERY characters (hex - 00-FF) without any control meanings ? 

 

Is possible to disable control characters in options.c_cc[xxx] ? 

 

I retype  

options.c_cc[VSTART] = 0xFE; 

 

and the message is read ok, but echo was set ... why ? 

 

Where is error ? 

 

 

Jan, Czech Republic
0 Kudos
Altera_Forum
Honored Contributor II
317 Views

Hi all, 

is anyone who can solve this problem ?  

 

Jan
0 Kudos
Altera_Forum
Honored Contributor II
317 Views

Are you actually certain the PC is sending the 0x11 byte ?

0 Kudos
Reply