我们买了个串口转TCP的转换器,将他连接到我们的串口设备上,然后我写了个客户端程序,串口服务器作为TCP服务端:
var
buf:array of byte;
begin
client.Active:=false;
client.host:='192.168.1.213';
client.Port:=2000;
client.Active:=true;
ret:=client.Socket.Connected; setlength(buf,5);
buf[0]:=$c0;
buf[1]:=3;
buf[2]:=$39;
buf[3]:=0;
buf[4]:=$fc; total:=client.Socket.Sendbuf(buf,5);
end;
然后在clientsocket的接收事件中写了个messagebox("aaa"),结果发现该事件没有被激活,也就是socket没有收到数据.
procedure TForm1.clientRead(Sender: TObject; Socket: TCustomWinSocket);
begin
showmessage('aaa');
end;
var
buf:array of byte;
begin
client.Active:=false;
client.host:='192.168.1.213';
client.Port:=2000;
client.Active:=true;
ret:=client.Socket.Connected; setlength(buf,5);
buf[0]:=$c0;
buf[1]:=3;
buf[2]:=$39;
buf[3]:=0;
buf[4]:=$fc; total:=client.Socket.Sendbuf(buf,5);
end;
然后在clientsocket的接收事件中写了个messagebox("aaa"),结果发现该事件没有被激活,也就是socket没有收到数据.
procedure TForm1.clientRead(Sender: TObject; Socket: TCustomWinSocket);
begin
showmessage('aaa');
end;
你怎么二边都是client?
应该只是client端指定服务器端的IP和port,然后再active :=true;
服务器端
procedure TForm1.FormCreate(Sender: TObject);
begin
serversocket1.Port :=2000;
serversocket1.Active :=true;
end;
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
socket.SendText('test');
end;
客户端:
procedure TForm1.Button1Click(Sender: TObject);
begin
clientsocket1.Host :='192.168.1.213';
clientsocket1.Port :=2000;
clientsocket1.Active :=true;
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
begin
showmessage('aaa');
end;