这时我作的!你可以参考一下!
if ( OK == FileRead( FileName ) )
printf( "\nFile Readed Successed!\n" );
else
printf( "\nFile Readed Failed!\n" );
SerialDevFd = open( "/tyCo/0", O_RDWR, 0 ); // 打开串口设备
if ( ERROR == SerialDevFd )
printf( "\nOpen tyCo/0 Failed!\n" );
else
printf( "\nOpen tyCo/0 Successed!\n" );
/* change baud-rate */
ioctl( SerialDevFd, FIOBAUDRATE, 9600 );
/* set to 'raw' mode */
ioctl( SerialDevFd, FIOSETOPTIONS, OPT_TERMINAL & OPT_RAW );
/* set to data bits*/
// ioctl( SerialDevFd, SIO_HW_OPTS_SET, 8 );
/* create the reader & the writer tasks */
taskId = taskSpawn("SerialTask", 120, 0, 2 * 1024, (FUNCPTR)SerialTask, SerialDevFd,0,0,0,0,0,0,0,0,0);
#include "VxWorks.h"
#include "ioLib.h"
#include "stdio.h"
#include "sioLib.h"
#define COMMANDBEGIN 0x01
#define COMMANDEND 0x1C
#define MAX_RECEIVE_LENGTH 1024
#define WORD(HighByte,LowByte) ((LowByte<<8)+HighByte)
////////////////////////////////////////////////////////////////////////////
//
// 接收命令格式:命令头(7个字节) + 数据(DataLength-1个字节) + 数据校验码
// 注:若只有命令头无数据(DataLength == 0)时,则只接收7个字节
//
// 命令头格式:
//
// 命令头(2个字节):0xAA 0x55
// 命令号(1个字节):0x01
// 数据长度(2个字节):低字节在前,高字节在后:DataLength = ( ReceiveBuf[4] << 8 ) + ReceiveBuf[3];
// 校验码(1个字节):采用和校验:checksum = ReceiveBuf[0] + ReceiveBuf[1] + ReceiveBuf[2] +
// ReceiveBuf[3] + ReceiveBuf[4];
// 命令头结束符(1个字节):0x00
//
// 数据校验码:采用和校验:checksum = buf[0] + buf[1] + ... + buf[DataLength - 1]
//
// 发送命令格式:命令头(7个字节) + 数据(DataLength-1个字节) + 数据校验码
//
// 命令头格式:
//
// 命令头(2个字节):0x55 0xAA
// 命令号(1个字节):0x01
// 数据长度(2个字节):低字节在前,高字节在后:DataLength = ( ReceiveBuf[4] << 8 ) + ReceiveBuf[3];
// 校验码(1个字节):采用和校验:checkSum = SendBuf[0] + SendBuf[1] + SendBuf[2] + SendBuf[3] + SendBuf[4];
// 命令头结束符(1个字节):0x00
//
// 数据校验码:采用和校验:checksum = buf[0] + buf[1] + ... + buf[DataLength - 1]
////////////////////////////////////////////////////////////////////////////
unsigned char SendCommandHead[][7] = // 命令头格式
{
// 命令头 + 命令号+数据长度 + 校验和 + 结束码
{ 0x55, 0xAA, 0x01, 0x00, 0x00, 0x00, 0x00 }, //
{ 0x55, 0xAA, 0x02, 0x00, 0x00, 0x01, 0x00 }, //
{ 0x55, 0xAA, 0x03, 0x00, 0x00, 0x02, 0x00 }, //
{ 0x55, 0xAA, 0x04, 0x00, 0x00, 0x03, 0x00 }, //
{ 0x55, 0xAA, 0x05, 0x00, 0x00, 0x04, 0x00 }, //
{ 0x55, 0xAA, 0x06, 0x00, 0x00, 0x05, 0x00 }, //
{ 0x55, 0xAA, 0x07, 0x00, 0x00, 0x06, 0x00 }, //
{ 0x55, 0xAA, 0x08, 0x00, 0x00, 0x07, 0x00 }, //
{ 0x55, 0xAA, 0x09, 0x00, 0x00, 0x08, 0x00 }, //
{ 0x55, 0xAA, 0x0A, 0x00, 0x00, 0x09, 0x00 }, //
{ 0x55, 0xAA, 0x0B, 0x00, 0x00, 0x0A, 0x00 }, //
{ 0x55, 0xAA, 0x0C, 0x00, 0x00, 0x0B, 0x00 }, //
{ 0x55, 0xAA, 0x0D, 0x00, 0x00, 0x0C, 0x00 }, //
{ 0x55, 0xAA, 0x0E, 0x00, 0x00, 0x0D, 0x00 }, //
{ 0x55, 0xAA, 0x0F, 0x00, 0x00, 0x0E, 0x00 }, //
{ 0x55, 0xAA, 0x10, 0x00, 0x00, 0x0F, 0x00 }, //
{ 0x55, 0xAA, 0x11, 0x00, 0x00, 0x10, 0x00 }, //
{ 0x55, 0xAA, 0x12, 0x00, 0x00, 0x11, 0x00 }, //
{ 0x55, 0xAA, 0x13, 0x00, 0x00, 0x12, 0x00 }, //
{ 0x55, 0xAA, 0x14, 0x00, 0x00, 0x13, 0x00 }, //
{ 0x55, 0xAA, 0x15, 0x00, 0x00, 0x14, 0x00 }, //
{ 0x55, 0xAA, 0x16, 0x00, 0x00, 0x15, 0x00 }, //
{ 0x55, 0xAA, 0x17, 0x00, 0x00, 0x16, 0x00 }, //
{ 0x55, 0xAA, 0x18, 0x00, 0x00, 0x17, 0x00 }, //
{ 0x55, 0xAA, 0x19, 0x00, 0x00, 0x18, 0x00 }, //
{ 0x55, 0xAA, 0x1A, 0x00, 0x00, 0x19, 0x00 }, //
{ 0x55, 0xAA, 0x1B, 0x00, 0x00, 0x1A, 0x00 }, //
{ 0x55, 0xAA, 0x1C, 0x00, 0x00, 0x1B, 0x00 } //
};
//////////////////////////////////////////////////////////////////////
//
// checkSum:对接收数据的前8个进行校验,采用和校验。
//
// buffer:接收数据的起始地址。
//
//////////////////////////////////////////////////////////////////////
unsigned char Checksum( unsigned char *buffer, int Bengin, int End )
{
unsigned char sum = 0;
int i;
for ( i = Bengin; i < End + 1; i++ )
sum += *( buffer + Bengin + i );
return sum;
}
//////////////////////////////////////////////////////////////////////
//
// SerialTask:对接收与发送的线程SerialTask。
//
// SerialDevFd:串口描述符。
//
//////////////////////////////////////////////////////////////////////
void SerialTask( int SerialDevFd )
{
struct fd_set readFds; /* bit mask of fds to read from */
int width; /* number of fds on which to pend */
unsigned char ReceiveBuf[MAX_RECEIVE_LENGTH];
unsigned int DataLength;
unsigned int ReciveLength = 0;
int i = 0;
/* loop forever reading data and servicing clients */
FOREVER
{
/* clear bits in read bit mask */
FD_ZERO( &readFds ); // 清除文件描述符集中对应SerialDevFd的位
/* initialize bit mask */
FD_SET( SerialDevFd, &readFds ); // 设置文件描述符集中对应SerialDevFd的位
width = SerialDevFd + 1; // 文件描述符集有效部分大小
/* pend, waiting for one or more fds to become ready */
if ( ERROR == select( width, &readFds, NULL, NULL, NULL ) ) // 1、文件描述符集有效部分大小 2、读文件描述符集 3、写文件描述符集 4、VxWorks未支持 5、最大等待时间
{
printf( "ERROR!\n" );
}
// check if this fd has data to read
if ( FD_ISSET( SerialDevFd, &readFds ) ) // 如果文件描述符集中对于SerialDevFd的位置位,结果为TRUE,否则为FALSE
{
ReciveLength = 0;
FOREVER
{
// typically read from fd now that it is ready
if ( ERROR == ( read( SerialDevFd, ReceiveBuf + ReciveLength, 1 ) ) ) // 接收一个字符
{
printf( "ERROR Reading\n" );
}
else
{
ReciveLength++; // 接收字符长度加1
if ( ( 1 == ReciveLength ) && ( 0xAA != ReceiveBuf[0] ) ) // 对接收的第1个字符校验
{
printf( "Error:Invalid Synchronization Yard One!\n" );
ReciveLength = 0;
bzero( ReceiveBuf, sizeof( ReceiveBuf ) );
}
else if ( ( 2 == ReciveLength ) && ( 0x55 != ReceiveBuf[1] ) ) // 对接收的第2个字符校验
{
printf( "Error:Invalid Synchronization Yard Two!\n" );
ReciveLength = 0;
bzero( ReceiveBuf, sizeof( ReceiveBuf ) );
}
else if ( ( 3 == ReciveLength ) &&
( ( COMMANDBEGIN > ReceiveBuf[2] ) || ( COMMANDEND < ReceiveBuf[2] ) ) ) // 对接收的第3个字符校验
{
printf( "Error:Invalid Command!\n" );
ReciveLength = 0;
bzero( ReceiveBuf, sizeof( ReceiveBuf ) );
}
else if ( ( 6 == ReciveLength ) && ( Checksum( ReceiveBuf, 0, 4 ) != ReceiveBuf[5] ) )
{
printf( "Error:Command Head Checksum Failed!\n" );
ReciveLength = 0;
bzero( ReceiveBuf, sizeof( ReceiveBuf ) );
}
else if ( 7 == ReciveLength )
{
if ( 0x00 == ReceiveBuf[6] )
{
// DataLength = ( ReceiveBuf[4] << 8 ) + ReceiveBuf[3];
DataLength = WORD( ReceiveBuf[3], ReceiveBuf[4] );
if ( 0 == DataLength )
{
printf( "Length:%d Data:", ReciveLength );
for ( i = 0; i < ReciveLength; i++ )
{
printf( "%x ", ReceiveBuf );
}
printf( "\n" );
write( SerialDevFd, SendCommandHead[ReceiveBuf[2] - 1], 7 ); // 发送命令头
// 根据命令号SendCommandHead[ReceiveBuf[3]]发送数据
// write( SerialDevFd, SendCommandHead[ReceiveBuf[3]], 8 );
ReciveLength = 0;
bzero( ReceiveBuf, sizeof( ReceiveBuf ) );
}
}
else
{
printf( "Error:Command End!\n" );
ReciveLength = 0;
bzero( ReceiveBuf, sizeof( ReceiveBuf ) );
}
}
else if ( 8 <= ReciveLength )
{
DataLength--;
if ( 0 == DataLength ) // 数据接收完成
{
if ( Checksum( ReceiveBuf, 7, DataLength - 1 ) != ReceiveBuf[DataLength - 1] )
{
printf( "Error:Data Checksum!\n" );
ReciveLength = 0;
bzero( ReceiveBuf, sizeof( ReceiveBuf ) );
}
else
{
printf( "Length:%d Data:", ReciveLength );
for ( i = 0; i < ReciveLength; i++ )
{
printf( "%x ", ReceiveBuf );
}
printf( "\n" );
// 校验接收的命令号
write( SerialDevFd, SendCommandHead[ReceiveBuf[2] - 1], 7 ); // 发送命令头
// 根据命令号SendCommandHead[ReceiveBuf[3]]发送数据
// write( SerialDevFd, SendCommandHead[ReceiveBuf[3]], 8 );
// 对接收缓冲区清空
ReciveLength = 0;
bzero( ReceiveBuf, sizeof( ReceiveBuf ) );
}
}
}
// normally service request, for this example print it
/* if ( 0 == Length )
{
break;
}
*/ }
} // End FOREVER
} // End FD_ISSET
} // End FOREVER
}