免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1464 | 回复: 1
打印 上一主题 下一主题

[其他] 关于串口通信方面问题的求助 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-07-08 09:52 |只看该作者 |倒序浏览
本帖最后由 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 中在串口调试助手中的输入区输入内容,但是不能发送,接收区也接收不到任何内容。这是什么问题造成的。

论坛徽章:
5
丑牛
日期:2014-01-21 08:26:26卯兔
日期:2014-03-11 06:37:43天秤座
日期:2014-03-25 08:52:52寅虎
日期:2014-04-19 11:39:48午马
日期:2014-08-06 03:56:58
2 [报告]
发表于 2013-07-08 14:52 |只看该作者
这是什么问题造成的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP