-
Notifications
You must be signed in to change notification settings - Fork 0
/
TERMINAL.C
6479 lines (5452 loc) · 191 KB
/
TERMINAL.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
// ³ ASCII a HEX Terminal pro seriovou komunikaci ³
// ³ (c) 2001/2002 Tomas Hujer ³
// ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
#define NazevProgramu "COM Terminal"
#define VerzeProgramu "2.53b"
#define Creator "Tomas Hujer"
#define PosledniKompilace "22. 02. 2002"
#define ConfigFileHeader "THS Terminal Config File "
#define Copyright "Copyright (c) 2002"
#include <STDIO.H>
#include <ALLOC.H>
#include <CONIO.H>
#include <DOS.H>
#include <IO.H>
#include <STRING.H>
#include <FCNTL.H>
#include <STDLIB.H>
#define ScrBuffers 6 // Pocet bufferu pro obrazove operace
struct
{
unsigned int Y;
unsigned char *Item;
} MenuItems[20];
struct
{
// Hlavicka konfiguracniho souboru
unsigned char Header[25];
unsigned char Ver[5];
// Hlavni konfigurace
unsigned char PocBit;
unsigned char StpBit;
unsigned char Parita;
unsigned char Port;
unsigned int AdresaPortu;
unsigned char Speed;
unsigned char DisplayChars[12];
unsigned char BitMask;
int Delay_Ascii;
int Delay_HEX;
unsigned int CustomPortAdress;
unsigned char ExitDialog;
unsigned char SetFixAdress;
// Konfigurace pro protokol Distel
unsigned int DistelSendByteDelay;
unsigned int DistelSendMsgDelay;
unsigned long DistelMaxAutoSend;
unsigned int DistelTimeoutMsgDetect;
unsigned char DistelMaxRecMsgs;
unsigned char DistelBeepAfterRecMsg;
unsigned char DistelBeepAfterSendMsg;
unsigned char DistelBeepTimeout;
unsigned char DistelBeepType;
unsigned char DistelAdrMin;
unsigned char DistelAdrMax;
unsigned char DistelMSGDetectionType;
unsigned char DistelMSGSendMode;
unsigned char DistelBytesForDetect;
unsigned char DistelMaxMSGLength;
unsigned char DistelMinMSGLength;
unsigned char DistelCmdMin;
unsigned char DistelCmdMax;
unsigned char DistelAktualizeStatIfRec;
// Ukladani bufferu
unsigned char CustomASCIIBuffer[12][61]; // [Buffer][Znak]
unsigned char CustomHEXBuffer[12][61]; // [Buffer][Znak]
unsigned char CustomDistelBuffer[12][100]; // [Buffer][Znak]
unsigned char DistelBufferPopis[12][20]; // [Buffer][Znak]
unsigned char DistelMsgNumber[12]; // [Buffer][Znak]
// Nastaveni pro Terminal 2xCOM
unsigned char T2C_PORT1; // Port1 pro terminal 2xCOM
unsigned char T2C_PORT2; // Port2 pro terminal 2xCOM
unsigned char T2C_ENABLE_SEND; // Povoleni odesilani znaku pro 2xCOM port1
unsigned char T2C_TYPP1; // Typ vypisu portu1 (ASCII/HEX)
unsigned char T2C_TYPP2; // Typ vypisu portu2 (ASCII/HEX)
// Ostatni
unsigned char Pol; // Pozice v hlavnim menu
unsigned char ActivatePol; // bit 7 ... aktivace polozky
unsigned char AutoOpen; // 1=Povoleni automaticke aktivace
} Config;
// Vyber komunikacni rychlosti portu
unsigned long PortSpeed[12]={ 110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200,
38400, 57600, 115200 };
unsigned int I,TimeOut;
unsigned char Byte;
// Povoleni ukonceni programu
unsigned char Exit;
// Ukladani pozice kurzoru
unsigned char Cursor_X_0, Cursor_Y_0;
unsigned char Cursor_X_1, Cursor_Y_1;
unsigned char Cursor_X_2, Cursor_Y_2;
unsigned char Cursor_X_3, Cursor_Y_3;
unsigned char Cursor_X_4, Cursor_Y_4;
// Promenne pro zjisteni stisknute klavesy
unsigned int Ascii,Scan;
// Pozice v jednotlivych menu
unsigned char UPol, SPol, SNPol, NPol, DCPol, STPPol, DistelPol, DSPol,
DBLPol, BMPol, MPol, NT2CPol;
// Obrazovkove buffery
unsigned char far *ScrBuf[ScrBuffers];
// Promenne pro konverzi mezi HEX a DEC
unsigned char HEX[3];
unsigned char HEXWORD[4];
// Buffer pro radkovy vstup z klavesnice
unsigned char Vstup[256];
// Parametry spustitelneho souboru
unsigned char *Param;
// Rukojet oteviranych souboru
int handle;
// Promenna pro kontrolu CRC EXE souboru (terminal.exe)
unsigned long ActCRCtest;
unsigned long CRCtest;
// Zjisteni aktualniho stavu portu (chyba parity, spatny stopbit...)
unsigned char PortStatus;
unsigned char Text[300];
// Prepinani mezi editaci a obnovenim vypisu hex. pole
unsigned char Input_TAB_ENABLED;
// Stav zobrazeni napovedy klavesovych zkratek
unsigned char _KeybHelpStatus;
// Povoleni puvodni hodnoty radkoveho vstupu pri stisku ESC
unsigned char _CancelModeEnabled;
// Povoleni automatickeho odesilani znaku (Protokol Distel)
unsigned char _DistelAutoSend;
// Docasne pozastaveni prijimani znaku
unsigned char _PauseReceive;
// Promenna pro automaticke otevreni okna po spusteni
unsigned char AutoOpenStatus;
//------------------------------------------------------------------------
// Zjisti stisknutou klavesu, pokud neni klavesa stisknuta, ceka.
//------------------------------------------------------------------------
void GetKey()
{
unsigned char S,A;
asm{
MOV AH,0x10
INT 0x16
MOV A,AL
MOV S,AH
}
Ascii=A;
Scan=S;
}
//------------------------------------------------------------------------
// Filtruje znaky podle aktualni konfigurace
//------------------------------------------------------------------------
unsigned char FilterChar(unsigned char Ch)
{
unsigned char _Ch;
_Ch=Ch;
switch(_Ch)
{
case 0x0D: if(Config.DisplayChars[1]) _Ch=0; break;
case 0x0A: if(Config.DisplayChars[2]) _Ch=0; break;
case 0x07: if(Config.DisplayChars[3]) _Ch=0; break;
case 0x08: if(Config.DisplayChars[4]) _Ch=0; break;
case 0x09: if(Config.DisplayChars[5]) _Ch=0; break;
}
return(_Ch);
}
//------------------------------------------------------------------------
// Prevede maly znak na velky
//------------------------------------------------------------------------
unsigned char UpChr(unsigned char Ch)
{
if((Ch>=65) & (Ch<=122)) return(Ch & (0xFF-0x20));
else return(Ch);
}
//------------------------------------------------------------------------
// Zjisti z tabulky BIOSu adresu serioveho portu
// Pokud je vracena hodnota 0, port neni pritomen nebo chyba portu
//------------------------------------------------------------------------
unsigned int AdrPort(char P)
{
if(!Config.SetFixAdress)
{
switch(P)
{
case 0: return(peek(0,0x0400)); // default 0x3F8;
case 1: return(peek(0,0x0402)); // default 0x2F8;
case 2: return(peek(0,0x0404)); // default 0x3E8;
case 3: return(peek(0,0x0406)); // default 0x2E8;
case 4: return(Config.CustomPortAdress);
default: return(0);
}
}
else // Pokud toto zjisteni adresy portu nepracuje spravne
{ // dosadi se adresy portu napevno:
switch(P)
{
case 0: return(0x3F8);
case 1: return(0x2F8);
case 2: return(0x3E8);
case 3: return(0x2E8);
case 4: return(Config.CustomPortAdress);
default: return(0);
}
}
}
//-----------------------------------------------------------------------
// Ulozi do bufferu Buf vyrez obrazovky dany rozmery x0, y0, x1, y1
//------------------------------------------------------------------------
void GetScr(unsigned char Buf,char x0,char y0,char x1,char y1)
{
unsigned char x,y;
if(ScrBuf[Buf]!=NULL)
{
for (y=y0;y<=y1;y++)
for (x=x0;x<=x1;x++)
{ ScrBuf[Buf][x*2 + y*80*2]=peekb(0xB800,x*2 + y*80*2);
ScrBuf[Buf][x*2 + y*80*2 + 1]=peekb(0xB800,x*2 + y*80*2 + 1); }
}
else
{
gotoxy(1,1); textattr(15);
cprintf("\nChyba: Nedostatek pameti pro GetScr! Buffer: %2x",Buf);
}
}
//-----------------------------------------------------------------------
// Obnovi z bufferu Buf vyrez obrazovky dany rozmery x0, y0, x1, y1
//------------------------------------------------------------------------
void SetScr(unsigned char Buf,char x0,char y0,char x1,char y1)
{
unsigned char x,y;
if(ScrBuf[Buf]!=NULL)
{
for (y=y0;y<=y1;y++)
for (x=x0;x<=x1;x++)
{ pokeb(0xB800,x*2+y*80*2,ScrBuf[Buf][x*2 + y*80*2]);
pokeb(0xB800,x*2+y*80*2 + 1,ScrBuf[Buf][x*2 + y*80*2 + 1]); }
}
else
{
gotoxy(1,1); textattr(15);
cprintf("\nChyba: Nedostatek pameti pro SetScr! Buffer: %2x",Buf);
}
}
//-----------------------------------------------------------------------
// Vykresli na obrazovku ramecek s rozmery x0, y0, x1, y1
// s attributy Attr
// Pokud je Shadow = True je vykreslovan stin ramecku
//------------------------------------------------------------------------
void Frame(char x0,char y0,char x1,char y1,char Attr,char Shadow)
{
unsigned char x,y;
textattr(Attr);
for(x=x0;x<x1;x++)
for(y=y0;y<(y1-1);y++)
{
gotoxy(x+1,y+1);putch(' ');
}
gotoxy(x0,y0);putch('Ú');
for(x=0;x<(x1-x0);x++) putch('Ä');
putch('¿');
for(y=0;y<(y1-y0);y++)
{
textattr(Attr);
gotoxy(x0,y0+y+1);putch('³');
gotoxy(x1+1,y0+y+1);putch('³');
}
textattr(Attr);
gotoxy(x0,y1);putch('À');
for(x=0;x<(x1-x0);x++) putch('Ä');
putch('Ù');
if(Shadow)
{
for(y=0;y<(y1-y0+1);y++)
{
textcolor(7);textbackground(0);
gotoxy(x1+2,y0+y+1);cprintf("°°");
}
textcolor(7);textbackground(0);
gotoxy(x0+2,y1+1);
for(x=0;x<(x1-x0);x++) putch('°');
}
}
//------------------------------------------------------------------------
// Zapise do promennych Cursor_X[B] a Cursor_Y[B] aktualni pozici kurzoru
//------------------------------------------------------------------------
void SaveCursorPos(unsigned char B)
{
unsigned char _x,_y;
asm{
MOV AH,3
MOV BH,0
INT 0x10
MOV _x,DL
MOV _y,DH
}
switch(B)
{
case 0: Cursor_X_0=_x; Cursor_Y_0=_y; break;
case 1: Cursor_X_1=_x; Cursor_Y_1=_y; break;
case 2: Cursor_X_2=_x; Cursor_Y_2=_y; break;
case 3: Cursor_X_3=_x; Cursor_Y_3=_y; break;
case 4: Cursor_X_4=_x; Cursor_Y_4=_y; break;
default: printf("Chybne cislo bufferu pro ulozeni pozice kurzoru !");
}
}
//------------------------------------------------------------------------
// Nastavi kurzor na pozici zapsanou v promennych Cursor_X_? a Cursor_Y_?
//------------------------------------------------------------------------
void LoadCursorPos(unsigned char B)
{
unsigned char _x,_y;
switch(B)
{
case 0: _x=Cursor_X_0; _y=Cursor_Y_0; break;
case 1: _x=Cursor_X_1; _y=Cursor_Y_1; break;
case 2: _x=Cursor_X_2; _y=Cursor_Y_2; break;
case 3: _x=Cursor_X_3; _y=Cursor_Y_3; break;
case 4: _x=Cursor_X_4; _y=Cursor_Y_4; break;
default: printf("Chybne cislo bufferu pro obnoveni pozice kurzoru !");
}
asm{
MOV AH,2
MOV BH,0
MOV DL,_x
MOV DH,_y
INT 0x10
}
}
//------------------------------------------------------------------------
// Roluje okno _X0, _Y0, _X1, _Y1 o jeden radek nahoru
// pradne misto ma atribut dany BH
//------------------------------------------------------------------------
void RolWindowUP(unsigned char _X0,unsigned char _Y0,unsigned char _X1,unsigned char _Y1)
{
asm{
MOV AH,6
MOV CH,_Y0
MOV CL,_X0
MOV DH,_Y1
MOV DL,_X1
MOV AL,1
MOV BH,15
INT 0x10
}
}
//-----------------------------------------------------------------------
// Nastavi neviditelny textovy kurzor
//------------------------------------------------------------------------
void HideTextCursor()
{
asm{
MOV AH,1
MOV CH,20h
MOV CL,20h
INT 10h }
}
//-----------------------------------------------------------------------
// Nastavi viditelny textovy kurzor
//------------------------------------------------------------------------
void ShowTextCursor()
{
asm{
MOV AH,1
MOV CH,15
MOV CL,16
INT 10h }
}
//------------------------------------------------------------------------
// Zobrazuje malou napovedu ( na radku 24 )
//------------------------------------------------------------------------
void ShowHint(unsigned char *Hint)
{
textattr(10+16);
window(1,24,80,24); clrscr(); window(1,1,80,25);
gotoxy(1,24);
if(Hint[0]!=0) cprintf(" %s",Hint);
}
//------------------------------------------------------------------------
// Zobrazi text T uprostred radku Y
//------------------------------------------------------------------------
void CenterText(unsigned char Y, unsigned char *T)
{
unsigned char X;
X=80/2 - strlen(T)/2;
gotoxy(X,Y);
printf(T);
}
//------------------------------------------------------------------------
// Informace o programu
//------------------------------------------------------------------------
void About()
{
HideTextCursor();
Frame(25,9,55,17,64+32+16,0);
CenterText(9," About ");
CenterText(11,NazevProgramu" v"VerzeProgramu);
CenterText(13,Copyright);
CenterText(15,Creator);
GetKey();
}
//------------------------------------------------------------------------
// Ukonceni programu a vypis informaci o programu
//------------------------------------------------------------------------
void ExitAbout(unsigned char ExitCode)
{
textattr(7);
clrscr();
CenterText(2,NazevProgramu" v"VerzeProgramu);
CenterText(3,Copyright" "Creator"\n\n");
printf("Posledni upgrade: %s\n\n\r",PosledniKompilace);
exit(ExitCode);
}
void SetDistelDefaultConfig()
{
Config.DistelSendByteDelay=0;
Config.DistelSendMsgDelay=0;
Config.DistelMaxAutoSend=0;
Config.DistelMaxRecMsgs=13;
Config.DistelTimeoutMsgDetect=50;
Config.DistelBeepAfterRecMsg=0;
Config.DistelBeepAfterSendMsg=0;
Config.DistelBeepTimeout=0;
Config.DistelBeepType=1;
Config.DistelAdrMin=0;
Config.DistelAdrMax=255;
Config.DistelMSGDetectionType=3;
Config.DistelMSGSendMode=0;
Config.DistelBytesForDetect=50;
Config.DistelMaxMSGLength=255;
Config.DistelMinMSGLength=0;
Config.DistelCmdMin=0;
Config.DistelCmdMax=255;
Config.DistelAktualizeStatIfRec=0;
Config.PocBit=8; // Pocet prenasenych bitu
Config.StpBit=0; // Pocet stopbitu
Config.Parita=0; // None
Config.Port=0; // COM1
Config.AdresaPortu=0x3F8; // Adresa portu
Config.Speed=8; // 19200 Bd
Config.DisplayChars[0]=0; // Povoleni prichozim znakum zmenu kurzoru
Config.DisplayChars[1]=0; // 0x0D CR
Config.DisplayChars[2]=0; // 0x0A LF
Config.DisplayChars[3]=0; // 0x07 BELL
Config.DisplayChars[4]=0; // 0x08 BCK
Config.DisplayChars[5]=0; // 0x09 TAB
Config.BitMask=255; // Bitova maska (Zobrazovane bity)
Config.SetFixAdress=0; // Nastaveni default adres portu
// Ukoncovaci dialog ( pri stisku Alt+X )
Config.ExitDialog=1;
// -------- Zpozdeni ----------
Config.Delay_Ascii=1; // Pro vysilani ASCII bufferu
Config.Delay_HEX=1; // Pro vysilani HEX bufferu
// Nastaveni terminalu 2xCOM
Config.T2C_PORT1=0; // Port1 pro terminal 2xCOM
Config.T2C_PORT2=1; // Port2 pro terminal 2xCOM
Config.T2C_ENABLE_SEND=0; // Povoleni odesilani znaku pro 2xCOM port1
Config.T2C_TYPP1=0; // Typ vypisu portu1 (ASCII/HEX)
Config.T2C_TYPP2=0; // Typ vypisu portu2 (ASCII/HEX)
// Nastaveni automaticke aktivace polozky
Config.Pol=0;
Config.ActivatePol=0;
Config.AutoOpen=0;
}
//------------------------------------------------------------------------
// Nacteni konfigurace ze souboru
// Pokud soubor neexistuje, pokusi se ho zalozit, jestize neuspeje,
// DOS vyhlasi chybu
// Uklada se cela struktura Config, lze tedy jednoduse pridat dalsi
// promenne k ukladani do souboru
//------------------------------------------------------------------------
void ReadConfigFile()
{
unsigned Attr;
if ((handle = _open("TERMINAL.CFG", O_RDONLY )) == -1)
{
handle = _creat("TERMINAL.CFG", 0);
for(I=0; I<12; I++) Config.DistelMsgNumber[I]=0;
SetDistelDefaultConfig();
clrscr();
About();
}
else
{
printf("Nacita se konfigurace .... ");
lseek(handle, 0L, SEEK_SET);
if(_read(handle, &Config, sizeof(Config))==-1)
{
printf("Chyba pri cteni souboru s konfiguraci !");
while(1)
{
GetKey();
if(Scan==1) ExitAbout(0xFF);
if((Ascii==13) | (Scan==57)) return;
}
}
else printf("Ok\n");
_dos_getfileattr("TERMINAL.CFG",&Attr);
if(Attr & 1==1) About();
if((Config.Ver[0]!=VerzeProgramu[0]) | (Config.Ver[1]!=VerzeProgramu[1])|
(Config.Ver[2]!=VerzeProgramu[2]) | (Config.Ver[3]!=VerzeProgramu[3])|
(Config.Ver[4]!=VerzeProgramu[4]))
{
printf("\n\n\n\rUpozorneni: Konfiguracni soubor byl ulozen jinou verzi programu !\n\r");
printf("Nastaveni programu nemusi byt korektni... ");
GetKey();
printf("\n\n\r");
}
}
_close(handle);
if((Config.PocBit>8) | (Config.PocBit<6)) Config.PocBit=8;
if(Config.StpBit>1) Config.StpBit=0;
if(Config.Parita>4) Config.Parita=0;
if(Config.Port>4) Config.Port=0;
if(Config.Speed>12) Config.Speed=12;
for(I=0; I<12; I++)
{
if(Config.DistelMsgNumber[I] > 12) Config.DistelMsgNumber[I]=0;
}
if((Config.Pol > 6) & (Config.Pol != 10)) Config.Pol=0;
}
//------------------------------------------------------------------------
// Zapis konfigurace do souboru
//------------------------------------------------------------------------
void WriteConfigFile()
{
if ((handle = _open("TERMINAL.CFG", O_WRONLY)) == -1)
{
SaveCursorPos(3);
HideTextCursor();
window(1,1,80,25);
ShowHint("Nelze vytvorit soubor TERMINAL.CFG pro zapis konfigurace !");
GetScr(2,17,11,67,14);
Frame(18,12,65,14,64+15,0);
gotoxy(38,12); cprintf(" Chyba ");
gotoxy(22,13); cprintf("Nelze zapsat konfiguraci do souboru !",Config.AdresaPortu);
GetKey();
SetScr(2,17,11,67,14);
ShowTextCursor();
LoadCursorPos(3);
textattr(15);
}
else
{
memcpy(Config.Header, ConfigFileHeader, 30);
Config.Ver[0]=VerzeProgramu[0];
Config.Ver[1]=VerzeProgramu[1];
Config.Ver[2]=VerzeProgramu[2];
Config.Ver[3]=VerzeProgramu[3];
Config.Ver[4]=VerzeProgramu[4];
lseek(handle, 0L, SEEK_SET);
_write(handle, &Config, sizeof(Config));
}
_close(handle);
}
//------------------------------------------------------------------------
// Prerusitelne zpozdeni
//------------------------------------------------------------------------
unsigned char Delay(unsigned int D)
{
unsigned int I;
for(I=0; I<D; I++)
{
delay(1);
if(inp(0x60)==1)
{
getch();
return(1);
}
}
return(0);
}
//------------------------------------------------------------------------
// Nastavi prenosovou rychlost aktualniho portu
//------------------------------------------------------------------------
void NastavRychlostPortu(unsigned long Baud)
{
unsigned char D0,D1;
D0=(unsigned char) ((115200 / Baud) & 0xFF); // Dolni byte
D1=(unsigned char) (((115200 / Baud) >> 8) & 0xFF); // Horni byte
outp(Config.AdresaPortu+3,128); // Nastavi Zapis do reg. Adr+1
outp(Config.AdresaPortu ,D0); // Zapis dolniho byte delitele ryhlosti
outp(Config.AdresaPortu+1,D1); // Zapis horniho byte delitele ryhlosti
outp(Config.AdresaPortu+3,3);
};
//------------------------------------------------------------------------
// Inicializuje aktualni port
// Pokud port nelze inicializovat, vyhlasi chybu
//------------------------------------------------------------------------
void InitPort()
{
unsigned char PortSet;
if(Config.AdresaPortu==0)
{
GetScr(2,17,11,67,14);
Frame(18,12,67,14,64+15,0);
gotoxy(38,12); cprintf(" Chyba ");
gotoxy(20,13); cprintf("Port COM%x na adrese %.4Xh nelze inicializovat !",Config.Port+1,Config.AdresaPortu);
while(1)
{
GetKey(); if((Scan==1) | (Scan==28)) break;
}
SetScr(2,17,11,67,14);
}
else
{
NastavRychlostPortu(PortSpeed[Config.Speed]);
PortSet=0;
switch(Config.PocBit)
{
case 5: break; // 5 bitu
case 6: PortSet|=1; break; // 6 bitu
case 7: PortSet|=2; break; // 7 bitu
case 8: PortSet|=3; break; // 8 bitu
default: break;
}
if(Config.StpBit==1) PortSet+=4;
//cprintf("XXX%X",Config.Parita);
switch(Config.Parita)
{
case 0: break; //Neni
case 1: PortSet|=8+16; break; //Suda
case 2: PortSet|=8+16; break; //Licha
case 3: PortSet|=8+32; break; //Vzdy 0
case 4: PortSet|=8+16+32; break; //Vzdy 1
default: break;
}
outp(Config.AdresaPortu+3,PortSet);
outp(Config.AdresaPortu+1,0); // Vypnuti preruseni
outp(Config.AdresaPortu+4,1); // Aktivace DTR
}
}
//------------------------------------------------------------------------
// Vraci 1 pokud je port pripraven vyslat dalsi znak
//------------------------------------------------------------------------
unsigned char SendReady() // 0 = Vysilac je prazdny a pripraven vysilat
{
if((inp(Config.AdresaPortu+5)&(32+64))==(32+64)) return(1);
else return(0);
}
//------------------------------------------------------------------------
// Vraci 1 pokud port prijal dalsi znak
//------------------------------------------------------------------------
unsigned char CharReady() // 1 = Znak byl prijat
{
if((inp(Config.AdresaPortu+5) & 1)==1) return(1);
else return(0);
}
//------------------------------------------------------------------------
// Vraci prijaty znak, pokud je prijimac prazdny ceka na dalsi znak
//------------------------------------------------------------------------
unsigned char PrijatyByte()
{
while(1)
{
if(CharReady()) return(inp(Config.AdresaPortu));
if(inp(0x60)==1) return(0xFF);
}
}
//-----------------------------------------------------------------------
//Prevede HEX. cislo z ASCII formy do byte
//------------------------------------------------------------------------
unsigned char CharToHex()
{
unsigned char B;
B=0;
switch(HEX[1])
{
case '0': B=0; break;
case '1': B=1; break;
case '2': B=2; break;
case '3': B=3; break;
case '4': B=4; break;
case '5': B=5; break;
case '6': B=6; break;
case '7': B=7; break;
case '8': B=8; break;
case '9': B=9; break;
case 'A': B=10; break;
case 'B': B=11; break;
case 'C': B=12; break;
case 'D': B=13; break;
case 'E': B=14; break;
case 'F': B=15; break;
}
switch(HEX[0])
{
case '0': B=B+0x00; break;
case '1': B=B+0x10; break;
case '2': B=B+0x20; break;
case '3': B=B+0x30; break;
case '4': B=B+0x40; break;
case '5': B=B+0x50; break;
case '6': B=B+0x60; break;
case '7': B=B+0x70; break;
case '8': B=B+0x80; break;
case '9': B=B+0x90; break;
case 'A': B=B+0xA0; break;
case 'B': B=B+0xB0; break;
case 'C': B=B+0xC0; break;
case 'D': B=B+0xD0; break;
case 'E': B=B+0xE0; break;
case 'F': B=B+0xF0; break;
}
return(B);
}
//-----------------------------------------------------------------------
//Prevede HEX. cislo z byte do ASCII formy
//------------------------------------------------------------------------
void HexToChar(unsigned char B)
{
switch(B >> 4)
{
case 0: HEX[0]='0'; break;
case 1: HEX[0]='1'; break;
case 2: HEX[0]='2'; break;
case 3: HEX[0]='3'; break;
case 4: HEX[0]='4'; break;
case 5: HEX[0]='5'; break;
case 6: HEX[0]='6'; break;
case 7: HEX[0]='7'; break;
case 8: HEX[0]='8'; break;
case 9: HEX[0]='9'; break;
case 10: HEX[0]='A'; break;
case 11: HEX[0]='B'; break;
case 12: HEX[0]='C'; break;
case 13: HEX[0]='D'; break;
case 14: HEX[0]='E'; break;
case 15: HEX[0]='F'; break;
}
switch(B & 15)
{
case 0: HEX[1]='0'; break;
case 1: HEX[1]='1'; break;
case 2: HEX[1]='2'; break;
case 3: HEX[1]='3'; break;
case 4: HEX[1]='4'; break;
case 5: HEX[1]='5'; break;
case 6: HEX[1]='6'; break;
case 7: HEX[1]='7'; break;
case 8: HEX[1]='8'; break;
case 9: HEX[1]='9'; break;
case 10: HEX[1]='A'; break;
case 11: HEX[1]='B'; break;
case 12: HEX[1]='C'; break;
case 13: HEX[1]='D'; break;
case 14: HEX[1]='E'; break;
case 15: HEX[1]='F'; break;
}
}
//-----------------------------------------------------------------------
// Vyhradi pamet pro pouzivani funkci GetScr, SetScr, GetScreen, SetScreen
// Pokud neni pozadovana pamet v dispozici, vyhlasi chybu
//------------------------------------------------------------------------
void InitGSVideo()
{
unsigned char B,SBError;
printf("Probiha inicializace obrazovych bufferu : \n");
SBError=0;
for(B=0; B<ScrBuffers; B++)
{
printf(" Buffer[%X] ... ",B);
ScrBuf[B]=NULL;
ScrBuf[B]=(char far *) farmalloc(4096);
if(ScrBuf!=NULL) printf("Ok\n");
else { printf("Error\n"); SBError=1; }
}
if(SBError)
{
printf("\n Nedostatek pameti pro inicializaci vsech bufferu !\n");
printf(" Nektere textove operace nemusi byt korektni, chcete Pokracovat?\n");
printf(" ( ESC = exit / ENTER )");
while(1)
{
GetKey();
if(Scan==1) ExitAbout(0xFF);
if((Ascii==13) | (Scan==57)) return;
}
}
else printf("\n\n");
}
//-----------------------------------------------------------------------
// Uvolni pamet alokovanou procedurou InitGSVideo()
//------------------------------------------------------------------------
void DoneGSVideo()
{
char I;
for(I=0;I<3;I++)
{
if(ScrBuf[I]!=NULL) farfree(ScrBuf[I]);
else
{
printf("\nPred pouzitim procedury DoneGSVideo musi byt pouzita procedura InitGSVideo !\n");
printf("\nChyba pri dealokaci bufferu %x\n",I);
}
}
}
//-----------------------------------------------------------------------
// Vykresleni zakladni obrazovky
//------------------------------------------------------------------------
void InitScreen()
{
textmode(3);
textcolor(7);textbackground(0);
window(1,1,80,25); clrscr();
textcolor(1);textbackground(7);
gotoxy(1,2);
for(I=0;I<1840;I++) putch('°');
textbackground(7);
window(1,1,80,1); clrscr();
window(1,1,80,25);
textcolor(0);
CenterText(1,NazevProgramu);
gotoxy(1,2);
}
//------------------------------------------------------------------------
// Pokousi se odeslat znak, pri preteceni TimeOut vrati 0xFF
//------------------------------------------------------------------------
unsigned char VysliByte(char B)
{
unsigned int TimeOut;
TimeOut=4000; // cca 4s
while(1)
{
if(SendReady())
{
outp(Config.AdresaPortu,B);
return(0);
}
else delay(1);