Hello
I have a project where I use my board's UART4 with the stdio and I can "printf" just fine. Additionally, I need that very same port for another part of my project so in my code I open it with:
port = fopen("ttye:", IO_SERIAL_RAW_IO);
That works wonderfully because I can send data through the port using:
write(port, data, size);
But I'm having some problems when reading bytes from the port. I found a post where someone has a similar case scenario than me and wants to find out when four bytes have arrived into the port. Nevertheless, the post is 5 years old and inconclusive since it does not have a satisfactory answer and it doesn't help my situation.
It is suggested in the post to use "IO_IOCTL_CHAR_AVAIL" to detect bytes available. I'm doing it like this:
ioctl(port, IO_IOCTL_CHAR_AVAIL, &result);
But the problem is that I only get TRUE if the reception is 1 byte only and I need to detect when more than 1 byte has arrived.
Why is this happening? Or is there any other way of detecting when more than one bytes have been received?
By the way, I can't use interrupts and it's ok to poll as I'm doing it right now. And I can't use "fread" because that blocks execution even if I open the port with "IO_SERIAL_NON_BLOCKING".
I don't know if I'm facing these problems because of the fact that I'm trying to use the same port as the stdio, but I can write just fine to the port using printf and write. Isn't it the same for the reception?
Thanks!
Hi Manuel:
See below lines for the usage of IO_IOCTL_CHAR_AVAIL.
do {
if (IO_OK != ioctl(f_uart, IO_IOCTL_CHAR_AVAIL, &ch_avail)) {
printf("\nUnexpected error occured");
_task_block(); /* unexpected error occured */
}
if (ch_avail == TRUE) {
/* read data from UART */
num_done = fread(uart2usb + uart2usb_num, sizeof(uart2usb[0]), 1, f_uart); /* read max. 1 character from UART */
if (IO_ERROR == num_done)
{
printf("\nUnexpected error occured");
_task_block(); /* unexpected error occured */
}
uart2usb_num += num_done;
}
} while ( (ch_avail == TRUE) && (uart2usb_num < (sizeof(uart2usb)
You can call _time_delay() to allow lower priority tasks to run.
Regards
Daniel