0.6.3 Changes since last version
[digitaldcpower] / ddcp-script-getval.c
1 /* vim: set sw=8 ts=8 si et: */
2 /* 
3  * Linux software to communicate with the DDCP
4  * Written by Guido Socher 
5  */
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <termios.h>
12
13 int main(int argc, char *argv[])
14 {
15         char *device;
16         char c;
17         int c_cnt,state;
18         int fd;
19
20         if (argc != 2){
21                 printf("USAGE: ddcp-script-getval /dev/ttyUSB0\n");
22                 printf("or     ddcp-script-getval /dev/ttyUSB1\n");
23                 exit(0);
24         }
25         device=argv[1];
26
27         /* Set up io port correctly, and open it... */
28         fd = open(device, O_RDWR );
29         if (fd == -1) {
30                 fprintf(stderr, "ERROR: open for %s failed.\n",device);
31                 exit(1);
32         }
33         write(fd,"\r",1); // send empty line
34         usleep(100000); // commands are polled in the avr and it can take 100ms
35         write(fd,"\r",1); // send empty line
36         usleep(100000); // commands are polled in the avr and it can take 100ms
37         state=0;
38         while ((c_cnt=read(fd,&c,1))){
39                 //printf(":0x%x:\n",c); // debug
40                 if (c=='#') { // find begining of prompt
41                         state=1;
42                 }
43                 if (state<1) continue;
44                 if (c=='>') {
45                         putc(c,stdout);
46                         state=2;
47                 }
48                 if (state>1) break;
49                 putc(c,stdout);
50         }
51         printf("\n");
52         close(fd);
53         usleep(100000); // commands are polled in the avr and it can take 100ms
54         return(0);
55 }