当前位置:网站首页>[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
边栏推荐
- C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,LU分解(LU Decomposition)源程序
- VESC Benjamin test motor parameters
- MLPerf Training v2.0 榜单发布,在同等GPU配置下百度飞桨性能世界第一
- 如何进行导电滑环选型
- matlab timeserise
- Global and Chinese market for blood typing 2022-2028: Research Report on technology, participants, trends, market size and share
- IEEE access personal contribution experience record
- 1089 Insert or Merge 含测试点5
- Volatile of C language
- Global and Chinese markets for waste treatment air switches 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Consul installation

Process communication mode between different hosts -- socket

C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,LU分解(LU Decomposition)源程序

Embedded AI intelligent technology liquid particle counter

Application of ultra pure water particle counter in electronic semiconductors
![Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev](/img/42/d857452ccfeccbbf1ac34f47e47e2e.jpg)
Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev

研究发现,跨境电商客服系统都有这五点功能!

Use of orbbec Astra depth camera of OBI Zhongguang in ROS melody

Cadence simulation encountered "input.scs": can not open input file change path problem

The research found that the cross-border e-commerce customer service system has these five functions!
随机推荐
C WinForm [exit application] - practice 3
C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,LU分解(LU Decomposition)源程序
Global and Chinese market of plastic recycling machines 2022-2028: Research Report on technology, participants, trends, market size and share
[professional literacy] specific direction of analog integrated circuits
How to define guid in AMI code
研究发现,跨境电商客服系统都有这五点功能!
Development tools -- gcc compiler usage
Global and Chinese market of rammers 2022-2028: Research Report on technology, participants, trends, market size and share
如何将EasyCVR平台RTSP接入的设备数据迁移到EasyNVR中?
MLPerf Training v2.0 榜单发布,在同等GPU配置下百度飞桨性能世界第一
Makefile application
UEFI development learning 6 - creation of protocol
C WinForm [realize the previous and next selection pictures] - practice 7
Improve lighting C program
Create inf module in AMI code
Global and Chinese market of digital shore durometer 2022-2028: Research Report on technology, participants, trends, market size and share
Ads usage skills
Factors affecting the quality of slip rings in production
Relationship between line voltage and phase voltage, line current and phase current
Record the torch encountered by win10 cuda. is_ False problem in available()