J'ai essayé de télécharger ComPort Library mais ça ne marche pas
J'ai essayé de télécharger ComPort Library mais ça ne marche pas
Bonjour !
Moi j'utilise CreateFile de l'API windows.
Exemple de code que j'utilise pour ouvrir le port:
Ensuite pour le configurer (à adapter bien sur car j'ai copier/coller mon code):
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 //---------------------------------------------------------------------------- Function TCom.OpenCom :Boolean; Var PCharPort : Array[0..100] of Char; c : THandle; S:string; Begin S:=Name; if length(Name)>4 then S:='\\.\'+Name; // if the com port>9 then insert this string before: '\\.\' StrPCopy(PCharPort,UpperCase(S)); // The Name is the com port c:=0; Cid := CreateFile(PCharPort, GENERIC_READ or GENERIC_WRITE, 0, nil,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, c); // to avoid a crash at the end, recall the function.... if (Cid=INVALID_HANDLE_VALUE)then Cid:=CreateFile(PCharPort,GENERIC_READ or GENERIC_WRITE,0,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,c); Result:=(Cid <> INVALID_HANDLE_VALUE); End;
Fermeture:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Function TCom.SetupCom:boolean; Var DCB : TDCB; flag : longint; Begin result:=false; if ComIsOpen then begin if GetCommState(Cid, dcb) then begin dcb.BaudRate := strtoint(Baud); dcb.ByteSize := strtoint(DataSize); case lowercase(parity)[1] of 'n' : dcb.Parity :=0; 'o' : dcb.Parity :=1; 'e' : dcb.Parity :=2; else dcb.Parity :=3; end; dcb.StopBits := strtoint(NbStop)-1; dcb.Flags := 0; //08/12/2016: Ajout --------------------------------------- (* flag bitmap b0: fBinary (1) b1: fParity (2) b2: fOutxCtsFlow (4) b3: fOutxDsrFlow (8) b4,5: fDtrControl (x10,x20) b6: fDsrSensitivity (x40) b7: fTXContinueOnXoff (x80) b8: fOutX (x100) b9: fInX (x200) b10:fErrorChar (x400) b11:fNull (x800) b12,13:fRtsControl (x1000,x2000) b14:fAbortOnError (x4000) b15..31: fDummy2 *) flag := $5081; // fbinary(1) + TxContinueOnXOff($80) + RtsControlEnabled($1000) + AbordOnError($4000) if (dcb.Parity<>0) then flag := flag + 2; // dcb_ParityCheck; if (CTS<>0) then flag := flag + 4; // dcb_OutxCtsFlow; if (DSR<>0) then flag := flag + 8; // dcb_OutxDsrFlow; flag := flag OR DTR; if (CTS<>0) or (DSR<>0) or (DTR<>0) then dcb.flags := flag; //08/12/2016: fin Ajout------------------------------------- SetCommState(Cid, dcb); end; result := (Cid <> INVALID_HANDLE_VALUE); end; End;
Envoi de trame:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 Procedure TCom.CloseCom; Begin if ComIsOpen then CloseHandle(cid); Cid := INVALID_HANDLE_VALUE; End;
Lecture de trame:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 Function TCom.SendFrame(TxData:PByte;len:integer):boolean; Var lw : longword; Ok : Boolean; Begin if AppIsStopped then Begin result:=true; exit; End; if (FormAnalyser<>nil) then TCom_FormAnalyser(FormAnalyser).LogFrame(CurrentModule,false,ByteArrayToStr(TxData,len,' ')); PurgeComm(cid, PURGE_TXABORT or PURGE_RXABORT or PURGE_TXCLEAR or PURGE_RXCLEAR); lw := 0; Ok := true; if ComIsOpen then Ok:=winprocs.WriteFile(cid, Pansichar(@TxData[0])^, longword(len), longword(lw), nil); if not(ok) then closecom; inc(CounterFrame); ShowTx(false); result:= (lw=longword(len)) and Ok; End;
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 function TCom.ReadFrame(RxData:PByte;ExpectedLen:integer):integer; // This function is implemented only in the com port and in the socket Var Status : TComStat; n,dwErrorCode,cbCharsRead : dword; Begin result:=0; n := 0; if AppIsStopped then exit; try ClearCommError(cid, dwErrorCode, @status); n := Status.Cbinque; if (n>0) then ShowRx; except end; if (n > 0) then // if chars are ready to read Begin if integer(n)>ExpectedLen then n:=dword(ExpectedLen); result := n; try if ReadFile(cid, RxData^, n, cbCharsRead, nil) then result:=cbCharsRead; if (FormAnalyser<>nil) and (result>0) then TCom_FormAnalyser(FormAnalyser).LogFrame(CurrentModule,true,ByteArrayToStr(RxData,result,' ')); except end; End; End;
Delphi7 et j'ai de difficulté a créé une connection de communication entre mon application avec une machine de contrôle de facturation qui a un port RS232C.
Je veux envoyer de message à la machine à partir de delphi
Pas plus clair...
Pour envoyer une chaine avec ce composant on fait comme ça :
Après il faut déjà que les paramètres de communication et le port soient bien configurés (et ça je ne peux pas le faire pour toi, les paramètres dépendent de la machine connectée sur le port série ) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 type TForm3 = class(TForm) ComPort1: TComPort; private { Déclarations privées } public { Déclarations publiques } end; var Form3: TForm3; implementation {$R *.dfm} procedure TForm3.FormCreate(Sender: TObject); begin ComPort1.Open; ComPort1.WriteStr('TOTO'); end;
Merci Mr pour votre aide
C'est bon j'ai fait la configuration et tout ça marche mais le soucis est que comment envoyé une commande décris dans le protocole de communication à la machine. Merci
Delphi 5 Pro - Delphi 11.3 Alexandria Community Edition - CodeTyphon 6.90 sous Windows 10 ; CT 6.40 sous Ubuntu 18.04 (VM)
. Ignorer la FAQ Delphi et les Cours et Tutoriels Delphi nuit gravement à notre code !
Vous avez un bloqueur de publicités installé.
Le Club Developpez.com n'affiche que des publicités IT, discrètes et non intrusives.
Afin que nous puissions continuer à vous fournir gratuitement du contenu de qualité, merci de nous soutenir en désactivant votre bloqueur de publicités sur Developpez.com.
Partager