Bonjour, j'ai différentes fonctions SendData et ReceiveData et j'aimerai faire en sorte que la fonction ReceiveData me retourne la valeur String seulement quand on lui envoie un truc. Un peu comme le while(Serial3.available()) de l'arduino.
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
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 function TBTMethod.SendData( nPair:integer; sData:string ) : Boolean; var ToSend: TBytes; LDevice: TBluetoothDevice; begin result := FALSE; if ManagerConnected then try if (FSocket = nil) or ( ItemIndex <> nPair ) then begin if nPair > -1 then begin //-----------Pour la connexion LDevice := FPairedDevices[ nPair ] as TBluetoothDevice; FSocket := LDevice.CreateClientSocket( StringToGUID( FServiceGUID ), False); //-----------Pour la connexion if FSocket <> nil then begin ItemIndex := nPair; FSocket.Connect; ToSend := TEncoding.UTF8.GetBytes( sData ); FSocket.SendData(ToSend); // FSocket.ReceiveData result := TRUE; end else ShowMessage('Out of time -15s-'); end else ShowMessage('No paired device selected'); end else begin ToSend := TEncoding.UTF8.GetBytes( sData ); FSocket.SendData(ToSend); result := TRUE; end; except on E : Exception do begin ShowMessage(E.Message); FreeAndNil(FSocket); end; end; end; //--------------------------------------------------------------------------------------- // nPair : Paired Device List No function TBTMethod.Receivedata( nPair:integer) : String ; var ToReceive: TBytes; StrReceive : String; LDevice: TBluetoothDevice; begin result := ''; if ManagerConnected then try if (FSocket = nil) or ( ItemIndex <> nPair ) then begin if nPair > -1 then begin //-----------Pour la connexion LDevice := FPairedDevices[ nPair ] as TBluetoothDevice; FSocket := LDevice.CreateClientSocket( StringToGUID( FServiceGUID ), False); //-----------Pour la connexion if FSocket <> nil then begin ItemIndex := nPair; FSocket.Connect; // ToReceive := TEncoding.UTF8.GetBytes( sData ); ToReceive := FSocket.ReceiveData;; //TBytes //StrReceive := ByteToStr(ToReceive); StrReceive := TEncoding.UTF8.GetString(ToReceive); result := StrReceive; //Probleme renvoie un 3??????? en plus end else ShowMessage('Out of time -15s-Sorry-'); end else ShowMessage('No paired device selected'); end else begin ToReceive := FSocket.ReceiveData;; // FSocket.ReceiveData StrReceive := ByteToStr(ToReceive); result := StrReceive; end; except on E : Exception do begin ShowMessage(E.Message); FreeAndNil(FSocket); end; end; end;
Partager