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
|
unit UnitTest;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, CPort, StrUtils;
type
TForm1 = class(TForm)
ComPort1: TComPort;
procedure ComPort1RxChar(Sender: TObject; Count: Integer);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
nb : integer;
tab : array[1..10] of String;
ptr: integer;
implementation
{$R *.dfm}
function IsDigit(chaine:string):boolean;
begin
if (chaine[ptr] = '0') or (chaine[ptr] = '1') or (chaine[ptr] = '2') or (chaine[ptr] = '3') or (chaine[ptr] = '4') or (chaine[ptr] = '5') or (chaine[ptr] = '6') or (chaine[ptr] = '7') or (chaine[ptr] = '8') or (chaine[ptr] ='9') then
result := true
else
result := false;
end;
function IsSign(chaine:string):boolean;
begin
if (chaine[ptr] = '.') then//or (chaine[ptr] = '-') or (chaine[ptr] = '+') then
result := true
else
result := false;
end;
// Elimine les caractères "parasites" venant de l'appareil
procedure TraitementChaines();
var
chaine1: string;
i,debut,fin: integer;
begin
for i:=1 to 10 do // Pour chaque chaîne
begin
ptr:=1;
chaine1:=tab[i];
while (IsDigit(chaine1)=false) and (IsSign(chaine1)=false) do
ptr:=ptr+1;
debut:=ptr;
ptr:=ptr+1;
while (IsDigit(chaine1)=true) or (IsSign(chaine1)=true) do
ptr:=ptr+1;
fin:=ptr-1;
chaine1:= ANSIMidStr(chaine1,debut,fin);
tab[i]:= ANSILeftStr(chaine1,fin-debut+1);
end;
end;
// Evenement en cas d'envoi de données par l'appareil
procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
var
chaine:string;
i: integer;
begin
ComPort1.ReadStr(chaine,Count);
ComPort1.ClearBuffer(true,true);
if chaine <> #32#13 then
begin
ShowMessage('mesure n°'+IntToStr(nb)+' effectuée');
// ShowMessage(chaine);
tab[nb]:=chaine;
nb:=nb+1;
end;
if nb = 11 then
begin
TraitementChaines();
for i:=1 to 11 do
showmessage('mesure n°'+inttostr(i)+ ' = '+tab[i]);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ComPort1.open;
nb:=1;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ComPort1.Close;
end;
end. |
Partager