halcon算子通信算子

使用halcon算子创建服务器:

共两步:

1、open_socket_accept 打开一个接受连接请求的接口(相当于初始化服务器套接字)

2、socket_accept_connect 接受一个监听端口的连接请求(相当于服务器的accept操作)

使用halcon算子创建客户端:

open_socket_connect打开一个插口到一个已存在的插口(相当于执行客户端的connect操作)

以上执行成功便可进行数据传输

****服务器
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_set_colored (12)
**打开一个接受连接请求的接口(相当于初始化服务器套接字)
open_socket_accept (3000, 'protocol', 'HALCON4', AcceptingSocket)
**接受一个监听端口的连接请求(相当于服务区的accept操作)
socket_accept_connect (AcceptingSocket, 'true', Socket)

get_next_socket_data_type (Socket, DataType)
if(DataType='image')
    receive_image (Image, Socket)
endif

get_next_socket_data_type (Socket, DataType1)
if(DataType1='tuple')
    receive_tuple (Socket, Tuple)
endif

edges_sub_pix (Image, Edges, 'canny', 1.5, 20, 40)
send_xld (Edges, Socket)

receive_xld (Polygons, Socket)
split_contours_xld (Polygons, Contours, 'polygon', 1, 5)
gen_parallels_xld (Polygons, Parallels, 10, 30, 0.15, 'true')
send_xld (Parallels, Socket)
receive_xld (ModParallels, Socket)
receive_xld (ExtParallels, Socket)
stop ()
close_socket (Socket)
close_socket (AcceptingSocket)
****客户端
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_set_colored (12)
**打开一个插口到一个已存在的插口(执行客户端的connect操作)
open_socket_connect ('127.0.0.1', 3000, [], [], Socket)


read_image (Image, 'C:/Users/hua13/Desktop/C.jpg')
send_image (Image, Socket)
send_tuple (Socket, [1,2,3,4])
get_next_socket_data_type (Socket, DataType)
if(DataType='xld_cont')
    receive_xld (Edges, Socket)
endif
gen_polygons_xld (Edges, Polygons, 'ramer', 2)

send_xld (Polygons, Socket)
split_contours_xld (Polygons, Contours, 'polygon', 1, 5)
receive_xld (Parallels, Socket)
mod_parallels_xld (Parallels, Image, ModParallels, ExtParallels, 0.4, 160, 220, 10)
send_xld (ModParallels, Socket)
send_xld (ExtParallels, Socket)
stop ()
close_socket (Socket)

在进行数据接收之前可以通过get_next_socket_data_type 算子得到传输的数据类型,方便下一步操作

附注:个人感觉halcon的通讯协议有点像TCP协议,都是必先存在服务器后进行数据传输

本代码注意执行顺序:服务器:open_socket_accept()->客户端:open_socket_connect()->服务器:socket_accept_connect()

你可能感兴趣的:(halcon学习,图像处理,tcp/ip)