xueerjin 2015-10-07 14:01 采纳率: 0%
浏览 1947

centos下串口自发自收c++代码实现

各位大神求助 搞了两周了一直是一个问题 我是菜鸟 希望大家指点指点
我的问题是我想让串口(usb转rs232)实现自发自收功能 但每次read只能读到一个字符
在write后面加个延时函数 sleep 之后可以完整读到数据
但我老师说串口这种即时通讯怎么能延时 不合逻辑

代码如下:
主函数
main .cpp

#include /*标准输入输出定义*/
#include /*标准函数库定义*/
#include /*Unix 标准函数定义*/
#include
#include
#include /*文件控制定义*/
#include /*PPSIX 终端控制定义*/
#include /*错误号定义*/
#include
#include "SerialPort.h"
using namespace std;

int main()
{
int fd;
/*以读写方式打开串口*/
fd = open( "/dev/ttyUSB0", O_RDWR);
if (-1 == fd){
/* 不能打开串口一*/
perror(" 提示错误!");
return 0;
}

//串口设置
set_speed(fd,9600);
int ok=set_Parity(fd,8,1,'n');
if(ok==0)
{
    perror("串口设置出错");
    return 0;
}


/*读写功能实现*/
char s[100];
printf("发送数据:\n");
gets(s);
char buffer[1024];
int size;
write(fd,s,sizeof(s));
//sleep(1);
size = read( fd, buffer,sizeof(buffer));
if(size==-1)
{
    perror("读取数据出错");
    return 0;
}
close(fd);
printf("接受到的数据:\n");
printf( "%s", buffer);
return 0;

}

                                                                       配置函数头文件

SerialPort.h

#ifndef SERIALPORT_H_
#define SERIALPORT_H_
#define TRUE 1
#define FALSE 0
/* 波特率*/
struct termio
{
unsigned short c_iflag; /* 输入模式标志 /
unsigned short c_oflag; /
输出模式标志 /
unsigned short c_cflag; /
控制模式标志*/
unsigned short c_lflag; /* local mode flags /
unsigned char c_line; /
line discipline */
};
int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
19200, 9600, 4800, 2400, 1200, 300, };
void set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0;i<int(sizeof(speed_arr)/sizeof(int));i++)
{
if (speed == name_arr[i])
{
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0) {
perror("tcsetattr fd");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
}

/**
*@brief   设置串口数据位,停止位和效验位
*@param  fd     类型  int  打开的串口文件句柄
*@param  databits 类型  int 数据位   取值 为 7 或者8
*@param  stopbits 类型  int 停止位   取值为 1 或者2
*@param  parity  类型  int  效验类型 取值为N,E,O,,S
*/
int set_Parity(int fd,int databits,int stopbits,int parity)
{
    struct termios options;
    if  ( tcgetattr( fd,&options)  !=  0) {
        perror("SetupSerial 1");
        return(FALSE);
    }
    options.c_cflag &= ~CSIZE;
    switch (databits) /*设置数据位数*/
    {
    case 7:
        options.c_cflag |= CS7;
        break;
    case 8:
        options.c_cflag |= CS8;
        break;
    default:
        fprintf(stderr,"Unsupported data size\n"); return (FALSE);
    }
switch (parity)
{
    case 'n':
    case 'N':
        options.c_cflag &= ~PARENB;   /* Clear parity enable */
        options.c_iflag &= ~INPCK;     /* Enable parity checking */
        break;
    case 'o':
    case 'O':
        options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
        options.c_iflag |= INPCK;             /* Disnable parity checking */
        break;
    case 'e':
    case 'E':
        options.c_cflag |= PARENB;     /* Enable parity */
        options.c_cflag &= ~PARODD;   /* 转换为偶效验*/
        options.c_iflag |= INPCK;       /* Disnable parity checking */
        break;
    case 'S':
    case 's':  /*as no parity*/
        options.c_cflag &= ~PARENB;
        options.c_cflag &= ~CSTOPB;break;
    default:
        fprintf(stderr,"Unsupported parity\n");
        return (FALSE);
    }
/* 设置停止位*/
switch (stopbits)
{
    case 1:
        options.c_cflag &= ~CSTOPB;
        break;
    case 2:
        options.c_cflag |= CSTOPB;
       break;
    default:
         fprintf(stderr,"Unsupported stop bits\n");
         return (FALSE);
}
/* Set input parity option */
if (parity != 'n')
    options.c_iflag |= INPCK;
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
    perror("SetupSerial 3");
    return (FALSE);
}
return (TRUE);
}

#endif /* SERIALPORT_H_ */

我实在是自己研究不出来了 才来求问的 希望大家帮帮我看看 在此拜谢

  • 写回答

3条回答 默认 最新

  • threenewbee 2015-10-07 15:52
    关注

    usb转rs232要看什么芯片。一些芯片缺乏原生com的一些特征,硬件的限制你程序写死了也没办法。

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧