일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 플라스크
- OCPP
- 전기차충전기
- 라즈베리파이
- STM32
- esp8266
- 홈어시스턴트
- lambda
- 전기차
- AWS
- dynamodb
- 급속충전기
- 파이썬
- 완속충전기
- 충전기
- YMODEM
- 펌웨어
- 에버온
- thread
- everon
- IOT Core
- 안드로이드
- homeassistant
- 전기차충전
- flask
- 서버리스
- 보안
- Android
- raspberry
- 디자인패턴
Archives
- Today
- Total
Louie NRT Story
[RaspberryPi] Thread & Shared Memory & USB Serial Data 본문
에너지
[RaspberryPi] Thread & Shared Memory & USB Serial Data
hyeok0724.kim@gmail.com 2019. 5. 6. 21:41반응형
[Description]
- Thread 를 3개 생성함
- 하나의 Thread는 Shared Memory를 지속적으로 읽으면서 데이터가 있으면 출력
- 또 하나의 Thread는 USB를 통한 데이터를 지속적으로 출력
- 또 하나는 그냥 Printf 하는 단순 작업
[MakeFile]
1
2
3
4
5
6
7
8
9
10
11
|
CC = gcc
TARGET = main
LDFLAGS = -pthread -lwiringPi
OBJS = main.o SharedMemory.o
main : $(OBJS)
$(CC) -o $(TARGET) $(LDFLAGS) $(OBJS)
main.o: main.c SharedMemory.h
$(CC) -c main.c
SharedMemory.o: SharedMemory.c SharedMemory.h
$(CC) -c main.c SharedMemory.c
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
[SharedMemory.c Code]
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
|
#include "SharedMemory.h"
int shmid;
int SHM_create()
{
shmid = shmget((key_t)SHARED_MEMORY_KEY, MEMORY_SIZE, 0666|IPC_CREAT);
if(shmid == -1)
{
perror("shmget failed: ");
return -1;
}
return shmid;
}
int SHM_delete()
{
if(shmctl(shmid, IPC_RMID, 0) == -1)
{
perror("shmctl failed: ");
return -1;
}
return 1;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
[SharedMemory.h Code]
1
2
3
4
5
6
7
8
9
10
11
|
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <unistd.h>
#define SHARED_MEMORY_KEY 1111
#define MEMORY_SIZE 1024
int SHM_create();
int SHM_delete();
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
[Main.c Code]
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
|
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <errno.h> //error output
#include <stdint.h> //uint8_t definitions
//wiring Pi
#include <wiringPi.h>
#include <wiringSerial.h>
#include "SharedMemory.h"
#define READ_CLIENT_FLAG 0
#define READ_SERVER_FLAG 1
#define PRINT_CLIENT_FLAG 2
char device[]= "/dev/ttyACM0";
// filedescriptor
int fd;
unsigned long baud = 9600;
//unsigned long time=0;
void signal_handler(int signo)
{
printf("terminate Process\n");
SHM_delete();
exit(0);
}
void *t_function(void *data)
{
printf("Test");
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
char* thread_name = (char*)data;
int i = 0;
while(i<3)
{
printf("[%s] pid:%u, tid:%x --- %d\n",
thread_name, (unsigned int)pid, (unsigned int)tid, i);
i++;
sleep(1);
}
}
void *t_buffer()
{
int shmid;
shmid = SHM_create();
char *buffer;
char *string;
buffer = shmat(shmid, (void *)0, 0);
if(buffer == (void *)-1)
{
perror("shmat failed: ");
}
buffer[0] = READ_CLIENT_FLAG;
string = buffer + 1;
while(1)
{
if(buffer[0] == READ_SERVER_FLAG)
{
puts(string);
strcat(string, "by server");
buffer[0] = PRINT_CLIENT_FLAG;
}
printf("wait data\n");
sleep(1);
}
}
void setup(){
printf("%s \n", "Raspberry Startup!");
fflush(stdout);
//get filedescriptor
if ((fd = serialOpen (device, baud)) < 0){
fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
exit(1); //error
}
//setup GPIO in wiringPi mode
if (wiringPiSetup () == -1){
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
exit(1); //error
}
}
void loop()
{
// read signal
if(serialDataAvail (fd)){
char newChar = serialGetchar (fd);
printf("%c", newChar);
fflush(stdout);
}
}
void *t_usb_comm()
{
setup();
while(1) loop();
}
int main()
{
signal(SIGINT, signal_handler);
pthread_t p_thread[3];
int thr_id;
int status;
char p1[] = "thread_1";
char p2[] = "thread_2";
char pM[] = "thread_m";
sleep(1);
thr_id = pthread_create(&p_thread[0], NULL, t_buffer, NULL);
if(thr_id < 0)
{
perror("thread create error:1 ");
exit(0);
}
thr_id = pthread_create(&p_thread[1], NULL, t_usb_comm, NULL);
if(thr_id < 0)
{
perror("thread create error: ");
exit(0);
}
thr_id = pthread_create(&p_thread[2], NULL, t_function, (void*)p2);
if(thr_id < 0)
{
perror("thread create error: ");
exit(0);
}
t_function((void *)pM);
pthread_join(p_thread[0], (void **)&status);
pthread_join(p_thread[1], (void **)&status);
pthread_join(p_thread[2], (void **)&status);
printf("when terminal\n");
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
반응형
'에너지' 카테고리의 다른 글
[Raspberry Pi] Initialization on Raspberry Pi (0) | 2019.10.23 |
---|---|
[Raspberry Pi] Install Raspbian on Raspberry Pi (0) | 2019.10.23 |
[RasbperryPi] Shared Memory Queue (0) | 2019.05.06 |
[RaspberryPi] Chatting Program using Shared Memory Flag (0) | 2019.05.06 |
[RaspberryPi] Controll LED (0) | 2019.05.06 |
Comments