CubieBoard中文论坛

 找回密码
 立即注册
搜索
热搜: unable
查看: 8206|回复: 3

为什么我操作UART的驱动文件,写的时候串口软件里面没反应

[复制链接]
发表于 2016-4-20 15:34:09 | 显示全部楼层 |阅读模式
我在操作串口的时候,用系统提供的驱动,write()串口驱动文件的时候,PC端的串口软件为什么没反应呢,高手帮忙看看


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <limits.h>
#include <string.h>

#include "uart_set.h"

#define DEV_NAME "/dev/ttySP0"

int main(int argc,char **argv)
{
        if(argc < 2){
                printf("Usage:program <r/s>\n");
                return -1;
        }
       
        char c;
        int fd_port,i=0,j=0;
        int return_value=0;
        int length_data=0;
        char buf[100];    //从串口读取/发送数据的buffer
        memset(buf,0,sizeof(buf));
       
        c = *argv[1];
        switch(c)
        {
                case 'r':
                {
                        //打开串口0
                        fd_port = open(DEV_NAME,O_RDWR);
                        if(-1 == fd_port)
                        {
                          exit(1);
                  }
               

                        //串口,波特率,数据位,奇偶校验,停止位
                        return_value = set_Baudrate(fd_port,115200,8,'N',1);
                        if(-1 == return_value)
                        {
                          exit(1);
                        }

                        //接收串口0的数据
                         while(1)
                        {
                          length_data = read(fd_port,buf,1);  //数据的发送使用write(fd_port,buf,1); buf为要发送的字符串
                          if(-1 == length_data)
                          {
                                   exit(1);
                          }

                          if(0 > analysis_data(buf))
                           return -1;

                          memset(buf,0,100);
                        }

                        close(fd_port);
                        break;
                }
               
                case 's':
                default:
                {
                        if(argc < 3)
                        {
                                printf("Usage:program <s> <data>\n");
                                return -1;
                        }
                        fd_port = open(DEV_NAME,O_RDWR);
                        if(-1 == fd_port)
                        {
                          exit(1);
                        }

                        //串口,波特率,数据位,奇偶校验,停止位
                        return_value = set_Baudrate(fd_port,115200,8,'N',1);
                        if(-1 == return_value)
                        {
                          exit(1);
                        }

                        //向串口0发送数据
                        strcpy(buf,argv[2]);
                        while(1)
                        {
                          length_data = write(fd_port,buf,1);  //数据的发送使用write(fd_port,buf,1); buf为要发送的字符串
                          if(-1 == length_data)
                          {
                                   exit(1);
                          }

                          if(0 > analysis_data(buf))
                           return -1;
                           //发送完的字符从buf中去除
                   memset(buf,0,100);
                       
                       
                        }
                       
                        close(fd_port);
                       
                         break;
                }/* case 's' 结束*/
               
        }/* switch() 结束*/
       
        return 0;
}

回复

使用道具 举报

发表于 2016-4-21 10:40:16 | 显示全部楼层
PC端的驱动装了没有?
回复 支持 反对

使用道具 举报

发表于 2016-4-21 11:04:01 | 显示全部楼层
回复 支持 反对

使用道具 举报

发表于 2016-7-26 11:08:01 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|粤ICP备13051116号|cubie.cc---深刻的嵌入式技术讨论社区

GMT+8, 2024-4-19 21:55 , Processed in 0.029052 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2012 Comsenz Inc. | Style by Coxxs

返回顶部