Connect via Bluetooth with Delphi XE7 using portable printer

后端 未结 2 1951
天命终不由人
天命终不由人 2021-02-05 09:35

I\'m trying to communicate with a Sewoo LK-P32 printer via Bluetooth. For this, I am using Delphi XE7. I made a few examples that come with Del

2条回答
  •  北海茫月
    2021-02-05 09:56

    I recreated your program, and i get the same problem, but changing your code, it's working fine for me now.

    The problem is here

    if FSocket <> nil then
    begin
      // FSocket.Connect;
      FSocket.Connect;
      Memo1.Lines.Add('Criou socket cliente com o ServerSocket');
      Texto := #27 + '|cA' + 'Teste' + #13#10;
      ToSend := TEncoding.UTF8.GetBytes(Texto);
      FSocket.SendData(ToSend);
      Memo1.Lines.Add('Enviou ' + Texto + ' para a impressora.');
    end
    

    First, i recommend to add Fsocket as a private property, and create ONLY ONE fsocket object. So, your code will changed to

    if (Assigned(LDevice)) And (Assigned(FSocket))
    then Begin
         if Not FSocket.Connected
         then FSocket.Connect
         End
    Else Begin
         FSocket := LDevice.CreateClientSocket(Guid, True);
         Memo1.Lines.Add('Device Socked created to '+LDevice.DeviceName);
         FSocket.Connect;
         End;
    

    After connected, you can call a TTimer to send what you want, o create a method checking if Fsocket is connected.

    if Assigned(FSocket)
    then Begin
         if FSocket.Connected
         then Begin
              Texto := #27 + '|cA' + 'Teste' + #13#10;
              ToSend := TEncoding.UTF8.GetBytes(Texto);
              FSocket.SendData(ToSend);
    
              Sleep(100);
              End;
         End;
    

    Also, you can add a sleep between 2 commands, to be sure the data are received and executed by your printer.

    In my case i used an Arduino width Bluetooth hc-06 module.

提交回复
热议问题