Bonjour à tous,

J'ai créé un client socket me permettant de communiquer avec une balance i20 de Precia Molen (pour info http://www.developpez.net/forums/d15...cia-molen-i20/).

Je lui envoi une demande de poids à ma balance et je cherche également à faire une demande de tare. Par contre, autant pour la demande de poids que pour la demande de tare ce n'est que lors de mon deuxième clic que j'ai une réponse .. Je me demande si je n'ai pas un problème de connexion déconnexion socket... besoin d'un peu d'aide ...
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
const
  iPort = 11001;
  sHost = '192.168.xx.xxx';
 
//procedures fonctions
function Connexion(csSock : TClientSocket; iPort : integer; sHost : string) : integer;
function Deconnexion(csSock : TClientSocket) : integer;
 
implementation
 
{$R *.dfm}
// ********* Fonction de connexion à la socket *************
function Connexion(csSock : TClientSocket; iPort : integer; sHost : string) : integer;
begin
  csSock.Port := iPort;
  csSock.Host := sHost;
  csSock.Open;
end;
 
// ******** Fonction de déconnexion à la socket ***********
function Deconnexion(csSock : TClientSocket) : integer;
begin
  csSock.Close;
end;
 
//---------------------------------------------------------------------------------------------------------------------
procedure TForm1.ClientSocket1Connect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  StatusBar1.SimpleText := 'Connecté à la balance i20';
end;
 
procedure TForm1.ClientSocket1Connecting(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  StatusBar1.SimpleText := 'Tentative de connexion à l''adresse : '+ Edit2.Text;
end;
 
procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  StatusBar1.SimpleText := 'Déconnecté';
end;
 
procedure TForm1.ClientSocket1Error(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  var ErrorCode: Integer);
begin
  if ErrorEvent=eeConnect then
  begin
    ShowMessage('Impossible de se connecter à  '+Edit3.Text);
    ErrorCode:=0;// pour ne pas déclencher un autre message d'erreur par Delphi
  end;
  StatusBar1.SimpleText:='Déconnecté';
end;
 
procedure TForm1.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);
var
  sMsg, sMsg2 : AnsiString;
  i : integer;
const
  cstDebut=#2;
  cstFin=#13#10;
begin
  sMsg := Socket.ReceiveText;
  Deconnexion(ClientSocket1);  // Une fois le message reçu plus besoin de la connexion socket, on ferme la connexion
  sMsg2 := Trim(Copy(sMsg,Pos(cstDEbut,sMsg)+Length(cstDebut)+2,Pos(cstFin,sMsg)-Pos(cstDebut,sMsg)+Length(cstDebut)));
  RichEdit1.Lines.Add(sMsg2);
  Edit1.Text := Copy(sMsg2,1,9);
 
end;
 
 
 
procedure TForm1.Button1Click(Sender: TObject);  // Bouton Demande de poids
begin
  Connexion(ClientSocket1,iPort, sHost);
  ClientSocket1.Socket.SendText(#01#05#48#51#76#13#10);    // Demande de poids brut en décimal. SOH (Start of heading) + ENQ (Enquiry = Requête) + 03 (Poids net) + L (lecture seule) + CR + LF (Retour chariot)
end;
 
procedure TForm1.Button4Click(Sender: TObject);   // Bouton demande tarage
begin
  Connexion(ClientSocket1,iPort, sHost);
  ClientSocket1.Socket.SendText(#01#16#48#52#77#13#10);  // Envoi demande de tarage = 0. SOH + DLE + 0 4 + M + CR + LF
  ClientSocket1.Socket.SendText(#01#05#48#51#76#13#10);  // Une fois le tarage effectué on affiche le poids pour vérification si poids = 0kg alors tarage effectué
end;
Nom : poidstare.png
Affichages : 393
Taille : 8,0 Ko