linux环境下读取串口特定字符串的实现总结

刚刚开始接触串口的时的感觉是懵逼状的,自从用了霸王洗发水后… 不是,不是,题跑偏了。自从深入了解了串口的知识后发现又是一片知识的海洋,当工作中要运用到串口的知识时会感觉到有点迷茫,不过在大神的指点下已经很好的完成了这方面的工作。所以我在今天写这篇文章算是是对我往后工作的一个总结。
一:串口简介
串行口是计算机一种常用的接口,具有连接线少,通讯简单,得到广泛的使用。常用的串口是RS-232-C 接口(又称 EIA RS-232-C)它是在 1970 年由美国电子工业协会(EIA)联合贝尔系统、 调制解调器厂家及计算机终端生产厂家共同制定的用于串行通讯的标准。它的全名是”数据终端设备(DTE)和数据通讯设备(DCE)之间串行二进制数据交换接口技术标准”该标准规定采用一个 25 个脚的 DB25 连接器,对连接器的每个引脚的信号内容加以规定,还对各种信号的电平加以规定。传输距离在码元畸变小于 4% 的情况下,传输电缆长度应为 50 英尺。
二:读取串口特定数据的代码实现

  /*************************************************************************

File Name: readcom.c
Author: luca_xie
Mail: 15902090525@139.com
Created Time: 2016年05月11日 星期三 14时18分46秒
**************************************************************/

#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   <string.h>
#define TRUE 1
//初始化串口选项:  

    void setTermios(struct termios * pNewtio, int uBaudRate)
{
bzero(pNewtio, sizeof(struct termios)); /* clear struct for new port settings */
//8N1
pNewtio->c_cflag = uBaudRate | CS8 | CREAD | CLOCAL;
pNewtio->c_iflag = IGNPAR;
pNewtio->c_oflag = 0;
pNewtio->c_lflag = 0; //non ICANON
/*
   initialize all control characters
   default values can be found in /usr/include/termios.h, and
   are given in the comments, but we don't need them here*/

pNewtio->c_cc[VINTR] = 0; /* Ctrl-c */
pNewtio->c_cc[VQUIT] = 0; /* Ctrl-\ */
pNewtio->c_cc[VERASE] = 0; /* del */
pNewtio->c_cc[VKILL] = 0; /* @ */
pNewtio->c_cc[VEOF] = 4; /* Ctrl-d */
pNewtio->c_cc[VTIME] = 5; /* inter-character timer, timeout VTIME*0.1 */
pNewtio->c_cc[VMIN] = 0; /* blocking read until VMIN character arrives */
pNewtio->c_cc[VSWTC] = 0; /* '\0' */
pNewtio->c_cc[VSTART] = 0; /* Ctrl-q */
pNewtio->c_cc[VSTOP] = 0; /* Ctrl-s */
pNewtio->c_cc[VSUSP] = 0; /* Ctrl-z */
pNewtio->c_cc[VEOL] = 0; /* '\0' */
pNewtio->c_cc[VREPRINT] = 0; /* Ctrl-r */
pNewtio->c_cc[VDISCARD] = 0; /* Ctrl-u */
pNewtio->c_cc[VWERASE] = 0; /* Ctrl-w */
pNewtio->c_cc[VLNEXT] = 0; /* Ctrl-v */
pNewtio->c_cc[VEOL2] = 0; /* '\0' */

}

void clear_txt(void) //每一次操作都得清空文本中的内容
{
//  int ret1 = open("2.txt", O_WRONLY | O_CREAT);
//      close(ret1);
int ret = open("2.txt", O_WRONLY | O_CREAT | O_TRUNC);
if(ret == -1)
{
    printf("open txt fail\n");
    exit(-1);
}
close(ret);
}
/*对串口数据的读取函数主要是自定的findString函数*/
int findString(char * srcstr,int srclens,char * findstr,char *dststr)
{
int i;
int k;
int strlens = strlen(findstr);
for(i = 0;i<srclens;i++){
    if( memcmp(findstr,srcstr+i,strlens) == 0 ){
        i += strlens;
        for(k = 0;i<srclens;i++){
            if( k == 0 ){
                if( srcstr[i] == ' ' ){
                    continue;
                }
            }
            if( srcstr[i] <= 0x0d ){
                dststr[k++] = 0;
                return 0;
            }
            dststr[k++] = srcstr[i];
        }
    }
}
return -1;
}
#define BUFSIZE 1024
int main(int argc, char **argv)
{
int fd,fp;
int fq;
int nread , nwrite;
int nread1;
char *buff = malloc(BUFSIZE);
memset(buff,0,sizeof(char)*BUFSIZE);
char *buff1=malloc(BUFSIZE);
memset(buff1,0,sizeof(char)*BUFSIZE);
char *IP_buf = malloc(BUFSIZE);
memset(IP_buf,0,sizeof(char)*BUFSIZE);
struct termios oldtio, newtio;
struct timeval tv;
char *temp;
char *temp1;
char *dev ="/dev/ttyS0";
fd_set rfds;
if ((fd = open(dev, O_RDWR)) < 0)
{
    printf("err: can't open serial port!\n");
    return -1;
}
tcgetattr(fd, &oldtio); /* save current serial port settings */
setTermios(&newtio, B115200);
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &newtio);                 
tv.tv_sec=30;
tv.tv_usec=0;
int i;
FILE *file;

clear_txt();/*清除2.txt中的内容*/
if((fp =open("2.txt",O_RDWR | O_CREAT)) < 0)//打开2.txt文本
{
    printf("can not open file!\n");
    exit(-1);
}
while(TRUE)
{
    printf("wait 1...\n");
    FD_ZERO(&rfds);
    FD_SET(fd,&rfds);
    if (select(1+fd, &rfds, NULL, NULL, &tv)>0)
    {
        if (FD_ISSET(fd, &rfds))
        {
            nread = read(fd,buff,1);
            nwrite = write(fp,buff,1);
        }
    }
    i++;
    printf("%d\n",i);
    if(i>400) {printf("I'm going to close 2.txt\n");close(fp);usleep(100);break;}

}

printf("read start ---------------\n");
if((fq =open("2.txt",O_RDONLY)) < 0)//打开2.txt文本
{
    printf("can not open file!\n");
    exit(-1);
}
printf("open 2.txt succ!\n");
nread1 = read(fq,buff1,BUFSIZE);
char get_ssid[128];
if(0==findString(buff1,nread1,"ssid=",get_ssid)){
    printf("ssid:%s\n",get_ssid);
}
if(0==findString(buff1,nread1,"password=",get_ssid)){
    printf("password:%s\n",get_ssid);
}


return 0;
tcsetattr(fd, TCSANOW, &oldtio);
close(fd);
close(fq);
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值