方法通信例子

* ***********************************************************************
* This example program shows how to use the HALCON generic socket
* functionality to send and receive arbitrary data via sockets. In this
* example, a TCP socket is created and then used to listen for connection
* requests. To see how it works, please start another HDevelop with the
* program 'generic_socket_send.hdev' after starting this example.
* ***********************************************************************
* Initialize program
Protocol := 'TCP4'
Timeout := 3.0

* Open a listening socket
open_socket_accept (4660, ['protocol','timeout'], [Protocol,Timeout], AcceptingSocket)
* Strip AddressFamily from Protocl
tuple_regexp_match (Protocol, 'TCP|HALCON', BaseProtocol)
if (BaseProtocol == 'TCP' or BaseProtocol == 'HALCON')
    * 
    * Wait for an incoming connection, use the timeout of the
    * AcceptingSocket
    dev_error_var (Error, 1)
    dev_set_check ('~give_error')
    OpenStatus := 5
    while (OpenStatus != 2)
        socket_accept_connect (AcceptingSocket, 'auto', Socket)
        OpenStatus := Error
    endwhile
    dev_set_check ('give_error')
    * 
    * Set the same timeout on the newly created socket
    set_socket_param (Socket, 'timeout', Timeout)
else
    * 
    * UDP sockets do not need an accept()
    Socket := AcceptingSocket
endif
get_socket_param (Socket, 'address_info', Address)

Answer := []
while (Answer != 'End')
    receive_data (Socket, ['cz-csi','A3'], Answer, From)
    if (From[2] == 0)
        Data := 'Received:' + Answer[1]
    else
        Data := 'Received:' + Answer
    endif
    To := [From[0],From[1]]
    Format := 'z'
    send_data (Socket, Format, Data, To)
endwhile
stop ()
close_socket (Socket)
close_socket (AcceptingSocket)

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>