DT940程序

  1:  #include <stdio.h>    
  2:  #include <string.h>    
  3:  #include <stdlib.h>       
  4:  #include "itron.h"    
  5:  #include "cmndef.h"    
  6:  #include "bios1mac.h"    
  7:  #include  "COM_APLM.H"    
  8:  #include "clce800.h"    
  9:    
 10:  /* 定?义?的?功?能?键?值?*/  
 11:    
 12:  #define     BS_KEY      8    
 13:  #define     CLR_KEY     12    
 14:  #define     ENT_KEY     13    
 15:  #define     F1_KEY      45    
 16:  #define     F2_KEY      29    
 17:  #define     F3_KEY      28    
 18:  #define     F4_KEY      16    
 19:  #define     F5_KEY      32    
 20:  #define     F6_KEY      26    
 21:  #define     F7_KEY      27    
 22:   
 23:  #define     ENTERNEWLINE        "/x0d/x0a"       
 24:  #define     PASSFILE    "pass.txt"  
 25:    
 26:  /* 对?文?件?操?作?的?宏?定?义?*/    
 27:  #define     E_NOFILE        1   /* 没?有?这?个?文?件?*/    
 28:  #define     E_NOCOM         2   /* COM打?开?错?误?*/    
 29:  #define     E_NOOPEN        3   /* 文?件?打?开?错?误?*/    
 30:  #define     E_PWERR         4   /* 密?码?错?误?*/  
 31:    
 32:  /*  */    
 33:  #define     COM_OPEN_ERR    1   /* COM口?无?法?打?开?*/    
 34:  #define     FILE_SEND_ERR   2   /* 文?件?发?送?失?败?*/    
 35:  #define     FILE_REC_ERR    3   /* 文?件?接?收?失?败?*/      
 36:    
 37:  #define     E_NOFIND        1      
 38:   
 39:  H COM = COM0;    
 40:  DAT_PWR_STR pwr_str;        /* 这?个?定?义?为?全?局?变?量?*/    
 41:  CU_GRAPHSET tfGraphSet =    
 42:  {    
 43:      CU_GRAPH_ON_1,    
 44:      5,    
 45:      1,    
 46:      CU_GRAPH_NM_FILE,    
 47:      3    
 48:  };  
 49:   
 50:  /* 函?数?声?明?区?*/    
 51:  int CheckSN(void);    
 52:  void display(B *DspStrin, H x, H y);        /* 正?常?显?示?*/    
 53:  void display2(B *DspString, H x, H y);      /*  */    
 54:  void display3(B *DspString, H x, H y);      /* 粗?体?显?示?*/    
 55:  /*int getkey(void); */                      /* 获?得?一?个?键?*/    
 56:  int getstring(int X, int Y, int iMix, int iMax, char *string);    
 57:  int GetInt(int x, int y, int len, char *intstr, int type, int *iIsAdd);/*得?到?一?个?整?数?*/    
 58:  int GetDouble(int x, int y, int len, char *doustr, int type, int *IsAdd);/*得?到?一?个?实?数?*/    
 59:  void AddSpace(char *sStr, int iStrLen);    
 60:  void DecSpace(char *sStr, int iStrLen);    
 61:  void GetDate(char *sDate);   
 62:  /* 文?件?传?送?*/    
 63:  ER Com_open(void);    
 64:  ER SendFile(B *srcName, B *tarDir);    
 65:  ER RecvFile(B *srcName, B *tarDir);    
 66:   
 67:  /* 折?半?查?找?*/    
 68:  ER binary_search(int fname, W pos, int fieldsize, int keypos, int keylen, B *code, B *sdata, W *lno);    
 69:  /* 顺?序?查?找?*/    
 70:  ER order_search(int fname, W pos, int fieldsize, int keypos, int keylen, B *code, B *sdata, W* lno);  
 71:  void display(B *DspString, H x, H y)    
 72:  {    
 73:      lcd_csr_put(y, x);    
 74:      /* lcd_string( dsp_ank, dsp_mode, (UB *)data, LCD_LF_OFF ); */    
 75:      cputstr(LCD_ATTR_NORMAL, (UB*)DspString);    
 76:      /* cputstr800(dsp_mode,(UB *)data); */    
 77:  }    
 78:   
 79:  void display2(B *DspString, H x, H y)    
 80:  {    
 81:      lcd_csr_put(y, x);    
 82:      cputstr(LCD_ATTR_REVERS, (UB*)DspString);    
 83:  }    
 84:   
 85:  void display3(B *DspString, H x, H y)    
 86:  {    
 87:      lcd_csr_put(y, x);    
 88:      cputstr(LCD_ATTR_WIDTH, (UB*)DspString);    
 89:  }  
 90:   
 91:  /******************************************************************************  
 92:      函?数?说?明?:?  检?查?SN号?是?否?合?法? 
 93:      返?回?值?:? 
 94:          0       SN号?检?验?不?合?法? 
 95:          1       SN号?检?验?合?法? 
 96:  ******************************************************************************/    
 97:  int CheckSN()    
 98:  {    
 99:      if (!((dat_dealer_chk((UB*)"101105")==E_OK) || (dat_dealer_chk((UB*)"365331")==E_OK)))    
100:          return 1;    
101:      else    
102:          return 0;    
103:  }  
104:   
105:  int getstring(int X, int Y, int iMix, int iMax, char *string)    
106:  {    
107:      #ifndef INPUTALL    
108:      #define INPUTALL    0    
109:      return(read_str(X, Y, INPUTALL, string, iMax));    
110:      #endif    
111:  }  
112:   
113:  /******************************************************************************  
114:      函?数?说?明?:?  得?到?系?统?日?期? 
115:  ******************************************************************************/    
116:  void GetDate(char *sDate)    
117:  {    
118:      struct day_tabl My_day;    
119:      s_dateget(&My_day);    
120:      memset(sDate, 0x0, 11);     
121:      sprintf(sDate, "%04.04d-%02.02d-%02.02d", My_day.year, My_day.month, My_day.day);    
122:  }  
123:   
124:  /******************************************************************************  
125:      函?数?说?明?:   获?取?一?个?整?数? 
126:      变?   量?:  
127:      [x,y]       x,y的?坐?标?值? 
128:      [len]       获?取?的?数?值?长?度? 
129:      [doustr]    将?获?取?的?数?值?保?存?到?该?字?段? 
130:      [type]      设?置?正?、?负?号? 
131:          0   没?有?符?号?标?志? 
132:          1   有?符?号?标?志? 
133:      [iIsAdd]    返?回?选?择?的?正?、?负?号? 
134:          0   返?回?负?数? 
135:          1   返?回?正?数? 
136:  ******************************************************************************/    
137:  int GetInt(int x, int y, int len, char *intstr, int type, int *iIsAdd)    
138:  {    
139:      int  key, iIndex, ret;    
140:      char tempint[20];    
141:      iIndex = 0;    
142:      ret = 0;    
143:      memset(tempint, 0x0, 20);    
144:      strcpy(tempint, intstr);    
145:      *iIsAdd = 1;    
146:      if (type==1)    
147:          display("+", x, y);    
148:      if (type==1)    
149:          display(tempint, x+1, y);    
150:      else    
151:          display(tempint, x, y);  
152:   
153:      while(1)    
154:      {    
155:          key = getkey();    
156:          if (key==0x2D)    
157:          {    
158:              if (*iIsAdd==1)    
159:                  *iIsAdd = 0;    
160:              else    
161:                  *iIsAdd = 1;    
162:          }    
163:          if (type==1)    
164:          {    
165:              if (*iIsAdd==1)    
166:                  display("+", x, y);    
167:              else    
168:                  display("-", x, y);    
169:          }    
170:          if ((key==BS_KEY) && (iIndex>0))    
171:          {    
172:              tempint[iIndex] = 0x0;    
173:              iIndex--;    
174:              tempint[iIndex] = ' ';    
175:          }    
176:          if (key==CLR_KEY)    
177:          {    
178:              iIndex++;    
179:              tempint[0] = '0';    
180:              tempint[1] = 0x0;    
181:              ret = -1;    
182:              break;    
183:          }  
184:   
185:          if (key==ENT_KEY)    
186:          {    
187:              ret = 1;    
188:              break;    
189:          }    
190:          if ((key>=0x30) && (key<=0x39) && (iIndex<len))    
191:          {    
192:              tempint[iIndex] = key;    
193:              iIndex++;    
194:          }    
195:          if (type==1)    
196:              display(tempint, x+1, y);    
197:          else    
198:              display(tempint, x, y);    
199:      }    
200:      strcpy(intstr, tempint);    
201:      if (ret==1)    
202:          return(ENT_KEY);    
203:      else    
204:          return(CLR_KEY);  
205:   }  
206:   
207:  /******************************************************************************  
208:      函?数?说?明?:   获?取?一?个?实?数? 
209:      变?   量?:  
210:      [x,y]       x,y的?坐?标?值? 
211:      [len]       获?取?的?数?值?长?度? 
212:      [doustr]    将?获?取?的?数?值?保?存?到?该?字?段? 
213:      [type]      设?置?正?、?负?号? 
214:          0   没?有?符?号?标?志? 
215:          1   有?符?号?标?志? 
216:      [iIsAdd]    返?回?选?择?的?正?、?负?号? 
217:          0   返?回?负?数? 
218:          1   返?回?正?数? 
219:  ******************************************************************************/    
220:  int GetDouble(int x, int y, int len, char *doustr, int type, int *iIsAdd)    
221:  {    
222:      int  key, iIndex, ret, idian;    
223:      char tempdouble[20];    
224:      iIndex = 0;    
225:      ret = 0;  
226:      idian = 0;    
227:      memset(tempdouble, 0x0, 20);    
228:      strcpy(tempdouble, doustr);    
229:      *iIsAdd = 1;    
230:      if (type==1)     
231:          display("+", x, y);    
232:      if (type==1)    
233:          display(tempdouble, x+1, y);    
234:      else    
235:          display(tempdouble, x, y);    
236:      while(1)    
237:      {    
238:          key = getkey();    
239:          if (key==0x2D)  /* F1 */    
240:          {    
241:              if (*iIsAdd==1)    
242:                  *iIsAdd = 0;    
243:              else    
244:                  *iIsAdd = 1;    
245:          }    
246:          if (type==1)    
247:          {    
248:              if (*iIsAdd==1)    
249:                  display("+", x, y);    
250:              else    
251:                  display("-", x, y);    
252:          }    
253:          if ((key==BS_KEY) && (iIndex>0))    
254:          {    
255:              tempdouble[iIndex] = 0x0;    
256:              iIndex--;    
257:              if (tempdouble[iIndex]==46)    
258:                  idian = 0;    
259:              tempdouble[iIndex] = ' ';    
260:          }    
261:          if (key==CLR_KEY)    
262:          {    
263:              iIndex++;    
264:              tempdouble[0] = '0';    
265:              tempdouble[1] = 0x0;    
266:              ret = -1;    
267:              break;    
268:          }    
269:          if (key==ENT_KEY)    
270:          {    
271:              ret = 1;    
272:              break;    
273:          }    
274:          if ((key>=0x30) && (key<=0x39) && (iIndex<len))    
275:          {    
276:              tempdouble[iIndex] = key;    
277:              iIndex++;    
278:          }    
279:          if ((key==46) && (idian==0) && (iIndex<len))    
280:          {    
281:              tempdouble[iIndex] = key;    
282:              idian = 1;    
283:              iIndex++;    
284:          }    
285:          if (type==1)    
286:              display(tempdouble, x+1, y);    
287:          else    
288:              display(tempdouble, x, y);    
289:      }   /* end while */    
290:      strcpy(doustr, tempdouble);       
291:      if (ret==1)    
292:          return(ENT_KEY);    
293:      else    
294:          return(CLR_KEY);        
295:  }  
296:   
297:  /******************************************************************************  
298:      函?数?说?明?:?      用?空?格?填?充?字?符?串? 
299:      参?数?说?明?:? 
300:      sStr:?          需?要?添?加?空?格?的?字?符?串? 
301:      iStrLen:?       定?义?字?符?串?的?长?度? 
302:  ******************************************************************************/    
303:  void AddSpace(char *sStr, int iStrLen)    
304:  {  
305:        int i, j;    
306:      i = strlen(sStr);    
307:      if (i>iStrLen)    
308:      {    
309:          sStr[iStrLen] = 0x0;    
310:      }    
311:      else    
312:      {    
313:          for(j=i; j<iStrLen-1; j++)    
314:              sStr[j] = ' ';    
315:          sStr[j] = 0x0;    
316:      }    
317:  }  
318:  /******************************************************************************  
319:      函?数?说?明?:?      去?掉?字?符?串?尾?部?的?空?格? 
320:      参?数?说?明?:? 
321:      sStr:?          需?要?去?掉?空?格?的?字?符?串? 
322:      iStrLen:?       定?义?字?符?串?的?长?度? 
323:  ******************************************************************************/    
324:  void DecSpace(char *sStr, int iStrLen)    
325:  {    
326:      int i;    
327:      for (i=iStrLen-2; i>0; i--)    
328:      {    
329:          if (sStr[i] != ' ')    
330:          {    
331:              sStr[++i] = 0x0;    
332:              break;    
333:          }    
334:      }    
335:  }  
336:  /*********************************************************************/    
337:  /*                        文?件?传?输?模?块?                              */    
338:  /*********************************************************************/    
339:  ER Com_open()    
340:  {    
341:      ER ercd;    
342:      CU_RSPRM tfRsPrm;    
343:      cu_stopKeySet(CU_FNC_1);    
344:      ercd = Ir_SetWinMode(SET_WIN2K);/* 有?这?行?代?码?可?以?支?持?M的?传?输?:?DT980,?HA-60 IO */    
345:      ercd = cu_open(COM , CU_B115K, &tfRsPrm, CU_MODE_HT);    
346:      return ercd;    
347:  }  
348:   
349:  ER RecvFile(B *srcName, B *tarDir)    
350:  {    
351:      ER ercd;    
352:      ercd = Com_open();    
353:      if (ercd != E_OK)    
354:      {    
355:          cu_stopKeySet(CU_FNC_NON);    
356:          return ercd;    
357:      }  
358:      ercd = cu_fileRecv(COM, CU_TRANS_NORMAL, (B*)srcName, (B*)tarDir, CU_PROTECT_INVALID, &tfGraphSet);   
359:      cu_stopKeySet(CU_FNC_NON);    
360:      ercd = cu_close(COM, CU_CLOSE_NORMAL);    
361:      return ercd;    
362:  }    
363:   
364:  ER SendFile(B *srcName, B *tarDir)    
365:  {    
366:      ER ercd;    
367:      B SendFileName[16];    
368:      int i;   
369:      ercd = Com_open();    
370:      if (ercd != E_OK)     
371:      {    
372:          cu_stopKeySet(CU_FNC_NON);    
373:          return ercd;    
374:      }    
375:      memset(SendFileName, 0x0, sizeof(SendFileName)/sizeof(B));    
376:      if (strlen(srcName) < 15)    
377:      {    
378:          for (i=0; i<15; i++)    
379:          {    
380:              if (srcName[i] == '.')    
381:                  break;    
382:          }    
383:          strncpy(SendFileName, srcName, i);    
384:          strncat(SendFileName, "        ", (11-i));    
385:          strncat(SendFileName, &srcName[i], 4);    
386:      }    
387:      else    
388:      {    
389:          strcpy(SendFileName, srcName);    
390:      }    
391:      ercd = cu_fileSend(COM, CU_TRANS_NORMAL, (B*)SendFileName, (B*)tarDir, CU_PROTECT_INVALID, &tfGraphSet);  
392:      cu_stopKeySet(CU_FNC_NON);    
393:      ercd = cu_close(COM, CU_CLOSE_NORMAL);    
394:      return ercd;    
395:  }  
396:   
397:  int CheckPassword(char *password)    
398:  {    
399:      ER ret, fPASSFILE, i, iPassOk;    
400:      B sTemp[20], OldPass[20];    
401:      W lno;    
402:      if ((fPASSFILE = open(PASSFILE, O_RDONLY))== E_LOWERR)    
403:      {    
404:          return (E_NOOPEN); /* No Files */    
405:      }    
406:      memset(OldPass, 0x0, sizeof(OldPass));    
407:      read(fPASSFILE, OldPass, 6);    
408:      close(fPASSFILE);    
409:      if (strcmp(OldPass, password)==0)    
410:      {    
411:          return (0);    
412:      }    
413:      else    
414:      {    
415:          return (E_PWERR);    
416:      }    
417:  }  
418:   
419:  /******************************************************************************  
420:      函?数?说?明?:   折?半?查?找? 
421:      参?   数?:  
422:      [fname]     文?件?指?针? 
423:      [start_adr] 文?件?查?找?的?开?始?位?置? 
424:      [fieldsize] 一?行?文?本?的?长?度? 
425:      [keypos]    查?找?字?段?的?位?置? 
426:      [keylen]    查?找?字?段?的?长?度? 
427:      [code]      查?找?字?段?的?内?容? 
428:      [sdata]     返?回?查?到?到?的?字?段? 
429:      [fpos]      查?找?到?的?字?段?的?偏?移?量? 
430:  ******************************************************************************/  
431:    
432:  ER binary_search(int fname, W pos, int fieldsize, int keypos, int keylen, B *code, B *sdata, W *lno)    
433:  {  
434:      long filelength;    
435:      long head, trail;    
436:      long offset;    
437:      int i, j;    
438:      int Count=0, end=0;    
439:      head = pos;    
440:      filelength = lseek(fname, 0, 2);    
441:      /* 计?算?记?录?条?数?*/    
442:      trail = filelength / fieldsize - 1;    
443:      while(1)    
444:      {    
445:          /* 计?算?偏?移?量?*/    
446:          offset = ((head + trail) / 2) * fieldsize;    
447:          lseek(fname, offset, 0);    
448:          read(fname, sdata, fieldsize);    
449:          for (i=keypos, j=0; j<keylen; i++, j++)    
450:          {  
451:                if (sdata[i]==code[j])    
452:              {    
453:                  Count++;    
454:                  continue;  
455:                }    
456:              else if (sdata[i] > code[j])    
457:              {    
458:                  trail = (head + trail) / 2 - 1;    
459:                  if (head >= trail)    
460:                      end++;    
461:                  Count = 0;    
462:                  break;    
463:              }    
464:              else if (sdata[i] < code[j])    
465:              {    
466:                  head = (head + trail) / 2 + 1;    
467:                  if (head >= trail)    
468:                      end++;    
469:                  Count = 0;    
470:                  break;    
471:              }    
472:          }    
473:          if (end==2 || Count==keylen)    
474:              break;    
475:      }    
476:      if (Count == keylen)    
477:      {    
478:          *lno = offset;    
479:          return E_OK;    
480:      }    
481:      else    
482:      {    
483:          return E_NOFIND;  
484:      }  
485:  }  
486:   
487:  ER order_search(int fname, W pos, int fieldsize, int keypos, int keylen, B *code, B *sdata, W* lno)    
488:  {    
489:      ER err = 0;    
490:      W offset = pos;   
491:      err = lseek(fname, pos, 0);    
492:      if (err==E_LOWERR)    
493:          return E_PRM;   
494:      while(1)    
495:      {  
496:          err = read(fname, sdata, fieldsize);    
497:          if(err==E_LOWERR)  
498:              return E_NOFIND;  
499:          if(err<fieldsize)  
500:              return E_NOFIND;  
501:          if (strncmp(&sdata[keypos], code, keylen)==0)    
502:          {    
503:              *lno = offset;    
504:              return E_OK;    
505:          }    
506:          offset += fieldsize;    
507:      }    
508:  }  
posted @ 2012-02-21 10:34  小冰岛  阅读(747)  评论(0)    收藏  举报
        联系人 :梁小姐
       联系方式:18665009335
        邮箱 :busi@gzgolem.com