Code: Select all
SerialClose( SYNCON_PORT_NUMBER ); //close the serial port
syncon_serial_fd = OpenSerial( SYNCON_PORT_NUMBER, 250000, 2, 8, eParityNone ); //reopen the port in interrupt mode
iprintf("->Serial: Syncon Handler Opened on Port %d Successfully!\n", SYNCON_PORT_NUMBER); //report progress
if (OSTaskCreate(serial_in_handler_task, NULL, ( void * ) &serial_in_stack[USER_TASK_STK_SIZE], ( void * ) serial_in_stack, SERIAL_IN_PRIO ) != OS_NO_ERR )
{
iprintf("->**Error Creating Serial In Task**\n");
return 1;
}
Code: Select all
void serial_in_handler_task( void *pd )
{
static unsigned char input_buffer[1024]; //input buffer
if (syncon_serial_fd > 0) //make sure the port opened without any errors
{
while (1){ //continuous loop that blocks on UART read
int bytes_read = ReadWithTimeout(syncon_serial_fd, (char*)input_buffer, 1024, TICKS_PER_SECOND); //read from port
switch (bytes_read){ //switch on return value
case 0: //TIMEOUT
break;
case -1: //ERROR
iprintf("->Serial: Error ** \n");
break;
default: //NORMAL
//iprintf("->Serial: Read %d bytes\n", bytes_read);
process_incoming_bytes(input_buffer, bytes_read); //process incoming bytes
break;
}
}
iprintf("->**TERMINATING SYNCON TASK**\n");
}
else
{
//TODO: Handle the error
}
}
Code: Select all
void process_incoming_bytes(unsigned char byte_buffer[], int bytes_read)
{
for (int i=0; i<bytes_read; i++)
{
char current_byte = byte_buffer[i]; //get byte to be processed
switch (syncon_packet_pointer)
{
default: //receive the payload of the packet
if (current_byte != 0)
{
iprintf("->Serial: Byte %d = %x\n", syncon_packet_pointer, current_byte);
}
if (syncon_packet_pointer != SYNCON_PACKET_LENGTH-1)
{
packet_buffer[syncon_packet_pointer++] = current_byte;
}
else
{
packet_buffer[syncon_packet_pointer] = current_byte;
syncon_packet_count++;
//iprintf("->Serial: Count - %d\n", syncon_packet_count);
syncon_packet_pointer = 0;
}
break;
} // end switch
}
}