source: fact/tools/ListenToArduino/ListenToArduino.cc@ 9882

Last change on this file since 9882 was 229, checked in by ogrimm, 15 years ago
Config requests non-blocking if not made from main thread, adapted all servers to GetConfig() returning std::string, workaround for erroneous SERVICE_LIST
File size: 4.2 KB
Line 
1#include <fcntl.h>
2#include <termios.h>
3#include <stdio.h>
4
5// For Evidence server
6#define SERVER_NAME "ARDUINO" // Name to use in DIM
7#include "Evidence.h"
8#define NUM_VAL 10
9
10#define MODEMDEVICE "/dev/myArduino"
11
12int main() {
13
14 int fd, res;
15 struct termios oldtio,newtio;
16 char Textbuffer[255], Temp[sizeof(Textbuffer)], *Token;
17
18 // Start Evidence server and create services
19 EvidenceServer Srv(SERVER_NAME);
20
21 float Val[NUM_VAL]={0};
22 DimService Data(SERVER_NAME"/Data", "F", Val, sizeof(Val));
23 DimService TextService(SERVER_NAME"/Textdata", Textbuffer);
24
25 // Open modem device for reading and writing and not as controlling tty
26 // because we don't want to get killed if linenoise sends CTRL-C.
27 fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
28 if (fd <0) {
29 Srv.Message(Srv.FATAL, "Could not open modem device (%s)", strerror(errno));
30 }
31
32 tcgetattr(fd,&oldtio); /* save current serial port settings */
33 memset(&newtio, 0, sizeof(newtio)); /* clear struct for new port settings */
34
35 /*
36 BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
37 CRTSCTS : output hardware flow control (only used if the cable has
38 all necessary lines. See sect. 7 of Serial-HOWTO)
39 CS8 : 8n1 (8bit,no parity,1 stopbit)
40 CLOCAL : local connection, no modem contol
41 CREAD : enable receiving characters
42 */
43 newtio.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
44
45 // CRTSCTS | maybe not needed //DN 090907
46
47 /*
48 IGNPAR : ignore bytes with parity errors
49 ICRNL : map CR to NL (otherwise a CR input on the other computer
50 will not terminate input)
51 otherwise make device raw (no other input processing)
52 */
53 newtio.c_iflag = IGNPAR ;
54
55 // | ICRNL // i guess this is not needed here. DN 090907
56
57 // Raw output.
58 newtio.c_oflag = 0;
59
60 /*
61 ICANON : enable canonical input
62 disable all echo functionality, and don't send signals to calling program
63 */
64 newtio.c_lflag = ICANON;
65
66 /*
67 initialize all control characters
68 default values can be found in /usr/include/termios.h, and are given
69 in the comments, but we don't need them here
70 */
71 newtio.c_cc[VINTR] = 0; /* Ctrl-c */
72 newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */
73 newtio.c_cc[VERASE] = 0; /* del */
74 newtio.c_cc[VKILL] = 0; /* @ */
75 newtio.c_cc[VEOF] = 4; /* Ctrl-d */
76 newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
77 newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */
78 newtio.c_cc[VSWTC] = 0; /* '\0' */
79 newtio.c_cc[VSTART] = 0; /* Ctrl-q */
80 newtio.c_cc[VSTOP] = 0; /* Ctrl-s */
81 newtio.c_cc[VSUSP] = 0; /* Ctrl-z */
82 newtio.c_cc[VEOL] = 0; /* '\0' */
83 newtio.c_cc[VREPRINT] = 0; /* Ctrl-r */
84 newtio.c_cc[VDISCARD] = 0; /* Ctrl-u */
85 newtio.c_cc[VWERASE] = 0; /* Ctrl-w */
86 newtio.c_cc[VLNEXT] = 0; /* Ctrl-v */
87 newtio.c_cc[VEOL2] = 0; /* '\0' */
88
89 // now clean the modem line and activate the settings for the port
90 tcflush(fd, TCIFLUSH);
91 tcsetattr(fd,TCSANOW,&newtio);
92
93 while (!Srv.ExitRequest) {
94
95 /* read blocks program execution until a line terminating character is
96 input, even if more than 255 chars are input. If the number
97 of characters read is smaller than the number of chars available,
98 subsequent reads will return the remaining chars. res will be set
99 to the actual number of characters actually read */
100 res = read(fd, Textbuffer, sizeof(Textbuffer));
101
102 // Set end of string marker anbd replace all non-printable characters
103 Textbuffer[res]=0;
104 for (unsigned int i=0; i<strlen(Textbuffer); i++) if (isprint(Textbuffer[i])==0) Textbuffer[i] = ' ';
105
106 printf("%s\n", Textbuffer);
107 TextService.updateService();
108 if (strncmp (Textbuffer,"[Temp,",6) != 0) continue;
109
110 // Update DIM services
111 memcpy(Temp, Textbuffer, sizeof(Textbuffer));
112 Token = strtok(Temp, ", ");
113 for (int i=0; i<NUM_VAL; i++) {
114 Token = strtok(NULL, ", ");
115 if (Token != NULL) Val[i] = atof(Token);
116 }
117 Data.updateService();
118
119 usleep(atoi(Srv.GetConfig("Period", "10").c_str()) * 1000000);
120 } // while()
121
122 /* restore the old port settings */
123 tcsetattr(fd,TCSANOW,&oldtio);
124}
Note: See TracBrowser for help on using the repository browser.