New Devices
[owSlave2.git] / stest / main.cpp
1 //============================================================================
2 // Name        : owtools.cpp
3 // Author      : 
4 // Version     :
5 // Copyright   : Your copyright notice
6 // Description : Hello World in C++, Ansi-style
7 //============================================================================
8
9 #include <iostream>
10 #include <queue>
11 using namespace std;
12 extern "C" {
13 #include <ownet.h>
14 }
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <stdint.h>
20
21
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26
27
28
29 unsigned char conf2(const char *s) {
30         unsigned char b = 0;
31         for (int i = 0; i < 2; i++) {
32                 b = b * 16;
33                 if ((s[i] >= '0') && (s[i] <= '9')) {
34                         b += s[i] - '0';
35                 } else
36                 if ((s[i] >= 'A') && (s[i] <= 'F')) {
37                         b += s[i] - 'A'+10;
38                 } else
39                 if ((s[i] >= 'a') && (s[i] <= 'f')) {
40                         b += s[i] - 'a'+10;
41                 }
42                 
43         }
44         return b;
45 }
46
47 void strcpys(char *s, const char* d,bool up) {
48         if (strlen(d) > 7990) {
49                 strncpy(s, d, 7990);
50                 s[200] = 0;
51         }
52         else strcpy(s, d);
53         if (up) {
54                 for (int i = 0; i<strlen(s); i++)
55                         if ((s[i] >= 'a') && (s[i] <= 'z'))
56                                 s[i] += 'A' - 'a';
57         }
58 }
59
60 int main(int argc, char *argv[]) {
61         uchar SNum[8];
62         int rslt, cnt, i;
63         uchar send_block[256];
64         uchar send_cnt = 0;
65         int portnum;
66         char adapter[20];
67         int found = 0;
68         int rw = 0;
69         int err = 0;
70         char s[8000];
71         int adt = 0;
72         int mod = 0;
73         if (argc >= 2) {
74                 strcpys(s, argv[1],true);
75                 
76                 
77                 if (strncmp(s,"USB",3)==0) {adt = 6;}
78                 if (strncmp(s, "COM", 3) == 0) { adt = 5; }
79                 if (adt != 0) {
80                         strcpy(s, argv[1] + 3);
81                         sprintf(adapter, "DS2490-%s\0",s);
82                         //sprintf(adapter, "{%s,%i}\0",s,adt);
83                         printf("Open Port %s\n", argv[1]);
84                         if ((portnum = owAcquireEx(adapter)) < 0) {
85                                 printf("ERROR \n", adt);
86                         }
87                         else mod = 1;
88
89                 } 
90                 if ((argc == 2) && (mod == 1)) mod = 2;
91                 if (argc > 2) {
92                         strcpys(s, argv[2],true);
93                         if (strncmp(s, "DC", 2) == 0) {
94                                 if (argc > 3) {
95                                         if (strlen(argv[3])>=16)
96                                         for (int i = 0; i < 16; i += 2) {
97                                                 SNum[7 - (i / 2)] = conf2(argv[3] + i);
98
99                                         }
100                                         printf("Select: %02X%02X%02X%02X%02X%02X%02X%02X\n", SNum[7], SNum[6], SNum[5], SNum[4], SNum[3], SNum[2], SNum[1], SNum[0]);
101                                         mod = 3;
102                                 }
103
104                         }
105                 }
106                 if ((mod == 3) && (argc>4)) {
107                         strcpys(s, argv[4], true);
108                         mod = 4;
109                 }
110         
111         }
112
113         if (mod == 2) {
114                 rslt = owFirst(portnum, TRUE, FALSE);
115                 cnt = 0;
116                 while (rslt) {
117                         cnt++;
118                         owSerialNum(portnum, &SNum[0], TRUE);
119                         printf("%02X%02X%02X%02X%02X%02X%02X%02X\n", SNum[7], SNum[6], SNum[5], SNum[4], SNum[3], SNum[2], SNum[1], SNum[0]);
120                         //                      fprintf(ft,"%02X%02X%02X%02X%02X%02X%02X%02X\n",SNum[7],SNum[6],SNum[5],SNum[4],SNum[3],SNum[2],SNum[1],SNum[0]);
121 /*                      switch (SNum[0]) {
122
123
124                                 case 0x14: {
125                                         //TestDS2430(portnum,&SNum[0]);
126                                         //scanf("%i",&i);
127                                         //ResetDS2430(portnum,&SNum[0]);
128                                         //uchar id[]={0x14,0xF5 ,0xEA ,0x24 ,0x06 ,0x00 ,0x00 ,0x5E};
129                                         //SetIDDS2430(portnum,&SNum[0],id);
130                                         if (rw) WriteDS2430(portnum, &SNum[0]); else ReadDS2430(portnum, &SNum[0]);
131                                         //WriteDS2430(portnum,&SNum[0],"5E00000624EAF514.bin");
132                                         break;
133                                 }
134                                 case 0x23: {
135                                         //TestDS2433(portnum,&SNum[0]);
136                                         //TestDS2433_reset(portnum,&SNum[0]);
137                                         //scanf("%i",&i);
138                                         if (rw) WriteDS2433(portnum, &SNum[0]); else ReadDS2433(portnum, &SNum[0]);
139                                         break;
140                                 }
141                                 }*/
142                         rslt = owNext(portnum, TRUE, FALSE);
143                 }
144         }
145         if (mod == 4) {
146                 owSerialNum(portnum, SNum, FALSE);
147                 strcat(s, "_R");
148                 int len = strlen(s);
149                 int i = 0;
150                 while (i < len) {
151                         if (s[i] == '_') {
152                                 if (s[i + 1] == 'R') {
153                                         if (owAccess(portnum)) {
154                                                 if (owBlock(portnum, FALSE, send_block, send_cnt)) {
155                                                         for (int j = 0; j < send_cnt; j++) {
156                                                                 printf("%02X", send_block[j]);
157                                                         }
158                                                         printf("\n");
159                                                 }
160                                                 else printf("ERROR RESET\n");
161                                         }
162                                         else printf("ERROR SEND\n");
163                                         send_cnt = 0;
164                                 }
165                                 if (s[i + 1] == 'P') {
166                                         if (i + 2 < len) {
167                                                 i += 2;
168                                                 msDelay(conf2(s + i));
169                                                 
170                                         }
171                                 }
172                         }
173                         else {
174                                 send_block[send_cnt++] = conf2(s + i);
175                         }
176                         i += 2;
177                 
178                         
179                 }
180         }
181         if (mod>0) owRelease(portnum);
182         
183         return 0;
184 }