串口备份

串口备份





#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <mysql/mysql.h>
#include "uart.h"

#define BUF_SIZE 1024  
int main(void)  
{  
    int fd,res;  

    char buf[BUF_SIZE];  
    char buff[BUF_SIZE];  

    fd_set inset,tempset;  
    struct timeval tv;  


    FD_ZERO(&inset);  

    //1. 打开串口        
    if((fd = open_uart(1)) < 0)  
        {  
            perror("open port");  
            return -1;  
        }  

    if(set_uart_config(fd,115200,8,'N',1) < 0)  
        {  
            perror("set com_config");  
            return -1;  
        }  

    FD_SET(fd,&inset);  

    tv.tv_sec = 5;  
    tv.tv_usec = 0;  

    do{  
        tempset = inset;  


#if 0
        printf("Please input some data(quit to exit)\n");  
        memset(buf,0,BUF_SIZE);  
        if(fgets(buf,BUF_SIZE,stdin) == NULL)  
            {  
                perror("input error");  
                break;  
            }         

        write(fd,buf,strlen(buf));  
#endif
        memset(buff,0,BUF_SIZE);  
        tv.tv_sec = 5;  
        tv.tv_usec = 0;  
        res = select(fd + 1,&tempset,NULL,NULL,&tv);   
        if(res == 0)
             write(fd,"是收到水电费水电费\r\n",strlen("是收到水电费水电费\r\n"));
            //printf("time out 怎么办 \n");  
        else if (res < 0)  
            printf("select error\n");  
        else  
        {  
            if(FD_ISSET(fd,&inset))
            //if(1)
            {
                int rx_length = read(fd,buff,BUF_SIZE);

                if(rx_length > 0){
                  buff[rx_length] = '\0';
                  //write(fd,buff,BUF_SIZE);
                  printf("%d  from uart:%s\r\n",rx_length,buff); 
                }  
            } 




        }  

    }while(1 );  

    close(fd);  
    return 0;     
} 



#if 0
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
int main()
{
    int fd = -1;
    fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1)
    {
        perror("Open Serial Port Error!\n");
        return -1;
    }

    struct termios options;
    tcgetattr(fd, &options);
    //115200, 8N1
    options.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
    options.c_iflag = IGNPAR;
    options.c_oflag = 0;
    options.c_lflag = 0;
    options.c_cc[VTIME]=0;
    options.c_cc[VMIN]=1;
    tcflush(fd, TCIFLUSH);
    tcsetattr(fd, TCSANOW, &options);
    unsigned char rx_buffer[256];
    while(1){
        int rx_length = read(fd, (void*)rx_buffer, 255);
        if (rx_length > 0)
        {
            //Bytes received
            rx_buffer[rx_length] = '\0';
            printf("%i bytes read : %s\n", rx_length, rx_buffer);
            write(fd,"是收到水电费水电费\r\n",strlen("是收到水电费水电费\r\n"));
            //tcflush(fd, TCIFLUSH);   
        }
    }
    close(fd);
    return 0;
}
#endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值