当前位置:网站首页>[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

原网站

版权声明
本文为[Changjiang houlang blog]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140547277941.html