(25.10.2016, 06:18)admin Wrote: Since you are calling receive() without any arguments it will try to receive all data before socket is closed which is probably not the behavior you want. Try specifying the number of bytes that the reply should contain and it should work.
Good tip, because now I have response but this response is shorter than the query(look at the screenshot). I've tried also '*a' and '*l' arguments but they gave 'nill'. Response should have 15 bytes.
Code:
function str2hex(str)
raw_len = string.len(str)
i = 1
while i <= raw_len do
current_hexvalue = '0x' .. string.format("%02x", string.byte(str, i))
if value then
value = value .. ', ' .. current_hexvalue
else
value = current_hexvalue
end
i = i + 1
end
return value
end
socket = require('socket')
client=socket.tcp()
client:connect('192.168.2.9', 7094)
client:settimeout(1)
cmd = string.char(0xFE,0xFE,0x18,0xD7,0xFA,0xFE,0x0D)
log(cmd)
client:send( cmd )
result = client:receive(15)
result_str = lmcore.strtohex(result, true)
result_str1 = str2hex(result)
log(result, result_str, result_str1)
And also unfortunately I've tried use lmcore.strtohex() but it gives me empty string. I've tried also the str2hex() from Erwin example but it gives me the same. I don't know also why the last 2 variables are not logged and there is no error describing this situation?
I've checked another way and it works perfect
Code:
function str2hex(str)
raw_len = string.len(str)
i = 1
while i <= raw_len do
current_hexvalue = '0x' .. string.format("%02x", string.byte(str, i))
if value then
value = value .. ', ' .. current_hexvalue
else
value = current_hexvalue
end
i = i + 1
end
return value
end
socket = require('socket')
client=socket.tcp()
client:connect('192.168.2.9', 7094)
client:settimeout(1)
cmd = string.char(0xFE,0xFE,0x18,0xD7,0xFA,0xFE,0x0D)
log(cmd)
client:send( cmd )
result = client:receive(15)
result_str = lmcore.strtohex(result, true)
result_str1 = str2hex(result)
log(result)
log(result_str)
log(result_str1)
I don't know why this works
Code:
log(result)
log(result_str)
log(result_str1)
Code:
log(result, result_str, result_str1)
And maybe there is another way to receive some data because sometimes I will not know what is the data length? Maybe not in this script but in the future.