按键驱动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/input.h>
#include <pthread.h>
 
#define CMD_CHANGLE "\xEE\xB1\x00\x00\x01\xFF\xFC\xFF\xFF"
int salesman_num=6;
int fd;
int key_cond = 0;
int set_opt(int,int,int,char,int);
static pthread_mutex_t key_lock;
void key_run();
 
void main(int argc, char* argv[])
{
    int ret,flag = 0;
    int i=0;
    pthread_t key_id;
    char uart[] = {"/dev/ttySAC3"};
     
     
    unsigned char cmd[9] = { 0xEE, 0xB1, 0x00, 0x00, 0x00, 0xFF, 0xFC, 0xFF, 0xFF};
    unsigned char buf[16]={0};
    //sprintf(cmd,"%H %H %H %H %H %H %H %H %H","EE", "B1", "00", "00", flag++, "FF", "FC", "FF", "FF");
    //printf("%s\n",cmd);
    printf("%s\n",uart);
    pthread_mutex_init(&key_lock, NULL);
    if((fd = open(uart, O_RDWR))<0)
    {  
        printf("open %s is failed\n", uart);   
        return;
    }
    else{
        set_opt(fd, 9600, 8, 'N', 1);
        //set_opt(fd, 115200, 8, 'N', 1);
        ret = pthread_create(&key_id,NULL,(void *)key_run,NULL);
        if(ret!=0){
        printf("Create pthread error!\n");
        exit(1);
        }
        sleep(5);
        while(1){
            if(key_cond){
                pthread_mutex_lock(&key_lock);
                key_cond=0;
                pthread_mutex_unlock(&key_lock);
                //sleep(5);
                continue;
            }
            if(flag > 5 ){
                flag = 1;
                //cmd[4] = salesman_num;           
            }
            cmd[4] = flag;
            write(fd,cmd,9);
            printf("%x\n",cmd[4]);
            sleep(3);
            memset(buf,0,16);
            read(fd,buf,9);
            for(i=0;i<9;i++ )
                printf("%x ,",buf[i]);
            printf("\n");
            flag++;
        }
    }
}
 
 
 
int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
    struct termios newtio,oldtio;
    if  ( tcgetattr( fd,&oldtio)  !=  0) {
        perror("SetupSerial 1");
        return -1;
    }
    bzero( &newtio, sizeof( newtio ) );
    newtio.c_cflag  |=  CLOCAL | CREAD;
    newtio.c_cflag &= ~CSIZE;
 
    switch( nBits )
    {
    case 7:
        newtio.c_cflag |= CS7;
        break;
    case 8:
        newtio.c_cflag |= CS8;
        break;
    }
 
    switch( nEvent )
    {
    case 'O':
        newtio.c_cflag |= PARENB;
        newtio.c_cflag |= PARODD;
        newtio.c_iflag |= (INPCK | ISTRIP);
        break;
    case 'E':
        newtio.c_iflag |= (INPCK | ISTRIP);
        newtio.c_cflag |= PARENB;
        newtio.c_cflag &= ~PARODD;
        break;
    case 'N'
        newtio.c_cflag &= ~PARENB;
        break;
    }
 
    switch( nSpeed )
    {
    case 2400:
        cfsetispeed(&newtio, B2400);
        cfsetospeed(&newtio, B2400);
        break;
    case 4800:
        cfsetispeed(&newtio, B4800);
        cfsetospeed(&newtio, B4800);
        break;
    case 9600:
        cfsetispeed(&newtio, B9600);
        cfsetospeed(&newtio, B9600);
        break;
    case 115200:
        cfsetispeed(&newtio, B115200);
        cfsetospeed(&newtio, B115200);
        break;
    case 460800:
        cfsetispeed(&newtio, B460800);
        cfsetospeed(&newtio, B460800);
        break;
    case 921600:
        printf("B921600\n");
        cfsetispeed(&newtio, B921600);
                cfsetospeed(&newtio, B921600);
        break;
    default:
        cfsetispeed(&newtio, B9600);
        cfsetospeed(&newtio, B9600);
        break;
    }
    if( nStop == 1 )
        newtio.c_cflag &=  ~CSTOPB;
    else if ( nStop == 2 )
    newtio.c_cflag |=  CSTOPB;
    newtio.c_cc[VTIME]  = 0;
    newtio.c_cc[VMIN] = 0;
    tcflush(fd,TCIFLUSH);
    if((tcsetattr(fd,TCSANOW,&newtio))!=0)
    {
        perror("com set error");
        return -1;
    }
    return 0;
}
 
void key_run(){
    int fd_key;
    int version;
    int ret;
    unsigned char cmd[9] = { 0xEE, 0xB1, 0x00, 0x00, 0x00, 0xFF, 0xFC, 0xFF, 0xFF};
    struct input_event ev;
    fd_key = open("/dev/input/event0", O_RDONLY);
    if (fd < 0) {
        printf("open file failed\n");
        exit(1);
    }
    ioctl(fd_key, EVIOCGVERSION, &version);
    printf("evdev driver version is 0x%x: %d.%d.%d\n",version, version>>16, (version>>8) & 0xff, version & 0xff);
 
    while (1) {
        ret = read(fd_key, &ev, sizeof(struct input_event));
        if (ret < 0) {
            printf("read event error!\n");
            exit(1);
        }
        if (ev.type == EV_KEY){
            printf("type %d,code %d, value %d\n", ev.type, ev.code, ev.value);
            if(ev.value){
                pthread_mutex_lock(&key_lock);
                key_cond=1;
                pthread_mutex_unlock(&key_lock);
                if(salesman_num >=15){
                    salesman_num = 6;
                }else{
                    salesman_num++;
                }
                cmd[4] = salesman_num;
                write(fd,cmd,9);
            }
            //cmd [4] = salesman_num;
            //write(fd,cmd,9);
        }
    }
}

 

1
2
3
4
5
6
7
8
9
10
#CC = /usr/local/arm/4.3.2/bin/arm-linux-gcc
#CFLAGS = -I/usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/usr/include
 
CC = /usr/local/arm/arm-2009q3/bin/arm-none-linux-gnueabi-gcc
CFLAGS = -I/usr/local/arm/arm-2009q3/arm-none-linux-gnueabi/libc/usr/include
 
uarttest: uarttest.c
    $(CC) $(CFLAGS) -o uarttest uarttest.c -static -lpthread
clean:
    rm -rf uarttest

  

 

posted on   lydstory  阅读(23)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-01-16 http://fuzzing.org/
2021-01-16 find安全
2021-01-16 这是什么大拿,编程语言选择
2021-01-16 web浏览器模糊器
2021-01-16 web应用模糊器
2021-01-16 网络协议模糊器
2021-01-16 文件格式模糊器

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示