linux 下串口通信



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

#define BAUDRATE B115200 ///Baud rate : 115200
#define DEVICE "/dev/ttyAMA0"
#define SIZE 1024

 

int nFd = 0;
struct termios stNew;
struct termios stOld;

//Open Port & Set Port

int SerialInit()
{
    nFd = open(DEVICE, O_RDWR|O_NOCTTY|O_NDELAY);
    
    if(-1 == nFd)
    {
        perror("Open Serial Port Error!\n");
        return -1;
    }

    if( (fcntl(nFd, F_SETFL, 0)) < 0 )

    {

        perror("Fcntl F_SETFL Error!\n");
        return -1;
    }

    if(tcgetattr(nFd, &stOld) != 0)
    {
        perror("tcgetattr error!\n");
        return -1;
    } 

    stNew = stOld;
    cfmakeraw(&stNew);//将终端设置为原始模式,该模式下所有的输入数据以字节为单位被处理

    //set speed
    cfsetispeed(&stNew, BAUDRATE);//115200
    cfsetospeed(&stNew, BAUDRATE);

    //set databits 
    stNew.c_cflag |= (CLOCAL|CREAD);
    stNew.c_cflag &= ~CSIZE;
    stNew.c_cflag |= CS8;

 

    //set parity
    stNew.c_cflag &= ~PARENB;
    stNew.c_iflag &= ~INPCK;

 

    //set stopbits
    stNew.c_cflag &= ~CSTOPB;
    stNew.c_cc[VTIME]=0;    //指定所要读取字符的最小数量
    stNew.c_cc[VMIN]=1; //指定读取第一个字符的等待时间,时间的单位为n*100ms
                //如果设置VTIME=0,则无字符输入时read()操作无限期的阻塞

    tcflush(nFd,TCIFLUSH);  //清空终端未完成的输入/输出请求及数据。
    if( tcsetattr(nFd,TCSANOW,&stNew) != 0 )
    {
        perror("tcsetattr Error!\n");
        return -1;
    }

    return nFd;
}

 

int main(int argc, char **argv)
{
    int nRet = 0;
    char buf[SIZE];

    if( SerialInit() == -1 )
    {
        perror("SerialInit Error!\n");
        return -1;
    }

    bzero(buf, SIZE);
    
    while(1)
    {

        nRet = read(nFd, buf, SIZE);

        if(-1 == nRet)

        {

            perror("Read Data Error!\n");

            break;

        }

        if(0 < nRet)

        {

            buf[nRet] = 0;

            printf("Recv Data: %s\n", buf);

        }

    }

 

    close(nFd);

    return 0;

}

下面这个在//dev/ttyUSB*下可用,在/devttyAMA*下无法接受,可能是串口设置的问题,暂不处理,以后有空再看看是什么问题 

参考 https://blog.csdn.net/oyoung_2012/article/details/80403258

#include"unistd.h"
#include"fcntl.h"
#include"termios.h"
#include"string.h"
#include"iomanip"

void ttyconfig(int ttyid)
{
  struct termios tios;
  tios.c_oflag=0;
  tios.c_iflag=0;
  tios.c_lflag=0;
  cfsetispeed(&tios,0010002);
  cfsetospeed(&tios,0010002);
  
  //data bits
  tios.c_cflag&=~0x30;
  tios.c_cflag|=CS8;
  
  //stop bits
  tios.c_cflag&=~CSTOPB;
  
  //parity
  tios.c_cflag&=~PARENB;
  
  tios.c_cc[VMIN]=0;
  tios.c_cc[VTIME]=50;
  
  tcsetattr(ttyid,TCSANOW,&tios);
  tcflush(ttyid,TCIOFLUSH);
  
}

void setMode(std::string ttypath,int mode=1)
{
  unsigned char buf1[9]={0xAA,0x05,0x00,0x15,0x01,0x00,0xC5,0xEB,0xAA}; 
  unsigned char buf2[9]={0xAA,0x05,0x00,0x15,0x01,0x01,0xC6,0xEB,0xAA};
  unsigned char buf3[9]={0xAA,0x05,0x00,0x16,0x01,0x00,0xC6,0xEB,0xAA};
  unsigned char buf4[9]={0xAA,0x05,0x00,0x16,0x01,0x02,0xC8,0xEB,0xAA};
  unsigned char *buf;
  
  if(mode==1) //自动校正关闭
    buf=buf1; 
  else if(mode==2) //自动校正开启
    buf=buf2; 
  else if(mode==3) //快门校正
    buf=buf3; 
  else if(mode==4) //背景校正
    buf=buf4; 
  
  int ttyid=open(ttypath.c_str(),O_RDWR|O_NOCTTY|O_NONBLOCK);
  if(ttyid<0)
  {
    //sduo usermod -aG dialout currentusername
    std::cout<<"tty open failed\n"<<std::endl;
    exit(-1);
  }
  ttyconfig(ttyid);
  
  int sz=sizeof(buf1);
  unsigned char res_buf[9];
  while(write(ttyid,buf,sz)<0)
    std::cout<<"<0"<<std::endl;;
  //int res=write(ttyid,buf,sz) ;
  //std::cout<<"   "<<res<<std::endl;
  while(1)
  {
    if(read(ttyid,res_buf,sizeof(res_buf))>0)
    {
      //for(int i=0;i<sizeof(res_buf);i++)
	//std::cout<<std::setbase(16)<<"0x"<<(int)res_buf[i]<<std::endl;
      break;
    }
  }
  
  close(ttyid);
  
}

int main(int argc,char **argv)
{
  std::string ttypath="/dev/ttyUSB0";
  setMode(ttypath,atoi(argv[1]));
  
}

猜你喜欢

转载自blog.csdn.net/haha074/article/details/82014398