|
read/write example (None-Blocking Read)
void test8_uart(void){
int uart, result;
char szHello[] = "\r\nHello from Nios II Uart, please input:\r\n";
char szRead[1];
// open uart
uart = open(UART_NAME, O_ACCMODE); // UART_NAME defined in system.h
if (!uart){
printf("failed to open uart\n");
return;
}
// write uart
if (write(uart, szHello, strlen(szHello)) != strlen(szHello)){
printf("failed to write uart");
close(uart);
return;
}
// none-blocking read
fcntl(uart, F_SETFL, O_ACCMODE | O_NONBLOCK);
while(result >= 0){
result = read(uart, szRead, sizeof(szRead));
if (result == -1){
printf("failed to read uart");
}else if (result > 0){
printf("%c", szRead[0]);
}
}
fcntl(uart, F_SETFL, O_ACCMODE);
close(uart);
} |
|