吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3123|回复: 2
收起左侧

[C&C++ 转载] linux 串口编程入门

[复制链接]
kmyun2006 发表于 2013-11-16 21:43
本帖最后由 kmyun2006 于 2013-11-16 23:26 编辑

/*************************************************************************
        > File Name: rebot.cpp
        > Author:
        > Mail: kmyuan2008@gmail.com
        > Created Time: 2013年10月30日 星期三 00时47分14秒
************************************************************************/

#include<iostream>
#include<stdio.h>
#include<termios.h>
#include<unistd.h>
#include<fcntl.h>
using namespace std;
int speed_arr[]={38400,19200,9600,4800,2400,1200,300};
int name_arr[]={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<sizeof(speed_arr)/sizeof(int);i++)
    {
        if(speed==name_arr)
        {
            tcflush(fd,TCIOFLUSH);
            cfsetispeed(&Opt,speed_arr);
            cfsetospeed(&Opt,speed_arr);
            status = tcsetattr(fd,TCSANOW,&Opt);
            if(status!=0)
            {
                perror("tcsetattr fd\n");
                return;
            }
            tcflush(fd,TCIOFLUSH);
        }
    }
}

int set_parity(int fd,int databits,int stopbits,int parity)
{
    struct termios options;
    if(tcgetattr(fd,&options)!=0)
    {
        perror("SetupSerial 1");
        return -1;
    }
    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 sizeof");
            break;
    }
    switch(parity)
    {
        case 'n':
        case 'N':
            options.c_cflag &= ~PARENB;
            options.c_iflag &= ~INPCK;
            break;
        case 'o':
        case 'O':
            options.c_cflag |=(PARODD | PARENB);
            options.c_iflag |=INPCK;
            break;
        case 'e':
        case 'E':
            options.c_cflag |= PARENB;
            options.c_cflag &= ~PARODD;
            options.c_iflag |= INPCK;
            break;
        case 's':
        case 'S':
            options.c_cflag &=~PARENB;
            options.c_cflag &=~CSTOPB;
            break;
        default:
            fprintf(stderr,"Unsupported parity");
            return -1;
    }
    switch(stopbits)/*设置停止位*/
    {
        case 1:
            options.c_cflag &=~CSTOPB;
            break;
        case 2:
            options.c_cflag |= CSTOPB;
            break;
        default:
            fprintf(stderr,"Unsupported stop bits");
            return -1;
    }
    if(parity !='n')
        options.c_iflag |= INPCK;
    tcflush(fd,TCIOFLUSH);
    options.c_cc[VTIME]=150;/*设置超时15 seconds*/
    options.c_cc[VMIN]=0;
    if(tcsetattr(fd,TCSANOW,&options)!=0)
    {
        perror("SetupSerial 3");
        return -1;
    }
    return 1;
}

int OpenDev(const char *Dev)
{
    int fd=open(Dev,O_RDWR);
    if(-1==fd)
    {
        perror("Can't Open Serial Port");
        return -1;
    }
    else
        return fd;
}
int main()
{
    const char *dev ="/dev/ttyACM0";
    int fd = OpenDev(dev);
    if(-1==fd)
    {
        //perror("提示错误");
        return(-1);
    }
    set_speed(fd,9600);
    if(set_parity(fd,8,1,'N')==-1)
    {
        printf("Set Parity Error");
        return -1;
    }
    while(1)
    {
        cout<<"请输入命令:";
        string cmd;
        cin>>cmd;
        string str;
        if(cmd=="exit")
        {
            close(fd);
            return 1;
        }
        str = cmd + "\r\n";
        //char * a = (char*)str.c_str();
        write(fd,str.c_str(),str.length());
    }
    close(fd);
    return 1;
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

蓝色_ 发表于 2013-11-16 22:15
能把版排好点吗!!!
 楼主| kmyun2006 发表于 2013-11-16 23:27
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-12 05:43

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表