serial

http://wenku.baidu.com/view/bbfe874ae45c3b3567ec8b94.html


#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' */

}
 
#define BUFSIZE 512

int main(int argc, char **argv)
{

    int fd;

    int nread;

    char buff[BUFSIZE];

    struct termios oldtio, newtio;

    struct timeval tv;

    char *dev ="/dev/ttyS0";

    fd_set rfds;

    if ((fd = open(dev, O_RDWR | O_NOCTTY))<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;

    while (TRUE)

    {

        printf("wait...\n");

        FD_ZERO(&rfds);

        FD_SET(fd, &rfds);

        if (select(1+fd, &rfds, NULL, NULL, &tv)>0)

        {

            printf("wait...\n");

            if (FD_ISSET(fd, &rfds))
            {

                nread=read(fd, buff, BUFSIZE);

                printf("readlength=%d\n", nread);

                buff[nread]='\0';

                printf("%s\n", buff);
            }
        }
    }

    tcsetattr(fd, TCSANOW, &oldtio);

    close(fd);
}

 

 









//= = = = = send.c= = = = = =

#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>

//初始化串口选项:

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' */
}

int main(int argc, char **argv)
{
    int fd;

    int nCount, nTotal, i;

    struct termios oldtio, newtio;

    char *dev ="/dev/ttyUSB0";

/*
    if ((argc!=3) || (sscanf(argv[1], "%d", &nTotal) != 1))

    {

        printf("err: need tow arg =%d!\n", argc );

        return -1;

    }  
*/
    if ((fd = open(dev, O_RDWR | O_NOCTTY))<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);

    for (i=0; i<nTotal; i++)

    {
        nCount=write(fd, argv[2], strlen(argv[2]));

        printf("send data\n");

        sleep(1);

    }

    tcsetattr(fd, TCSANOW, &oldtio);

    close(fd);

    return 0;
}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值