Browse Source

serial/ structuration du code

tth 2 years ago
parent
commit
dd359c9f82
4 changed files with 73 additions and 17 deletions
  1. 10
    0
      serial/essai.sh
  2. 7
    0
      serial/funcs.c
  3. 1
    0
      serial/serial.h
  4. 55
    17
      serial/t.c

+ 10
- 0
serial/essai.sh View File

@@ -0,0 +1,10 @@
1
+#!/bin/bash
2
+
3
+DEVICE="/dev/ttyACM0"
4
+
5
+for p in {0..15}
6
+do
7
+	echo pass $p
8
+	./t ${DEVICE} | tee -a foo.dat
9
+	sleep 8
10
+done

+ 7
- 0
serial/funcs.c View File

@@ -34,3 +34,10 @@ for (foo=0; foo<4; foo++) {
34 34
 return value;
35 35
 }
36 36
 /* ---------------------------------------------------------------- */
37
+int parse4values(char *line, char cflag, float array[4])
38
+{
39
+
40
+
41
+return -777;
42
+}
43
+/* ---------------------------------------------------------------- */

+ 1
- 0
serial/serial.h View File

@@ -17,5 +17,6 @@ int getline_to(int fd, char *where, int szm, int to_ms);
17 17
 /* auxiliary and test functions */
18 18
 
19 19
 int parseXvalue(char *asciidatas, char id);
20
+int parse4values(char *line, char cflag, float array[4]);
20 21
 
21 22
 

+ 55
- 17
serial/t.c View File

@@ -12,39 +12,77 @@
12 12
 #include  <fcntl.h>			//Used for UART
13 13
 #include  <errno.h>
14 14
 #include  <termios.h>			//Used for UART
15
+#include  <getopt.h>
15 16
 
16 17
 #include  "serial.h"
17 18
 
18 19
 int	verbosity;
19 20
 
20 21
 /* ---------------------------------------------------------------- */
21
-/* ---------------------------------------------------------------- */
22
-int main (int argc, char *argv[])
22
+int loop(int sfd, int iters)
23 23
 {
24
-int		serial_in, foo, count;
24
+int		count, foo;
25
+long		temps;
25 26
 char		ligne[200];
26 27
 
27
-if (2 != argc) {
28
-	fprintf(stderr, "give me a device name, please.\n");
29
-	return 2;
28
+for (count=0; count<iters; count++) {
29
+	foo = getline_to(sfd, ligne, 100, 0);
30
+	//
31
+	if (verbosity) {
32
+		fprintf(stderr, "getline #%d -> %d\n", count, foo);
33
+		fprintf(stderr, "%s\n", ligne);
34
+		}
35
+	foo = parseXvalue(ligne, 'X');
36
+	// 
37
+	if (foo >= 0) {
38
+		temps = time(NULL);
39
+		printf("%ld %d\n", temps, foo);
40
+		fflush(stdout);
41
+		}
42
+	else	{
43
+		fprintf(stderr, "%s: parse -> %d\n", __func__, foo);
44
+		}
30 45
 	}
46
+return 0;
47
+}
48
+/* ---------------------------------------------------------------- */
49
+int main (int argc, char *argv[])
50
+{
51
+int		serial_in;
52
+char		*device;
53
+int		nbre, speed, opt;
31 54
 
32
-serial_in = prepare_UART(argv[1], 9600);
55
+/* set some default values */
56
+verbosity	= 0;
57
+nbre		= 25;
58
+speed		= 9600;
59
+device		= "/dev/ttyACM0";
33 60
 
34
-fprintf(stderr, "going to listen on %d\n", serial_in);
61
+while ((opt = getopt(argc, argv, "d:n:v")) != -1) {
62
+	switch (opt) {
63
+		case 'v':	verbosity++;		break;
64
+		case 'n':	nbre = atoi(optarg);	break;
65
+		case 'd':	device = optarg;	break;
35 66
 
36
-for (count=0; count<100000; count++) {
37
-	foo = getline_to(serial_in, ligne, 100, 0);
38
-	fprintf(stderr, "getline #%d -> %d\n", count, foo);
39
-	fprintf(stderr, "%s\n", ligne);
40
-	foo = parseXvalue(ligne, 'X');
41
-	// fprintf(stderr, "parse -> %d\n", foo);
42
-	if (foo>= 0) {
43
-		printf("%d %d\n", count, foo);
44
-		fflush(stdout);
67
+		default:
68
+			fprintf(stderr, "%s : uh ?", argv[0]);
69
+			exit(1);
70
+			break;
45 71
 		}
72
+
46 73
 	}
47 74
 
75
+serial_in = prepare_UART(device, speed);
76
+if (serial_in < 0) {
77
+	fprintf(stderr, "%s : open device : error %d on %s\n",
78
+				argv[0], serial_in, device);
79
+	exit(1);
80
+	}
81
+
82
+fprintf(stderr, "going to listen on %d\n", serial_in);
83
+
84
+(void)loop(serial_in, nbre);
85
+
48 86
 return 0;
49 87
 }
50 88
 

Loading…
Cancel
Save