当前位置:网站首页>[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)
WEND
Effect 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
边栏推荐
- Fundamentals of C language
- Extern keyword function
- Extended application of single chip microcomputer-06 independent key
- Halcon's practice based on shape template matching [2]
- Altium Designer 19.1.18 - 隐藏某一个网络的飞线
- [professional literacy] core conferences and periodicals in the field of integrated circuits
- Programming knowledge -- assembly knowledge
- Scm-05 basis of independent keyboard
- Summary -st2.0 Hall angle estimation
- 1089 insert or merge, including test point 5
猜你喜欢
L'étude a révélé que le système de service à la clientèle du commerce électronique transfrontalier a ces cinq fonctions!
万字详解八大排序 必读(代码+动图演示)
Volatile of C language
C language # and #
C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program
C WinForm [change the position of the form after running] - Practical Exercise 4
Screen record of the opening ceremony of the Beijing winter olympics 2
C language uses arrays to realize the intersection, union, difference and complement of sets
Acwing - the collection of pet elves - (multidimensional 01 Backpack + positive and reverse order + two forms of DP for the answer)
Beijing Winter Olympics opening ceremony display equipment record 3
随机推荐
UEFI development learning 3 - create UEFI program
The global and Chinese market of lithographic labels 2022-2028: Research Report on technology, participants, trends, market size and share
UEFI development learning series
Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world
Query the table name used by kettle in Oracle
solver. Learning notes of prototxt file parameters
Gradle composite construction
Altium Designer 19.1.18 - 导入板框
生产中影响滑环质量的因素
UEFI development learning 2 - running ovmf in QEMU
Consul安装
Can't find real-time chat software? Recommend to you what e-commerce enterprises are using!
matlab timeserise
万字详解八大排序 必读(代码+动图演示)
Global and Chinese markets for recycled boilers 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for waste treatment air switches 2022-2028: Research Report on technology, participants, trends, market size and share
1089 insert or merge, including test point 5
Train your dataset with yolov4
Altium designer 19.1.18 - Import frame
Global and Chinese market of rammers 2022-2028: Research Report on technology, participants, trends, market size and share