To make a Virtual Tool by using VS IDE

  1 #include<iostream>
  2 #include <iomanip>
  3 #include <string>
  4 #include  <stdio.h>    
  5 #include  <time.h>    
  6 #include <windows.h>
  7 using namespace std;
  8 typedef unsigned long u32;
  9 typedef unsigned char u8;
 10 typedef unsigned int u16;
 11 u16 crc_16(u16 crc, u8 data);
 12 u16 database_task(char*s, int length);
 13 
 14 typedef union
 15 {
 16     u32 value;
 17     struct
 18     {
 19         u32 second : 6;
 20         u32 minute : 6;
 21         u32 hour : 5;
 22         u32 day : 5;
 23         u32 month : 4;
 24         u32 year : 6;
 25     }Bit;
 26 }LocationTimeStamp_e;
 27 
 28 LocationTimeStamp_e value;
 29 
 30 int main(void)
 31 {
 32     char s[512];
 33     char offset[512];
 34     int len, i, j = 2;
 35     int types=-1;
 36     u16 checksum;
 37     HANDLE hOut;
 38     hOut = GetStdHandle(STD_OUTPUT_HANDLE);
 39     SetConsoleTextAttribute(hOut, 8);
 40     while (types)
 41     {
 42         if (j>15)
 43             j = 2;
 44         switch (types)
 45         {
 46         case -1:
 47         {
 48             SetConsoleTextAttribute(hOut, 8);
 49             cout << "------------------------------------------------------------------------" << endl;
 50             cout << "1. Open SendRaw Encoder (Vitual device);" << endl;
 51             cout << "2. Open Timestamp Encoder;" << endl;
 52             cout << "3. Open Timestamp Decoder;" << endl;
 53             cout << "------------------------------------------------------------------------" << endl;
 54             cout << "-$:";
 55             cin >> s;
 56             len = strlen(s);
 57             if ((!strcmp(s, "exit")) || (!strcmp(s, "EXIT")))
 58             {
 59                 types = 0;
 60             }
 61             else if (!strcmp(s, "1"))
 62             {
 63                 SetConsoleTextAttribute(hOut, j++);
 64                 cout << "SendRaw Encoder Opened!" << endl;
 65                 cout << "========================================================================" << endl;
 66                 cout << " Copyright:   GALAXY .ltd in China" << endl;
 67                 cout << " Designed by: Milo lu " << endl;
 68                 cout << " Date:        May 09  2018" << endl;
 69                 cout << " Version:     05092018" << endl;
 70                 cout << "e.g. 'AckRequest' command:" << endl << "enter:40" << endl << "print: sendRaw outgoing 10100140a934" << endl;
 71                 cout << "=========================================================================" << endl << endl;;
 72                 types = 1;
 73             }
 74             else if (!strcmp(s, "2"))
 75             {
 76                 SetConsoleTextAttribute(hOut, j++);
 77                 cout << "Timestamp Encoder Opened!" << endl;
 78                 cout << "========================================================================" << endl;
 79                 cout << " Copyright:   GALAXY .ltd in China" << endl;
 80                 cout << " Designed by: Milo Lu" << endl;
 81                 cout << " Date:        March 27  2018" << endl;
 82                 cout << " Version:     05152018" << endl;
 83                 cout << "Instruction: type any word to get the timestamp." << endl;
 84                 cout << "========================================================================" << endl << endl;
 85                 types = 2;
 86             }
 87             else if (!strcmp(s, "3"))
 88             {
 89                 SetConsoleTextAttribute(hOut,j++);
 90                 cout << "Timestamp Decoder Opened." <<endl;
 91                 cout << "========================================================================" << endl;
 92                 cout << " Copyright:   GALAXY .ltd in China" << endl;
 93                 cout << " Designed by: Milo Lu" << endl;
 94                 cout << " Date:        March 27  2018" << endl;
 95                 cout << " Version:     05162018" << endl;
 96                 cout << "Instruction: type 4 bytes data." << endl;
 97                 cout << "========================================================================" << endl;
 98                 types = 3;
 99             }
100             break;
101         }
102         case 1:            // SendRaw Encoder
103         {
104             SetConsoleTextAttribute(hOut, j++);
105             cout << "-------------------------------------------" << endl;
106             cin >> s;
107             if ((!strcmp(s, "exit")) || (!strcmp(s, "EXIT")))
108             {
109                 types = -1;
110                 break;
111             }
112             len = strlen(s);
113             if (len % 2)        //if not to be a 8bits array, going to rebuild, fill '0' at first.
114             {
115                 len++;
116                 strncpy_s(offset, s, len - 1);
117                 s[0] = 0x30;
118                 for (i = 0; i < len; i++)
119                 {
120                     s[i + 1] = offset[i];
121                 }
122                 s[i] = 0;        //NULL end flag
123             }
124             checksum = database_task(s, len);
125             if (checksum == 0xffff)
126             {
127                 cout << "error." << endl;
128                 break;
129             }
130             cout << "sendRaw outgoing ";
131             cout << "1010";
132             cout.fill('0'); cout.width(2); cout << hex << len / 2;
133             cout.fill('0'); cout.width(len); cout << s;
134             cout.fill('0'); cout.width(4); cout << hex << checksum << endl;
135             break;
136         }
137         case 2:            // Timestamp Encoder
138         {
139             SetConsoleTextAttribute(hOut, j++);
140             cout << "-------------------------------------------" << endl;
141             cout << "-$:";
142             cin >> s;
143             if ((!strcmp(s, "exit")) || (!strcmp(s, "EXIT")))
144             {
145                 types = -1;
146                 break;
147             }
148             time_t t = time(NULL);
149             tm tp;
150             localtime_s(&tp, &t);
151             value.value = 0;
152             value.Bit.year = tp.tm_year + 1900 - 1990;
153 
154             value.Bit.month = tp.tm_mon + 1;
155             value.Bit.day = tp.tm_mday;
156             value.Bit.hour = tp.tm_hour;
157             value.Bit.minute = tp.tm_min;
158             value.Bit.second = tp.tm_sec;
159             cout << setiosflags(ios::uppercase) << hex << value.value << "    //";
160             cout << dec << tp.tm_year + 1900 << ".";
161             cout.fill('0'); cout.width(2); cout << dec << tp.tm_mon + 1 << ".";
162             cout.fill('0'); cout.width(2); cout << dec << tp.tm_mday << ". ";
163             cout.fill('0'); cout.width(2); cout << dec << tp.tm_hour << ":";
164             cout.fill('0'); cout.width(2); cout << dec << tp.tm_min << ":";
165             cout.fill('0'); cout.width(2); cout << dec << tp.tm_sec << endl;
166             break;
167         }
168         case 3:            // Timestamp Decoder
169         {
170             SetConsoleTextAttribute(hOut, j++);
171             cout << "-------------------------------------------" << endl;
172             cout << "-$:";
173             cin >> s;
174             if ((!strcmp(s, "exit")) || (!strcmp(s, "EXIT")))
175             {
176                 types = -1;
177                 break;
178             }
179             else if(strlen(s)==8)
180             {
181                 //2018-05-17 8:45:36
182                 memset(&value, 0, sizeof(value));
183                 for (int i = 0; i < 8; i++)
184                 {
185                     value.value <<= 4;
186                     if ((s[i]>='0')&&('9'>=s[i]))
187                     {
188                         value.value |= s[i] - '0';
189                     }
190                     else if ((s[i] >= 'a') && ('f' >= s[i]))
191                     {
192                         value.value |= (s[i] - 87);
193                     }
194                     else if ((s[i] >= 'A') && ('F' >= s[i]))
195                     {
196                         value.value |= (s[i] - 55);
197                     }
198                     else
199                     {
200                         value.value = 0;
201                         break;
202                     }
203                 }
204                 if (value.value==0)
205                 {
206                     cout << "bytes of timestamp error." << endl;
207                     break;
208                 }
209                 if (value.Bit.month>12)
210                 {
211                     cout << "month error." << endl; break;
212                 }
213                 if (value.Bit.day > 31)
214                 {
215                     cout << "day error." << endl; break;
216                 }
217                 if (value.Bit.hour>23)
218                 {
219                     cout << "hour error." << endl; break;
220                 }
221                 if (value.Bit.minute>59)
222                 {
223                     cout << "minute error." << endl; break;
224                 }
225                 if (value.Bit.second>59)
226                 {
227                     cout << "second error." << endl; break;
228                 }
229                 cout << value.Bit.year + 1990 << "-";
230                 cout.fill('0'); cout.width(2); cout << value.Bit.month << "-" ;
231                 cout.fill('0'); cout.width(2); cout << value.Bit.day << " ";
232                 cout.fill('0'); cout.width(2); cout << value.Bit.hour << ":" ;
233                 cout.fill('0'); cout.width(2); cout << value.Bit.minute << ":";
234                 cout.fill('0'); cout.width(2); cout << value.Bit.second << endl;
235             }
236             else
237             {
238                 cout << "number of bytes error." << endl;
239             }
240             break;
241         }
242         default:
243         {
244             cout << "error." << endl;
245             types = -1;
246             break;
247         }
248         }
249 
250     }
251     return 0;
252 }
253 const unsigned short crc_table[256] = {
254     0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
255     0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
256     0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
257     0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
258     0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
259     0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
260     0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
261     0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
262     0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
263     0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
264     0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
265     0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
266     0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
267     0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
268     0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
269     0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
270     0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
271     0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
272     0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
273     0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
274     0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
275     0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
276     0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
277     0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
278     0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
279     0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
280     0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
281     0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
282     0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
283     0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
284     0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
285     0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
286 };
287 
288 
289 /************************************************
290 function name: crc 16
291 using e.g.
292 crc = 0xffff;
293 for(i=0;i<bytes_length;i++)
294 {
295 crc = crc_16(crc,data_bytes_buf[i]);
296 }
297 return crc;
298 ***
299 parameter: crc, data
300 return: crc-16
301 ************************************************/
302 u16 crc_16(u16 crc, u8 data)
303 {
304     u8 da;
305 
306     da = (u8)(crc / 256);
307     crc <<= 8;              //crc=crc*256
308     crc ^= crc_table[da^data];
309     return(crc);
310 }
311 /////10100141b915
312 u16 database_task(char*s, int length)
313 {
314     u16 crc = 0xffff;
315     unsigned char hex[256] = { 0 };
316     for (int i = 0; i<length / 2; i++)
317     {
318         //high 4bits
319         if ((*s >= 'A') && ('F' >= *s))
320         {
321             hex[i] = *s++ - 0x37;
322             hex[i] <<= 4;
323         }
324         else if ((*s >= 'a') && ('f' >= *s))
325         {
326             hex[i] = *s++ - 0x57;
327             hex[i] <<= 4;
328         }
329         else if ((*s >= '0') && ('9' >= *s))
330         {
331             hex[i] = *s++ - 0x30;
332             hex[i] <<= 4;
333         }
334         else
335         {
336             cout << "ERROR" << endl;    //other char break;
337             crc = 0xffff;
338             break;
339         }
340         //low 4bits
341         if ((*s >= 'A') && ('F' >= *s))
342         {
343             hex[i] |= *s++ - 0x37;
344         }
345         else if ((*s >= 'a') && ('f' >= *s))
346         {
347             hex[i] |= *s++ - 0x57;
348         }
349         else if ((*s >= '0') && ('9' >= *s))
350         {
351             hex[i] |= *s++ - 0x30;
352         }
353         else
354         {
355             cout << "ERROR" << endl;
356             crc = 0xffff;
357             break;
358         }
359         crc = crc_16(crc, hex[i]);
360     }
361     return crc & 0xffff;    //because the u16 size is more than 16bits in PC system.
362 }
View Code

Thank you!

 

posted on 2018-05-17 09:37  Milo_lu  阅读(90)  评论(0编辑  收藏  举报

导航