xiejiang
xiejiang

Reputation: 3

Why does my ST code only send UDP message once?

I set MainTask to execute "PLC_PRG" every 20 milliseconds.
screenshot

Code in "PLC_PRG":

PROGRAM PLC_PRG
VAR
    str: STRING := 'abc123456789';  
    UDP_Peer_1: NBS.UDP_Peer;
    UDP_Send_1: NBS.UDP_Send;
    
    
    ipAddress_PC:STRING:='192.168.1.200';
    IP_PC: NBS.IP_ADDR;
    
    ipAddress_PLC:STRING:='192.168.1.101';
    IP_PLC: NBS.IP_ADDR;
    
    Sended:BOOL;
END_VAR
IP_PC.sAddr:=ipAddress_PC;
IP_PLC.sAddr:=ipAddress_PLC;

UDP_Peer_1(
    xEnable:= TRUE, 
    xDone=> , 
    xBusy=> , 
    xError=> , 
    ipAddr:= IP_PLC, 
    uiPort:= 12000, 
    ipMultiCast:= , 
    eError=> , 
    xActive=> , 
    hPeer=> );
    
UDP_Send_1(
    xExecute:= TRUE, 
    udiTimeOut:= , 
    xDone=> Sended, 
    xBusy=> , 
    xError=> , 
    hPeer:= UDP_Peer_1.hPeer, 
    ipAddr:= IP_PC, 
    uiPort:= 60000, 
    szSize:= INT_TO_UDINT(LEN(str)), 
    pData:= ADR(str), 
    eError=> );

The behavior I'm expecting is that every 20 milliseconds, the plc device will send the message 'abc123456789' to my PC via udp.

actually The PLC only sends the message once when it starts and then no longer sends it any more.

I set a breakpoint on line 16 of the code "UDP_Send_1(", and I confirmed that it is called multiple times. However, except for the first call, no message is sent to the pc.

thank you for your help.

Upvotes: 0

Views: 222

Answers (2)

Sergey Romanov
Sergey Romanov

Reputation: 3080

In addition to what @Guiorgy posted.

I assume you will not send same message, it is senseless. Thust it will change. Here is how you can send new message every time message changes.

PROGRAM PLC_PRG
VAR
    str, strm: STRING := 'abc123456789';  
    // .....
END_VAR
// ....    
UDP_Send_1(
    xExecute:= (str <> strm), 
    xDone=> Sended, 
    hPeer:= UDP_Peer_1.hPeer, 
    ipAddr:= IP_PC, 
    uiPort:= 60000, 
    szSize:= INT_TO_UDINT(LEN(str)), 
    pData:= ADR(str));

strm := str;

Result is simple raise edge trigger on string value change.

Upvotes: 0

Guiorgy
Guiorgy

Reputation: 1734

Yiu only need to look into the documentation to see:

xExecute: BOOL

Rising edge: Starts defined operation
FALSE: Resets the defined operation after ready condition was reached

In other words, xExecute is a triggered input that activates on a rising edge.

If you set the input to a constant TRUE, it is expected that the message will be sent at most once.

If you want to send messages continuously, then, as the documentation states, you should reset the sender when it's done (when xDone becomes true) or error is encountered (when xError becomes true) by setting xExecute to False for one cycle.

Needless to say, the message won't be sent at every cycle, the most you can hope for is every 2 cycles, but only if sending is always completed in 1 cycle, in other words, xDone becomes true on every next cycle.

Upvotes: 2

Related Questions