in reply to Sending ASCII Control Chars over TCP/IP socket

Assuming you already have your communication link established, just send the appropriate string to that link. perlrebackslash shows that you can send a hex byte thru the "\x##" sequence, so STX would be "\x02", and thus <STX>H<EOT> would be "\x02H\x04".

Replies are listed 'Best First'.
Re^2: Sending ASCII Control Chars over TCP/IP socket
by herman58 (Initiate) on Jun 29, 2019 at 16:07 UTC
    Somehow it's working:
    #!/usr/bin/perl use IO::Socket; use strict; my( $socket,$ret ); my $maxlen = 1024; my $timeout = 5; # my $data = "\x02H\x04"; # getVersion <STX>CMD<EOT> my $data = "\x02LS9F\x04"; # getSerial <STX>CMD<CHECKSUM><EOT> $socket = IO::Socket::INET->new ( PeerAddr => '192.168.1.16', PeerPort => 10001, Proto => 'tcp' ) or die "ERROR in + Socket Creation : $!\n"; main::printdata('Client sends',$data ); $socket->send($data) or die "send: $!"; $socket->recv( $ret,$maxlen ); main::printdata('Server responded',$ret ); $socket->close();
    Result:
    Client sends \x02LS9F\x04
    Server responded \x0200000000030000000000C3\x04

    But when I add $socket->recv( $ret,$maxlen ) or die "Fail: $!"; the cmd always fails. with an empty error:
    Client sends \x02LS9F\x04
    Fail:

    How do I increase the verbosity level to find out what's going wrong here?
      How do I increase the verbosity level to find out what's going wrong here?

      Item 1 in the Basic debugging checklist suggests to use warnings which you are not doing here. Enable that for starters.

      $socket->recv( $ret,$maxlen ) or die "Fail: $!";

      The docs for recv say:

      Returns the address of the sender if SOCKET's protocol supports this; returns an empty string otherwise. If there's an error, returns the undefined value.

      But your statement only tests for truth rather than definedness. Try this instead, perhaps:

      $socket->recv( $ret,$maxlen ) // die "Fail: $!";