We have moved at community.getvera.com

Author Topic: TCP - Telnet (port 23) how to receive/print a response ?  (Read 254 times)

Offline parkerc

  • Beta Testers
  • Sr. Hero Member
  • *****
  • Posts: 2479
  • Karma: +35/-48
  • Life Moves Pretty Fast....
TCP - Telnet (port 23) how to receive/print a response ?
« on: March 22, 2019, 04:53:39 am »
Hi

Im trying to communicate with a new HDMI Matrix device directly via IP, over port 23 and while I can send a command ok, I cant seem to capture the response thats supposed to be being sent back.

Doing some research online, I found the following post that suggests  I couldnt send and receive in the same connection when its a Telnet /port 23 session ?

https://stackoverflow.com/questions/32085919/lua-telnet-send-receive

 Is that true? If so how could I get around that ?

Below are two of the LUA code examples Ive tried, but both are fine with the send, but just result in a timeout/no response.


Any help would be greatly appreciated

1.
Code: [Select]
function SYHDMIA1TO8()
local socket = require("socket")
local host, port = "192.168.102.252", 23
local tcp = assert(socket.tcp())
tcp:connect(host, port);

--note the newline below
tcp:send(string.char(0x3E,0x40,0x57,0x56,0x53,0x4F,0x5B,0x31,0x5D,0x49,0x5B,0x36,0x5D,0x0D));

while true do
    local s, status, partial = tcp:receive()
    print(s or partial)
    if status == "closed" then break end
end
tcp:close()

SYHDMIA1TO8()

2.
Code: [Select]
function SYHDMIA1TO8()
local socket = require("socket")
host = "192.168.102.252"
c = assert(socket.connect(host, 23))
c:settimeout(5)
local sres, serr = c:send(string.char(0x3E,0x40,0x57,0x56,0x53,0x4F,0x5B,0x31,0x5D,0x49,0x5B,0x38,0x5D,0x0D))

print("Send:", sres, serr)
local data, rerr = c:receive(100)
luup.log (data)
print ("Receive:", data, rerr)
c:close()
end

SYHDMIA1TO8()
« Last Edit: March 22, 2019, 04:57:25 am by parkerc »

Offline reneboer

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1574
  • Karma: +110/-31
Re: TCP - Telnet (port 23) how to receive/print a response ?
« Reply #1 on: March 22, 2019, 06:46:54 am »
Hi Parker,

In the example it seem they look at a proxy like function that listens both ways, that's not simple to do in LUA.

However, sending and handling the response should be fine.

Have you tried a telnet session from your PC and see if it is not the remote device not responding? You can try to monitor the network traffic using wireshark and see what happens.

Cheers Rene
2xVeraLite, VeraEdge, openLuup, ALTUI, 20 switches, 10 dimmers, 20 sensors, 10 scene controllers, 1 Harmony Hub, many plug-ins. Not enough time.

Offline parkerc

  • Beta Testers
  • Sr. Hero Member
  • *****
  • Posts: 2479
  • Karma: +35/-48
  • Life Moves Pretty Fast....
Re: TCP - Telnet (port 23) how to receive/print a response ?
« Reply #2 on: March 22, 2019, 09:30:20 pm »
Hi @reneboer

Thanks for responding yes, I did try it with telnet and I do get a response back in the terminal window. 

I used to use the #2 code (above in my first post ) with a different matrix, and that would capture the response that came back ok, its just for some reason,  on this new matrix I can?t capture the responses.

Offline parkerc

  • Beta Testers
  • Sr. Hero Member
  • *****
  • Posts: 2479
  • Karma: +35/-48
  • Life Moves Pretty Fast....
Re: TCP - Telnet (port 23) how to receive/print a response ?
« Reply #3 on: March 23, 2019, 05:53:45 am »
Still plugging away, just tried to add a delay based on comments in other threads/forums, but I?m not sure how I do a delay while keeping the connection open, to view the results ?  Here?s my latest code.

Code: [Select]
function send_SYHDMIA1TO8()
local socket = require("socket")
host = "192.168.102.252"
c = assert(socket.connect(host, 23))
-- c:settimeout(5)
local sres, serr = c:send(">@R8012\r")
print("Send:", sres, serr)
luup.call_delay('recieve_SYHDMIA1TO8', 10)
end


function recieve_SYHDMIA1TO8()
local data, rerr = c:receive(100)
luup.log (data)
print ("Receive:", data, rerr)
c:close()
end

send_SYHDMIA1TO8()

Offline parkerc

  • Beta Testers
  • Sr. Hero Member
  • *****
  • Posts: 2479
  • Karma: +35/-48
  • Life Moves Pretty Fast....
Re: TCP - Telnet (port 23) how to receive/print a response ?
« Reply #4 on: March 23, 2019, 06:00:00 am »
Hold on, hold on, hold on

I can see the response in the logs now using the code above, ive been using LuaTest and that I assume stops after the first function is ended, it does not stay open untill the delayed action has been completed.

Ok, some progress, which is promising ..  ;D

Offline parkerc

  • Beta Testers
  • Sr. Hero Member
  • *****
  • Posts: 2479
  • Karma: +35/-48
  • Life Moves Pretty Fast....
Re: TCP - Telnet (port 23) how to receive/print a response ?
« Reply #5 on: March 24, 2019, 06:09:33 am »
Ok, a short update, I cant seem to capture the various responses that come back.

Ive tried the following variations, and so far I can only get some success when I specify an amount of characters to capture.

Ive altered the timeout too, to see if that works,

Any ideas ?

Code: [Select]
function recieve_SYHDMIA1TO8()
luup.log ("????????????????-   data is COMING NOW  ????????????????")
-- local data, rerr = c:receive('*a') -- does not work
-- local data, rerr = c:receive('*l') -- does not work
local data, rerr = c:receive(110)
luup.log (data)
print ("Receive:", data, rerr)
c:settimeout(10)
c:close()
end
« Last Edit: March 24, 2019, 06:11:27 am by parkerc »

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +290/-70
  • "Less is more"
Re: TCP - Telnet (port 23) how to receive/print a response ?
« Reply #6 on: March 24, 2019, 07:12:03 am »
Just a long shot...

...you might take a look at the LuaSocket documentation here:

http://w3.impa.br/~diego/software/luasocket/tcp.html#setoption

and, in particular, try:
Code: [Select]
c: setoption "tcp-nodelay"

after opening the socket and before doing anything else.

FYI, Nagle's algorithm buffers I/O and can get in the way of alternate read/writes on a socket.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.