当前位置:网站首页>[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
2022-07-05 08:02:00 【Changjiang houlang blog】
To meet the needs of customers , Today I will bring you an example of communication between two or more controllers . In the actual project application , It is often necessary to exchange data with another controller , The current plan can just meet your needs .
Look at the current communication scheme configuration

To configure :
controller 1:MC405
IP Address :192.168.0.8
controller 2:MC405
IP Address :192.168.0.9
Switch 1 platform
Enter code content , The communication core code is sub packaged into the Library
file name :SINOLIB.BAL
' Initialization control UDP Communications
'ipadr ip Address data 192.168.0.9
'sockport ip Address port 8899
FUNCTION initudp(ipadr AS STRING,sockport AS INTEGER)
DIM ipadrframe AS STRING(64)
DIM channel AS INTEGER
channel=20
ipadrframe ="dgram:"
ipadrframe =ipadrframe+ipadr
ipadrframe =ipadrframe+"("
ipadrframe =ipadrframe+STR(sockport,0)
ipadrframe =ipadrframe+")"
IF CONTROL >400 THEN
OPEN #channel AS ipadrframe FOR READ_WRITE
PRINT #0,"Init udp network:",ipadrframe
ENDIF
ENDFUNC
' Waiting to receive data
'startadr Data storage address
'num Data storage length
FUNCTION recvdata(startadr AS INTEGER,num AS INTEGER)
DIM getdata AS STRING(255)
DIM getinter AS INTEGER(4)
DIM channel AS INTEGER
DIM idex_read AS INTEGER
DIM get_float AS FLOAT
IF num >60 THEN
num=60
ENDIF
channel=20
IF KEY#channel THEN
'wait recv delay time
WA(1)
CHANNEL_READ(channel,getdata)
'"@" start
IF ASC(getdata)=64 THEN
FOR idex_read=0 TO num-1
getinter(0)=ASC(MID(getdata,1+idex_read*4))
getinter(1)=ASC(MID(getdata,2+idex_read*4))
getinter(2)=ASC(MID(getdata,3+idex_read*4))
getinter(3)=ASC(MID(getdata,4+idex_read*4))
get_float=IEEE_IN(getinter(0),getinter(1),getinter(2),getinter(3))
VR(startadr+idex_read)=get_float
NEXT idex_read
ENDIF
ENDIF
ENDFUNC
' send data
'startadr Data storage address
'num Data storage length
FUNCTION senddata(startadr AS INTEGER,num AS INTEGER)
DIM getdata AS STRING(255)
DIM getinter AS INTEGER(4)
DIM channel AS INTEGER
DIM idex_read AS INTEGER
DIM get_float AS FLOAT
IF num >60 THEN num=60
channel=20
'send header data
getdata="@"
FOR idex_read=0 TO num-1
get_float=VR(startadr+idex_read)
getinter(0)=IEEE_OUT(get_float,0)
getinter(1)=IEEE_OUT(get_float,1)
getinter(2)=IEEE_OUT(get_float,2)
getinter(3)=IEEE_OUT(get_float,3)
getdata =getdata + CHR(getinter(0))
getdata =getdata + CHR(getinter(1))
getdata =getdata + CHR(getinter(2))
getdata =getdata + CHR(getinter(3))
NEXT idex_read
getdata =getdata +CHR(13)+CHR(10)
PRINT #channel,getdata;
ENDFUNC
The code in the library encapsulates three functions
1、 initialization UDP Communications , Set the target IP Address and port number
' Initialization control UDP Communications
'ipadr ip Address data 192.168.0.9
'sockport ip Address port 8899
FUNCTION initudp(ipadr AS STRING,sockport AS INTEGER)
2、 Read the packet and save it in the specified register
' Waiting to receive data
'startadr Data storage address
'num Data storage length
FUNCTION recvdata(startadr AS INTEGER,num AS INTEGER)
3、 Sending a packet automatically grabs data from the specified register
' send data
'startadr Data storage address
'num Data storage length
FUNCTION senddata(startadr AS INTEGER,num AS INTEGER)
The actual engineering code calls the library function
' Set the save clock
DIM save_ticks,setpt_ticks AS INTEGER
DIM local_ipadr,send_ipadr AS STRING(32)
DIM main_program AS INTEGER
main_program=0
IF main_program=0 THEN
local_ipadr="192.168.0.8"
send_ipadr="192.168.0.9"
ELSEIF main_program=1 THEN
local_ipadr="192.168.0.9"
send_ipadr="192.168.0.8"
ENDIF
IP_ADDRESS=local_ipadr
WA(100)
PRINT #0,"set local network:",local_ipadr
PRINT #0,"set send network:",send_ipadr
TICKS=0
' Set the exchange data cycle : Company ms
setpt_ticks=100
save_ticks=TICKS
' Initialization control UDP Communications
'ipadr ip Address data 192.168.0.9
'sockport ip Address port 8899
'FUNCTION initudp(ipadr AS STRING,sockport AS INTEGER)
initudp(send_ipadr,8899)
WHILE TRUE
IF ABS(save_ticks-TICKS)>=setpt_ticks THEN
save_ticks=TICKS
senddata(2000,20)
ENDIF
'read data per loop time
recvdata(3000,20)
WA(10)
WENDEffect demonstration
192.168.0.8=》 towards 192.168.0.9 send data

192.168.0.9=》 towards 192.168.0.8 send data

The experimental test is successful , Achieve the desired effect
Function as follows :
No need to establish communication in advance , One party online instant messaging
Data is sent in real time , No disconnection problem
边栏推荐
- 导电滑环磨损快的原因
- Carrier period, electrical speed, carrier period variation
- Programming knowledge -- assembly knowledge
- C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program
- MLPerf Training v2.0 榜单发布,在同等GPU配置下百度飞桨性能世界第一
- Interview catalogue
- Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine
- Some tips for using source insight (solve the problem of selecting all)
- Drive LED -- GPIO control
- The firmware of the connected j-link does not support the following memory access
猜你喜欢

Network communication model -- Network OSI tcp/ip layering

Record the opening ceremony of Beijing Winter Olympics with display equipment

Altium designer learning (I)

Ads usage skills

万字详解八大排序 必读(代码+动图演示)
![Halcon's practice based on shape template matching [2]](/img/70/3e905661785e570fb406b8e97d41e6.jpg)
Halcon's practice based on shape template matching [2]

L'étude a révélé que le système de service à la clientèle du commerce électronique transfrontalier a ces cinq fonctions!

研究發現,跨境電商客服系統都有這五點功能!

A simple method to prove 1/t Fourier transform

Use of orbbec Astra depth camera of OBI Zhongguang in ROS melody
随机推荐
Bluetooth hc-05 pairing process and precautions
Ads learning record (lna_atf54143)
Acwing-宠物小精灵之收服-(多维01背包+正序倒序+两种形式dp求答案)
UEFI development learning 5 - simple use of protocol
About yolov3, conduct map test directly
Relationship between line voltage and phase voltage, line current and phase current
[professional literacy] specific direction of analog integrated circuits
Step motor generates S-curve upper computer
Embedded composition and route
Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
Ads usage skills
C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,LU分解(LU Decomposition)源程序
C WinForm [view status bar -- statusstrip] - Practice 2
C WinForm [exit application] - practice 3
Embedded AI intelligent technology liquid particle counter
Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world
Global and Chinese markets of nano biosensors 2022-2028: Research Report on technology, participants, trends, market size and share
Altium Designer 19.1.18 - 更改铺铜的透明度
Altium designer 19.1.18 - change the transparency of copper laying
. Net service governance flow limiting middleware -fireflysoft RateLimit