///////////////////////////////////////////////////////////////////////////////
//
// MODULE:
//
// $Id: console.c$
//
// ABSTRACT:
//
// COM console communication demo.
//
// ENVIRONMENT:
//
// VxWorks
//
// HISTORY:
//
// <author rel='nofollow' onclick='return false;'> <Date> <Version> <Modification>
// Jinnee 2008/04/03 1.0 Created
//
///////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <taskLib.h>
#include "console.h"
void config_com(int sfd) //Config COM1
{
ioctl(sfd, FIOSETOPTIONS, OPT_RAW); //Set tty device to RAW_MODE
ioctl(sfd, FIOBAUDRATE, 9600); //Set baudrate to 9600bps
ioctl(sfd, FIOFLUSH, 0); //Discards all bytes in the input and output buffers
}
void main(void)
{
taskSpawn ("COM_TASK", TASK_PRIORITY, 0, TASK_STACK_SIZE, (FUNCPTR)conTask,
0,0,0,0,0,0,0,0,0,0);
}
void conTask(void)
{
int sfd, width, num;
FD_SET fds_data;
unsigned char tmpStr[128]; /* length could be changed here! */
sfd = open(CONSOLE_NAME, O_RDWR, 0);
if (sfd == ERROR)
{
printf("open %s failed!\n", CONSOLE_NAME);
return;
}
config_com(sfd);
while (1)
{
FD_ZERO(&fds_data); // Zeroes all bits
FD_SET(sfd, &fds_data); // Set the bit corresponding to a specified file descriptor
width = sfd + 1;
if (select(width, &fds_data, NULL, NULL, NULL) == ERROR)
{
printf("select COM failed! Exception.\n");
continue;
}
/* read one byte from com */
num = read(sfd, (char *)&tmpStr, (size_t)1);
if (num != 1)
{
printf("read com error!\n");
continue;
}
/* write one byte to com */
writeCom(sfd, width, fds_data, tmpStr, 1);
}
}
void writeCom(int sfd, int width, FD_SET fds_data, unsigned char pBuf[], int length)
{
if (select(width, NULL,&fds_data, NULL, NULL) == ERROR)
{
printf("select in writeCom failed!\n");
return;
}
FD_ZERO(&fds_data);
FD_SET(sfd, &fds_data);
if (FD_ISSET(sfd, &fds_data))
write(sfd, (char*)pBuf, length);
else
printf("FD_ISSET in writeCom failed!\n");
}