北冥 发表于 2016-4-20 15:34:09

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

我在操作串口的时候,用系统提供的驱动,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;    //从串口读取/发送数据的buffer
        memset(buf,0,sizeof(buf));
       
        c = *argv;
        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);
                        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端的驱动装了没有?

@allen 发表于 2016-4-21 11:04:01

http://forum.cubietech.com/forum.php?mod=viewthread&tid=4544

wty123 发表于 2016-7-26 11:08:01

页: [1]
查看完整版本: 为什么我操作UART的驱动文件,写的时候串口软件里面没反应