- 论坛徽章:
- 0
|
本帖最后由 ZX880211 于 2013-07-08 16:35 编辑
我是学习vxwork的小菜鸟一只,这里有个程序是关于串口通信的,还请各位大侠帮助,感激不尽~~~~
powerpc有两个串口,\tyCo\0和\tyCo\1,其中\tyCo\0接到PC 1,\tyCo\1接PC 2,PC 1 装有vxworks,PC 2装有串口调试助手。
#include <stdio.h>
#include <taskLib.h>
#include <sioLib.h>
#include <string.h>
int tyRecv(int fd)
{
int readCnt1;
char rd;
char buff[512];
int i;
FOREVER
{
i=0;
taskDelay(50);
ioctl(fd,FIONREAD,(int) &readCnt1); /* 判断com2接收数据缓冲区是否有数据到来 */
if(readCnt1>0)
{
while(readCnt1>0)
{
read(fd,&rd,1);
readCnt1--;
buff[i++]=rd;
}
buff='\0';
printf("read '%s' from com2 whose data were sent by com1\n",buff);
}
}
}
int tySend(int fd)
{
int wrtCount;
char buff[]="I am god of war!"; /* 发送内容 */
wrtCount = write(fd,buff,strlen(buff));
printf("write %d bytes to com1\n",wrtCount); /* 写com1口,然后数据就会通过串口直连线发送到com2方了 */
}
int testMain()
{
int com1_Fd,com2_Fd;
com1_Fd = open("/tyCo/0",2,0) ; /* 打开串口0,即serial<->com1 */
com2_Fd = open("/tyCo/1",2,0); /* 打开串口1,即serial2<->com2 */
/* 设置串口0,亦即com1的波特率115200,8数据为,1停止位,无校验位 */
if ( ERROR==ioctl(com1_Fd,FIOBAUDRATE,115200) )
{
printf("can not set BAUDRATE!\n" ;
return ERROR ;
}
if ( ERROR==ioctl(com1_Fd,SIO_HW_OPTS_SET,(CLOCAL|CREAD|CS &~(HUPCL|STOPB|PARENB)))
{
printf("can not set OPT!\n" ;
return ERROR ;
}
ioctl(com1_Fd,FIOFLUSH,0);
/* 设置串口1,亦即com2的波特率115200,8数据为,1停止位,无校验位 */
if ( ERROR==ioctl(com2_Fd,FIOBAUDRATE,115200) )
{
printf("can not set BAUDRATE!\n" ;
return ERROR ;
}
if ( ERROR==ioctl(com2_Fd,SIO_HW_OPTS_SET,(CLOCAL|CREAD|CS &~(HUPCL|STOPB|PARENB)))
{
printf("can not set OPT!\n" ;
return ERROR ;
}
ioctl(com2_Fd,FIOFLUSH,0);
/* 发起接受数据的任务 */
taskSpawn("recv",60,0,0x2000,(FUNCPTR)tyRecv,com2_Fd,0,0,0,0,0,0,0,0,0);
/* 发起发送数据的任务*/
taskSpawn("send",80,0,0x2000,(FUNCPTR)tySend,com1_Fd,0,0,0,0,0,0,0,0,0);
}
输出结果是:I am god of war!write 16 bytes to com1.
在 PC 2 中在串口调试助手中的输入区输入内容,但是不能发送,接收区也接收不到任何内容。这是什么问题造成的。 |
|