当前位置:网站首页>[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
边栏推荐
- Global and Chinese market for blood typing 2022-2028: Research Report on technology, participants, trends, market size and share
- Some tips for using source insight (solve the problem of selecting all)
- 找不到实时聊天软件?给你推荐电商企业都在用的!
- A simple method to prove 1/t Fourier transform
- Win10 shortcut key
- Gradle composite construction
- Define in and define out
- matlab timeserise
- ·Practical website·
- [professional literacy] core conferences and periodicals in the field of integrated circuits
猜你喜欢
C WinForm [view status bar -- statusstrip] - Practice 2
Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine
[untitled] record the visual shock of the Winter Olympics and the introduction of the display screen
A simple method to prove 1/t Fourier transform
C WinForm [change the position of the form after running] - Practical Exercise 4
如何将EasyCVR平台RTSP接入的设备数据迁移到EasyNVR中?
The firmware of the connected j-link does not support the following memory access
Network communication model -- Network OSI tcp/ip layering
Drive LED -- GPIO control
随机推荐
Factors affecting the quality of slip rings in production
MLPerf Training v2.0 榜单发布,在同等GPU配置下百度飞桨性能世界第一
Some tips for using source insight (solve the problem of selecting all)
Global and Chinese market of digital shore durometer 2022-2028: Research Report on technology, participants, trends, market size and share
Win10 shortcut key
Acwing - the collection of pet elves - (multidimensional 01 Backpack + positive and reverse order + two forms of DP for the answer)
C language enhancement -- pointer
Improve lighting C program
matlab timeserise
Altium Designer 19.1.18 - 更改铺铜的透明度
Global and Chinese markets of nano biosensors 2022-2028: Research Report on technology, participants, trends, market size and share
Programming knowledge -- assembly knowledge
Interview catalogue
[untitled] record the visual shock of the Winter Olympics and the introduction of the display screen
Programming knowledge -- basis of C language
L'étude a révélé que le système de service à la clientèle du commerce électronique transfrontalier a ces cinq fonctions!
1-stm32 operation environment construction
C WinForm [get file path -- traverse folder pictures] - practical exercise 6
IEEE access personal contribution experience record
Process communication mode between different hosts -- socket