#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"
#include "stdio.h"
/*UART mode set*/
#define SIO_STOPBIT_SET 0x1020
#define SIO_CHARLGH_SET 0x1021
#define SIO_PTYODD_SET 0x1024
#define SIO_PTYEVE_SET 0x1025
#define SIO_BAUD_SET 0x1003
typedef unsigned char BYTE;
int comfd;
/*
* 函数名: int ComPortInit(int portid,int baudrate,int stopBitLength,int dataLength,int odd_even_none)
* 函数描述: 初始化PowerPC 8270 板的串口,并设置其波特率,停止位,数据长度以及是否奇偶校验
* 调用:
* m8270_sccuart_drv(),m8270_sccuart_create() ,open(),ioctl();logmsg()用于打印显示,可以去掉.
* 被调用:
* 访问记录:
* 修改记录:
* 输入:
* int portid: 需要初始化的串口编号,从1 到4 取值
* int baudrate: 需要设置的波特率,参数为0 时不设置波特率(默认为115200)
* int stopBitLength: 停止位的长度,可为1 和2.参数为0时不设置停止位(默认为1位)
* int dataLength: 数据位的长度,可为6,7,8. 参数为0时不设置数据位长度(默认为8位)
* int odd_even_none: 是否奇偶校验,1 为奇校验,2 为偶校验 .参数为0时不设置奇偶校验(默认为无校验)
* 输出: 无
* 返回:
* 正确初始化时返回串口操作的句柄,失败时返回-1.
* 其它:
*/
int ComPortInit(int portid,int baudrate,int stopBitLength,int dataLength,int odd_even_none)
{
int sccfd;
int errorflag;
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) ;
// if(errorflag== 0)
// {
// logMsg("Com%d init OK\n",portid,0,0,0,0,0);
sprintf(str,"sccuart%d",portid);
sccfd=open("/tyCo/3",0,0);
/* if (ERROR==sccfd)
{
printf("can not open device!\n") ;
return 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);
}
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 buf[20],* sendbuf;
int sendlen=20;
unsigned char tempi;
sendbuf=buf;
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=20;
unsigned char tempi;
unsigned char buf[20];
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);
}
if( FD_ISSET(comid,&rfds))
{
if(read(comid,readbuf,readlen)==ERROR)
{
logMsg("com read EEROR\n",0,0,0,0,0,0);
}
else
{
//readbuf[readlen]=buf[0];
readlen++;
//if(buf[0]=='o')
//{
readbuf[readlen]='\0';
logMsg("com read %s OK\n",readbuf,0,0,0,0,0);
readlen=0;
// }
}
}
}
}
int testUart()
{
logMsg("testUart\n");
comfd=ComPortInit(4,38400,1, 8, 2);
if(comfd<0)
{
logMsg("init comport error\n",0,0,0,0,0);
}
else
{
taskSpawn ("COMSend", 120, 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);
}
}