引用 8 楼 frank_zhou 的回复:
最好把真实用的代码帖出来,否则谁知道哪个小地方错了呢
ok
#include
#include "taskLib.h"
#include "locale.h"
#include "string.h"
#include "stdlib.h"
#include "Vxworks.h"
#include "configAll.h"
#include "dosFsLib.h"
#include "sioLib.h"
/*UART mode set*/
#define SIO_STOPBIT_SET 0x1020
#define SIO_CHARLGH_SET 0x1021
#define SIO_PTYODD_SET 0x1024
#define SIO_PTYEVE_SET 0x1025
typedef unsigned char BYTE;
int comfd;
/*
*/
int ComPortInit(int portid,int baudrate,int stopBitLength,int dataLength,int odd_even_none)
{
int sccfd;
int errorflag = 0;
unsigned char str[10];
if(portid<1||portid>4)
{
logMsg("Com%d is not existed\n",portid,0,0,0,0,0);
return(ERROR);
}
if(baudrate!=300&&baudrate!=600&&baudrate!=1200&&
baudrate!=2400&&baudrate!=4800&&baudrate!=9600&&
baudrate!=19200&&baudrate!=38400&&baudrate!=43000&&
baudrate!=56000&&baudrate!=57600&&baudrate!=115200&&baudrate!=0)
{
logMsg("invalid baudrate\n",0,0,0,0,0,0);
return(ERROR);
}
if(stopBitLength!=1&&stopBitLength!=2&&stopBitLength!=0)
{
logMsg("invalid stopbit length\n",0,0,0,0,0,0);
return(ERROR);
}
if(dataLength!=6&&dataLength!=7&&dataLength!=8&&dataLength!=0)
{
logMsg("invalid data length\n",0,0,0,0,0,0);
return(ERROR);
}
if(odd_even_none!=1&&odd_even_none!=2&&odd_even_none!=0)
{
logMsg("invalid odd even or none verify\n",0,0,0,0,0,0);
return(ERROR);
}
/**
m8270_sccuart_drv(portid);
errorflag=m8270_sccuart_create(portid) ;
**/
/**
把串口驱动程序添加到系统中
**/
ttyDrv();
/**
下面语句把串口 com2添加到系统中," tyC0/l"是设备名,sysSerialChanGet(1)
用来获得指向串口描述符的指针,后面两个参数定义了读缓冲区和写缓冲区的大小
**/
ttyDevCreate("/tyCo/1",sysSerialChanGet(1),100,100);
if(errorflag== 0)
{
logMsg("Com%d init OK\n",portid,0,0,0,0,0);
sccfd=open("/tyCo/1",O_RDWR,0);
if(sccfd<0)
{
logMsg("com%d open failed\n",portid,0,0,0,0,0);
return(ERROR);
}
else
{
logMsg("com%d open OK\n",portid,0,0,0,0,0);
ioctl(sccfd,FIOSETOPTIONS,OPT_RAW);
ioctl(sccfd,FIOFLUSH,0);
if(baudrate!=0)
{
ioctl(sccfd,SIO_BAUD_SET,baudrate);
}
/**
if(stopBitLength!=0)
{
ioctl(sccfd,SIO_STOPBIT_SET,stopBitLength);
}
if(dataLength!=0)
{
ioctl(sccfd,SIO_CHARLGH_SET,dataLength);
}
switch(odd_even_none)
{
case 1:
ioctl(sccfd,SIO_PTYODD_SET,0);
break;
case 2:
ioctl(sccfd,SIO_PTYEVE_SET,0);
break;
default:
break;
}
**/
return(sccfd);
}
}
else
{
logMsg("Com%d init failed\n",portid,0,0,0,0,0);
return(ERROR);
}
}
int ComWrite(int comid)
{
unsigned char sendbuf[20];
unsigned char sendlen=20;
unsigned char tempi;
logMsg("in ComWrite()\n",0,0,0,0,0,0);
for(tempi=0;tempi<20;tempi++)sendbuf[tempi]=65+tempi;
sendbuf[19]='\0';
while(1)
{
if((write(comid,sendbuf,sendlen))==ERROR)
{
logMsg("com write EEROR\n",0,0,0,0,0,0);
}
else
{
logMsg("com write %s OK\n",sendbuf,0,0,0,0,0);
}
taskDelay(60);
}
}
int ComRead(int comid)
{
unsigned char readbuf[50];
unsigned char readlen=0;
unsigned char tempi;
unsigned char buf[10];
fd_set rfds;
logMsg("in ComRead()\n",0,0,0,0,0,0);
/*for(tempi=0;tempi<20;tempi++)readbuf[tempi]=0x32;
readbuf[19]='\0';*/
while(1)
{
FD_ZERO(&rfds);
FD_SET(comid,&rfds);
if(select(comid+1,&rfds,NULL,NULL,NULL)< 0 )
{
logMsg("select error,quit Writing\n",0,0,0,0,0,0);
}
logMsg("in ComRead 1\n",0,0,0,0,0,0);
if( FD_ISSET(comid,&rfds))
{
if(read(comid,buf,1)==ERROR)
{
logMsg("com read EEROR\n",0,0,0,0,0,0);
}
else
{
logMsg("in ComRead 2\n",0,0,0,0,0,0);
readbuf[readlen]=buf[0];
readlen++;
/**
if(buf[0]=='A')
{
}
**/
readbuf[readlen]='\0';
logMsg("com read %s OK\n",readbuf,0,0,0,0,0);
readlen=0;
}
}
else
logMsg("in ComRead 0\n",0,0,0,0,0,0);
}
}
int testUart()
{
logMsg("testUart\n");
comfd=ComPortInit(1,19200,1, 8, 0);
if(comfd<0)
{
logMsg("init comport error\n",0,0,0,0,0);
}
else
{
taskSpawn ("COMSend", 100, 0, 4096, (FUNCPTR)ComWrite,comfd, 0, 0, 0, 0, 0, 0, 0, 0, 0);
taskSpawn ("COMRecv", 100, 0, 4096, (FUNCPTR) ComRead,comfd, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
}