-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_read4.c
502 lines (418 loc) · 13.2 KB
/
serial_read4.c
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <pthread.h>
#include <time.h>
#include <errno.h>
#include <mysql/mysql.h>
#include "modbus.h"
#define MODBUS_SERIAL_DEV "/dev/ttyM0"
#define MODBUS_SERIAL_BAUDRATE 9600 /* 9600, 38400, 115200, ... */
#define MODBUS_SERIAL_PARITY 'N' /* 'N', 'E', or 'O' */
#define MODBUS_SERIAL_DATABITS 8 /* 5, 6, 7, or 8 */
#define MODBUS_SERIAL_STOPBITS 1 /* 1 or 2 */
#define MODBUS_DEVICE_ID 10
#define MODBUS_TIMEOUT_SEC 3
#define MODBUS_TIMEOUT_USEC 0
#define MODBUS_DEBUG ON
/*---------------database_start----------------------*/
#define MyHOST "127.0.0.1"
#define MyNAME "root"
#define MyDB "SystemControl"
#define QSIZE 256
MYSQL *dp;
MYSQL_RES *qp;
char query[QSIZE] = "INSERT INTO Schedule_data (schedule_id, station_no, address, data, time) VALUES ( 1, 10, 1523, '00000000', '2015-04-24 00:00:00')";
char query2[QSIZE] = "UPDATE Schedule_data SET data= ' 13' where address=1522 and schedule_id=1 and station_no= 11";
char query3[QSIZE] = "UPDATE Schedule_data SET time= ' 13' where address=1522 and schedule_id=1 and station_no= 11";
char query4[QSIZE] = "select * from Schedule_data where schedule_id=1 and station_no=10 and address= 1521";
/*----------------database_end---------------------------*/
typedef struct schedule
{
char sch_id[4];
char sta_no[3];
char addr[5];
char t_start[20];
char t_stop[20];
char period[3];
}schedule_t;
void* schedule_thread(void *a);
int opendb()
{
char passwd[25];/*避免使用strings指令得知密碼*/
passwd[0]='g';
passwd[1]='h';
passwd[2]='3';
passwd[3]='7';
passwd[4]='1';
passwd[5]='4';
passwd[6]='3';
passwd[7]='7';
passwd[8]='1';
passwd[9]='5';
dp = mysql_init(NULL);
/* Use any MySQL API functions here */
if(dp == NULL)
{
fprintf(stderr,"mysql_init() faild\n");
return 2;
}
mysql_real_connect(dp, MyHOST, MyNAME, passwd, MyDB, 0, NULL, 0);
if(dp == NULL)
{
fprintf(stderr,"mysql_real_connect() faild\n");
return 3;
}
/* if(mysql_select_db(dp, MyDB) != 0)
{
fprintf(stderr, "mysql_select_db() failed\n");
mysql_close(dp);
return 4;
}*/
return 0;
}
void sql_write(char *sch_id, char *station_no, char *address, char *data, char *time)
{
sprintf(query, "INSERT INTO Schedule_data (schedule_id, station_no, address, data, time) VALUES ( %s, %s, %s, '%s', '%s')", sch_id, station_no, address, data, time);
printf("%s\n",query);
}
void sql_write2(char *sch_id,char *station_no, char *address, char *data)
{
sprintf(query2, "UPDATE Schedule_data SET data= '%s' where address=%s and schedule_id=%s and station_no= %s", data, address, sch_id, station_no);
printf("%s\n",query2);
}
int server (int client_socket)
{
int length=10;
char* text;
int ret;
/* Allocate a buffer to hold the text. */
text = (char*) malloc (length);
/* Read the text itself, and print it. */
ret = recv(client_socket, text , length, 0);
if(ret==0) {
printf ("peer close socket\n");
return 1;
}
else if (ret<0) {
printf("socket error: %s\n", strerror(errno));
return 1;
}
else {
text[ret] = '\0';
printf ("%s\n", text);
return 1;
}
/* Free the buffer. */
free (text);
return 0;
}
void* listen_thread(void *id)
{
int *p_id = (int*) id;
//Wait for php send
char soc_n[20];
sprintf(soc_n, "sch_%d",*p_id);
printf("pid=%s\n",soc_n);
const char* const socket_name="sch";
int socket_fd;
struct sockaddr_un name;
int client_sent_quit_message;
/* Create the socket. */
socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
/* Indicate this is a server. */
name.sun_family = AF_LOCAL;
strcpy (name.sun_path, socket_name);
bind (socket_fd, (struct sockaddr *)&name, sizeof(struct sockaddr_un));
/* Listen for connections. */
listen (socket_fd, 20);
/* Repeatedly accept connections, spinning off one server() to deal
with each client. Continue until a client sends a "quit" message. */
do {
struct sockaddr_un client_name;
socklen_t client_name_len;
int client_socket_fd;
/* Accept a connection. */
client_socket_fd = accept(socket_fd, (struct sockaddr *)&client_name, &client_name_len);
/* Handle the connection. */
client_sent_quit_message = server(client_socket_fd);
/* Close our end of the connection. */
close (client_socket_fd);
}
while (!client_sent_quit_message);
/* Remove the socket file. */
close (socket_fd);
unlink (socket_name);
return NULL;
}
int main(int argc, char *argv[])
{
if(argc != 2){
printf("INsufficient argument");
return -1;
}
//檔案處理
int fileLength = 0;
FILE *fptr;
int i = 0;
char *temp;
if(!(fptr = fopen(argv[1], "r")))
return -1;
//移動指標到檔案的結尾
if(fseek(fptr, 0, SEEK_END))
return -1;
//取得檔案的大小
fileLength = ftell(fptr);
//移動指標到檔案的起始
rewind(fptr);
char s[fileLength];
int num=0;
while(fgets(s, fileLength, fptr) != NULL)
{
num++;
}
rewind(fptr);
schedule_t sch[num-1];
int k=0;
while(fgets(s, fileLength, fptr) != NULL)
{
if(i == 0){
for(int j=0; j< num; j++){
strcpy(sch[j].sch_id, s);
printf("%d:%s\n",j,sch[j].sch_id);
}
}
else{
temp = strtok(s, " ");
for(int j=0; temp != NULL; j++)
{
if(j == 0)strcpy(sch[k].sta_no,temp);
if(j == 1)strcpy(sch[k].addr, temp);
if(j == 2)strcpy(sch[k].t_start, temp);
if(j == 3){
strncat(sch[k].t_start," ", 1);
strncat(sch[k].t_start,temp,strlen(temp));
}
if(j == 4)strcpy(sch[k].t_stop, temp);
if(j == 5){
strncat(sch[k].t_stop, " ", 1);
strncat(sch[k].t_stop, temp,strlen(temp));
}
if(j == 6){
temp = strtok(temp, "\n");
strcpy(sch[k].period, temp);
}
printf("%s\n", temp);
temp = strtok(NULL, " ");
}
k++;
}
//printf("csts:%s \n no:%s %s %s %s %s\n",sch[1].sch_id,sch[1].sta_no, sch[1].addr,sch[1].t_start,sch[1].t_stop,sch[1].period);
i++;
}
//Write pid to sql
int err, rows, cols;
//err = opendb();
//if(err)
//{
/*Database is not open*/
// return err;
//}
int p_id=getpid();
//char q[QSIZE] = "UPDATE Schedule SET pid=10000 where schedule_id=1";
//sprintf(q, "UPDATE Schedule SET pid=%d where schedule_id=%d", p_id, sch[1].sch_id);
//if( mysql_query(dp, q))
//{
// fprintf(stderr, "%s\n", mysql_error(dp));
//}
printf("num=%d\n",num);
for(int k=0;k<=num-2;k++)
printf("%s \n %s %s %s %s %s\n",sch[k].sch_id,sch[k].sta_no, sch[k].addr,sch[k].t_start,sch[k].t_stop,sch[k].period);
pthread_t listen;
pthread_create(&listen, NULL, listen_thread, (void*)&p_id);
pthread_t threads[num-1];
for(int k=0;k<num-1;k++)
{
pthread_create(&threads[k], NULL, schedule_thread, (void *)(sch+k));
}
void *result;
pthread_join(listen, &result);
//for(int k=0;k<num-1;k++)
// pthread_join(threads[k], &result);
return 0;
}
pthread_mutex_t beers_lock = PTHREAD_MUTEX_INITIALIZER;
void* schedule_thread(void* parm)
{
MYSQL_ROW row;
schedule_t* temp = (schedule_t*)parm;
printf("v v: %s \n %s \n %s %s %s %s\n",temp->sch_id,temp->sta_no, temp->addr,temp->t_start,temp->t_stop,temp->period);
struct tm time_start, time_stop;
char q1[20],q2[20],q3[20];
strptime(temp->t_start, "%Y-%m-%d %H:%M:%S", &time_start);//類似scanf
strftime(q1, 20, "%Y-%m-%d %H:%M:%S", &time_start);//類似sprintf
printf("%s\n",q1);
printf ("驗證 %d-%d-%d ", (time_start.tm_year),( time_start.tm_mon), time_start.tm_mday);
printf("%d:%d:%d\n",time_start.tm_hour, time_start.tm_min, time_start.tm_sec);
strptime(temp->t_stop, "%Y-%m-%d %H:%M:%S", &time_stop);//類似scanf
strftime(q2, 20, "%Y-%m-%d %H:%M:%S", &time_stop);//類似sprintf
printf("%s\n",q2);
time_t timestart, timestop;
timestart = mktime(&time_start);
timestop = mktime(&time_stop);
printf("%d\n", timestart);
printf("%d\n", timestop);
struct timeval start;
struct timeval end;
struct timeval res;
while(1){
time_t time_now;
struct tm p;
time(&time_now);
printf("now_time:%d\n", time_now);
// localtime_r(&time_now,&p);
// time_now=mktime(&p);
// printf("now_time:%d\n", time_now);
if(time_now >= timestart && time_now <= timestop)
{
//time(&time_now);
//p=localtime(&time_now);
gettimeofday(&start, NULL);
//time(&time_now);
//p=localtime(&time_now);
//sprintf (q3,"%d-%d-%d %d:%d:%d\n", (1900+p->tm_year),( 1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
//printf("%s\n",q3);
pthread_mutex_lock(&beers_lock);
modbus_t *ctx;
struct timeval timeout;
int ret, i, rc, ii;
// int nb_pointers;
// unit16_t *tab_rp_registers;
uint16_t regs[MODBUS_MAX_READ_REGISTERS] = {0};
char regs_8[MODBUS_MAX_READ_REGISTERS]={0};
ctx = modbus_new_rtu(MODBUS_SERIAL_DEV,
MODBUS_SERIAL_BAUDRATE,
MODBUS_SERIAL_PARITY,
MODBUS_SERIAL_DATABITS,
MODBUS_SERIAL_STOPBITS);
if (ctx == NULL) {
fprintf(stderr, "Unable to create the libmodbus context\n");
exit(-1);
}
i = modbus_rtu_get_serial_mode(ctx);
if( i == MODBUS_RTU_RS232)
{
printf("Serial mode = RS232\n");
ret = modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS232);
if(ret < 0)
fprintf(stderr, "modbus_rtu_set_serial_mode() error: %s\n", strerror(errno));
}
else if(i == MODBUS_RTU_RS485)
{
printf("Serial mode = RS485\n");
ret = modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS485);
if(ret < 0)
fprintf(stderr, "modbus_rtu_set_serial_mode() error: %s\n", strerror(errno));
}
else
{
printf("Serial mode = RS485\n");
ret = modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS485);
if(ret < 0)
fprintf(stderr, "modbus_rtu_set_serial_mode() error: %s\n", strerror(errno));
}
/* set slave device ID */
modbus_set_slave(ctx, strtol(temp->sta_no, NULL, 10));
/* Debug mode */
modbus_set_debug(ctx, MODBUS_DEBUG);
if (modbus_connect(ctx) == -1) {
fprintf(stderr, "Connection failed: %s\n",
modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
/* Allocate and initialize the memory to store the registers */
/* nb_points = (UT_REGISTERS_NB > UT_INPUT_REGISTERS_NB) ?
UT_REGISTERS_NB : UT_INPUT_REGISTERS_NB;
tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
*/
//Get Data
rc = modbus_read_registers(ctx, strtol(temp->addr, NULL, 16), strtol("1", NULL, 10), regs);
if (rc < 0) {
fprintf(stderr, "%s\n", modbus_strerror(errno));
}
else {
printf("HOLDING REGISTERS:\n");
for (ii=0; ii < rc; ii++) {
sprintf(regs_8, "%d", regs[ii]);
printf("[%d]=%d\n", ii, regs[ii]);
}
/* Close the connection */
modbus_close(ctx);
modbus_free(ctx);
pthread_mutex_unlock(&beers_lock);
pthread_mutex_lock(&beers_lock);
int err, rows, cols;
err = opendb();
if(err)
{
/*Database is not open*/
return err;
}
sprintf(query4, "select * from Schedule_data where schedule_id=%s and station_no=%s and address= %s", temp->sch_id, temp->sta_no, temp->addr);
printf("%s\n",query4);
if( mysql_query(dp, query4))
{
fprintf(stderr, "%s\n", mysql_error(dp));
}
qp = mysql_store_result(dp);
if(qp != NULL){
/*所有執行結果都放在RAM中,由qp指著*/
rows = mysql_num_rows(qp);
cols = mysql_num_fields(qp);
printf("row=%d: col=%d\n",rows,cols);
while((row = mysql_fetch_row(qp)) != NULL)
mysql_free_result(qp);
}
//Write Data to Database
if(row==0){
sql_write(temp->sch_id,temp->sta_no, temp->addr, regs_8, q3);
}else
{
sql_write2(temp->sch_id, temp->sta_no, temp->addr, regs_8);
}
/*if( mysql_query(dp, query))
{
fprintf(stderr, "%s\n", mysql_error(dp));
}
qp = mysql_store_result(dp);
mysql_free_result(qp);*/
}
gettimeofday(&end, NULL);
timersub(&end, &start, &res);
pthread_mutex_unlock(&beers_lock);
usleep(atoi(temp->period)* 1000000 - (res.tv_sec * 1000000 + res.tv_usec));
}
else if(time_now > timestop) {
mysql_close(dp);
pthread_exit(0);
}
}
return NULL;
}
/*int SIZE(char *p)
{
int num = 0;
while(*p != '\0')
{
num++;
p++;
}
return num;
}
*/