• 大小: 230KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-03
  • 语言: 其他
  • 标签: linux串口  源码  

资源简介

linux串口编程 非阻塞read 打包write 设备打开 串口配置

资源截图

代码片段和文件信息

#include 
#include 
#include 
#include 
#include 
#include 
#include “sq110_uart.h“
#include 
#define TTY_TEST  “/dev/ttyO2“
struct termios tio;
int init_tty(struct termios *oldtio){
int fd;
struct termios newtio;
if((fd = open(TTY_TEST O_RDWR | O_NOCTTY)) < 0)
{
   printf(“tty_test open fail\n“); 
   return -1;
}
fcntl(fd F_SETFL 0);
tcgetattr(fd oldtio);
bzero(&newtio sizeof(newtio));
cfsetispeed(&newtio B115200);
cfsetospeed(&newtio B115200);
newtio.c_cflag |= CLOCAL|CREAD;
newtio.c_cflag &= ~CSIZE;
newtio.c_cflag |= CS8;
newtio.c_cflag &= ~(PARENB|PARODD);  
newtio.c_cflag &= ~CSTOPB;
newtio.c_cflag &= ~CRTSCTS;
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
newtio.c_oflag &= ~OPOST;


tcflush(fd TCIFLUSH);
newtio.c_cc[VTIME] = 1;
newtio.c_cc[VMIN] = 0;
tcsetattr(fd TCSANOW &newtio);

return fd;
}

int send(int ftty unsigned char str[] int len){
   // printf(“send -----> len = %d\n“ len);
return (!write(ftty str len)) ? -1 : 1;
}

int recieve(int ftty int streamfd){
int n max_fd len count=0 i = 0;
char buffer[2048] *ptr = buffer;
fd_set input;
struct timeval timeout;

FD_ZERO(&input);
FD_SET(ftty &input); 
max_fd = ftty+1;
    
    memset(buffer 0 2048);
    count = 0;
while(1){
timeout.tv_sec = 1;
timeout.tv_usec = 0; 
if((n = select(max_fd &input NULL NULL &timeout)) <= 0)
            break;
len = read(ftty ptr 0x05);

ptr += len;
count += len;
if(count>=5)
break;
}

buffer[count] = 0;
printf(“reading %d data:\n“ count); 
    for (i = 0; i < count; i++)
        printf(“        msg[%d] = 0x%x\n“ i buffer[i]);
}

void hand_shake_init(fd)
{
 if(send(fd ribao_init_msg 5) < 1)
     {
            printf(“tty write fail...\n“);
            return ;
     }  
    recieve(fd 0);
    send(fd ribao_init_msg 5);

}

unsigned int char_to_int(unsigned char * strunsigned int len){
   unsigned int tmpijtmp2;
   
   for(i=0;i   {
   tmp2=1;
   for(j=i+1;j    {
   tmp2*=10;
   }
   tmp+=((*str)*(tmp2));
   str++;
   }
   return tmp;
}

unsigned char phone[18]={
12345678912345678
};

int main(int argc char *argv[]){
int fd = -1 count = 0;
struct termios oldtio;
int cnt = 0 i = 0;

printf(“start\n“);
if((fd = init_tty(&oldtio)) < 0) {
        printf(“init tty fail so return....\n“);
        return -1;
    }

    unsigned int phone_num;
    unsigned int *phonep;
    int count_time=115700;
   
    while(1) {
        
        send(fd ribao_init_msg 5);
        phone_num=char_to_int(&phone[count%9]8);
       // printf(“tty_send:%d\n“phone_num);
        phonep=&phone_num;
        char *char_phonep=(char *)phonep;
        for(i=9;i>5;i--)
        {
ack_test_msg[i]=*char_phonep;
char_phonep++;
    }
  

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    I.A....       585  2018-08-14 17:01  uart_test\Makefile

    I.A....    473002  2018-08-14 17:01  uart_test\sq110_uart

    I.A....      3607  2018-08-14 17:01  uart_test\sq110_uart.c

    I.A....       479  2018-08-14 17:01  uart_test\sq110_uart.h

    I..D...         0  2018-08-14 17:01  uart_test

----------- ---------  ---------- -----  ----

               477673                    5


评论

共有 条评论