【ECEN002】Lua script for robot communication with TCP Server


Elibot support robot be as TCP/IP server and client. This passage mainly introduce robot as client.

For example, camera as TCP/IP server. Camera receive specified data and send feedback data to robot through TCP/IP.

Elibot lua script manual download link:

https://www.elibot.cn/upload/elibotEn/Download/20220210130125.pdf

Lua Instruction

1. Connect Server

int connect_tcp_server (std ::string IP , int port)

func: connect TCP server with IP and port

para: IP:Server IP

port

return: 0 connect failure,1 connect success

example:

connect_tcp_server ("192.168.1.100" ,7777)

2. Receive Server Data

int,string client_recv_data (std ::string IP ,std :: double recv_timeout ,
int hex ,std :: int port)

Function: The client receives the data sent by the server with the specified IP and port number

Parameter: IP: IP address;

recv_timeout: timeout time (seconds);

hex: Whether it is a hexadecimal number, the received data of 1 is in hexadecimal

character format (default is 0)

port: optional parameter, port number

Return: string returns the data sent from the server, int returns the number of recv, -1 means

that the acceptance fails and needs to be reconnected

ret ,recv= client_recv_data ("192.168.1.100",0.1 ,0 ,7777)
Note: recv_timeout is option, user can use "client_set_recv_timeout" to set

Note: This command is applicable to v2.9.4 and above.

The port parameter is only applicable to v2.15.2 and above.


3. Overall Setup Timeout

int client_set_recv_timeout (std ::string IP ,std :: double recv_timeout)

Function: overall setup recv_timeout and use with the above recv

Parameter: IP:IP address

recv_timeout: timeout time (seconds)

Return: 1 means setting is successful, -1 means failure.

client_set_recv_timeout ("192.168.1.100",0.1)

4. Send Data to Server

int client_send_data (std ::string ip ,std ::string msg ,int hex ,std :: int
port)

Function: The client sends data MSG to the server that specifies the IP and port number.

Parameter: IP: IP address;

msg: Data sent to the server returns: string;

hex: Is it a 16-en-acting number, 1 Send data to 16 credit character format (default is

0)

port: optional parameters, port number

Return: The number of send, -1 indicates that the send fails and needs to be reconnected

client_send_data ("127.0.0.1","OK" ,0 ,7777)

Note: This command applies to V2.9.4 and above.

The Port parameter applies only to V2.15.2 and above.

5. Disconnect TCP

void disconnect_tcp_server (string ip ,std :: int port)

Function: Disconnect the client and server connection

Parameter: IP: IP address

Port: Optional parameters, port numbers (all TCP connections are disconnected by

default)

Return: None

disconnect_tcp_server ("192.168.1.200" ,7777)

Note: The Port parameter applies only to V2.15.2 and above.

Lua example

This example shows robot as client to communicate with TCP/IP server. The code can make lua communicate with TCP/IP server and lua communicate with robot jbi program.

ip = '192.168.1.10'                         -- Server IP(camera or other device)
port = 7777                                 -- Port
----------------------------------------------------------------------------------------------
sleep(2)

script_state = 'D0'
send_state = 'D1'
send_cmd = 'D2'

recv_x = 'D10'
recv_y = 'D11'
recv_z = 'D12'
recv_rx = 'D13'
recv_ry = 'D14'
recv_rz = 'D15'
----variable used between lua and jbi
----------------------------------------------------------------------------------------------
function var_init()                                  -- Initial variable
    set_global_variable(script_state, 0)
    set_global_variable(send_state, 0)
    set_global_variable(send_cmd, 0)
end
----------------------------------------------------------------------------------------------
function get_msgs(i)                                 
    -- user defined data to send to server
    local msgs = {}
    msgs[1] = "This is msg"                        
    msgs[2] = "Please modify as required"          
    msgs[3] = "send data1"                 
    msgs[4] = "send data2"           

    msgs[5] = ""        --
    msgs[6] = ""        --
    msgs[7] = ""        --
    msgs[8] = ""        --
    msgs[9] = ""        --
    return msgs[i]
end
----------------------------------------------------------------------------------------------
function Tcp_client_init(ip, port)                    -- connect server
    local ret = connect_tcp_server(ip, port) 
    local RECV_TIME_OUT = 0.1                         -- time out(s)
    client_set_recv_timeout(ip, RECV_TIME_OUT)
    if ret == 1 then 
        elite_print("client connect "..ip.." success")
    else
        elite_print("connect error")
    end
end 
----------------------------------------------------------------------------------------------
function send_msg(data)                                    -- send data
    if #data <= 0 then
        elite_print("data length error")
    end
    local ret = client_send_data(ip, data)                 -- send message
    if ret == -1 then
        elite_print("send data error")
    end
end
----------------------------------------------------------------------------------------------
function repeat_B(i)                                    -- wait until variable B is equal to 1
    repeat
        elite_print("Wait "..(i).." ~= 0")
        sleep(0.5)
    until(get_global_variable(i) ~= 0)
end
---------------------------------------------------------------------------------------------
function recv_msg()                                      -- recevice data
    while (1) do 
        local ret, recv = client_recv_data(ip)           -- receive data from server
        if ret <= 0 then
            elite_print("client recv data error")
        else
            elite_print(recv)
            local temp = string.split(data,",")
            return temp
        end
    end

end
---------------------------------------------------------------------------------------------
function Example1(data)                          -- data handler1
    for i,v in ipairs(data) do 
        elite_print(v)
    end
end
-------------------------------------------main----------------------------------------------
var_init()
repeat_B(script_state)                          -- wait until B0 equals 1,connect server
elite_print("Script running starts")

Tcp_client_init(ip, port)

while(get_global_variable(script_state) ~= 0) do
    is_send = get_global_variable(send_state)

    if is_send ~= 0 then
        cmd = get_global_variable(send_cmd)

        if cmd == 1 then
            send_msg(get_msgs(cmd))
            recv_table = recv_spilt(recv_msg())

            -- data handler1 --
            Example1(recv_table)


        elseif cmd == 2 then
            send_msg(get_msgs(cmd))
            recv_table = recv_spilt(recv_msg())
            -- data handler2 --

        elseif cmd == 3 then
            send_msg(get_msgs(cmd))
            recv_table = recv_spilt(recv_msg())
            -- data handler3 --


        end

        set_global_variable(send_state, 0)
        set_global_variable(send_cmd, 0)
    end

    sleep(0.001)
end

disconnect_tcp_server(ip)
elite_print("Script execution ends")













点击显示全文
赞同0
发表评论
分享

手机扫码分享
0
301
收藏
举报
收起
登录
  • 密码登录
  • 验证码登录
还没有账号,立即注册
还没有账号,立即注册
注册
已有账号,立即登录
选择发帖板块
举报
请选择举报理由
举报
举报说明