Receive-Transmit Bytes - Program-Example in LCE

Top 



The source code below demonstrates the ability of the PLC acts the retransmitter of information with active inner processing.



plc
Receive_Transmit_Bytes // Receive bytes in the Port 0, invert the position of its nibbles (the two halfs of 4 bits of the byte) and then retransmit them in the Port 1.

// Functioning:


// 1) Move the focus to the field of edition that simulates the buffer of the Port 0 (click left mouse above this);


// 2) Enter the byte in this field (it's enough to press the character key or, keeping hold the <Alt> key pressed, to type the decimal value of the byte (from 0 to 255) in the numeric keyboard and, after this, to liberate the <Alt> key - observing that, in this case, the typed values and the correspondent printable characters will depend of the code page set in the operational system);


// 3) Press the button next to the field (buffer) that simulates reception or transmission (mouse click on this, or type <Tab> and <Enter>);


// 4) In this moment an event of reception occurs; then the interrup handle receives the byte, does the programmed processing and returns the already processed byte to the Port 1, waiting the event which will indicate the final of its transmission by the hardware of the equipment;


// 5) Simulate the occurrence of this event of final of transmission pressing the button next to the "buffer" of the Port 1 (the byte will be erased in this instant, completing the simulation of the event);


// 6) End of the cycle, that, done repeatedly, simulates the continual flow of reception, processing and retransmission of bytes by the equipment.


// Obs.: which can occur if the byte is received and, previously it have been processed and retransmitted, other byte comes to be received in the port? Suggestion: alter this program to it to deal with this situation by appropriate way.


var
   I0.0    Enable_Interrup,
AQW0
Byte_Received_P0,
AQW1
Byte_to_be_Transmitted_P1,
Q0.0
    Byte_was_Sent,
Q1.0
    Start;

network
1 // Enables the interrupt handling and attachs the events to be treated to the respective handles.

if
(immediate Enable_Interrup) {
if
(not Start) {
enable_interrupt
;
attach
Receive_Byte    to_the interrupt reception_Port0;
attach
Transmit_Byte    to_the interrupt transmission_Port1;

turn_on
immediate Byte_was_Sent;

turn_on
Start;
}
}
else
{
turn_off
Start;
}

network
2 // Handling of the occurrence of reception byte from the Port 0 event

interrupt_handle
Receive_Byte ( )

if
(Byte_was_Sent) {
receive_byte
in Byte_Received_P0 come_in Port0;

Byte_to_be_Transmitted_P1
B:= 16 * (Byte_Received_P0 B& 2#00001111) + (Byte_Received_P0 B& 2#11110000) / 16; // Inverts position of the nibbles of the received byte

transmit_byte
Byte_to_be_Transmitted_P1 in Port1;

turn_off
immediate Byte_was_Sent;
}

end_interrupt_handle


network
3 // Handling of the occurrence of conclusion of the transmission byte sent in the Port 1 event

interrupt_handle
Transmit_Byte ( )

if
(not Byte_was_Sent) {
turn_on
immediate Byte_was_Sent;
}

end_interrupt_handle


end




As result of the compilation of the
Receive_Transmit_Bytes program above, the SimuPLC 4.1.0 has generated, exactly, the following code, in Instruction List - IL:



// PLC    Receive_Transmit_Bytes

// Receive bytes in the Port 0, invert the position of its nibbles (the two halfs of 4 bits of the byte) and then retransmit them in the Port 1.
// Functioning:
// 1) Move the focus to the field of edition that simulates the buffer of the Port 0 (click left mouse above this);
// 2) Enter the byte in this field (it's enough to press the character key or, keeping hold the <Alt> key pressed, to type the decimal value of the byte (from 0 to 255) in the numeric keyboard and, after this, to liberate the <Alt> key - observing that, in this case, the typed values and the correspondent printable characters will depend of the code page set in the operational system);
// 3) Press the button next to the field (buffer) that simulates reception or transmission (mouse click on this, or type <Tab> and <Enter>);
// 4) In this moment an event of reception occurs; then the interrup handle receives the byte, does the programmed processing and returns the already processed byte to the Port 1, waiting the event which will indicate the final of its transmission by the hardware of the equipment;
// 5) Simulate the occurrence of this event of final of transmission pressing the button next to the "buffer" of the Port 1 (the byte will be erased in this instant, completing the simulation of the event);
// 6) End of the cycle, that, done repeatedly, simulates the continual flow of reception, processing and retransmission of bytes by the equipment.
// Obs.: which can occur if the byte is received and, previously it have been processed and retransmitted, other byte comes to be received in the port? Suggestion: alter this program to it to deal with this situation by appropriate way.

//
=VAR   I0.0   Enable_Interrup
//
=VAR   AQW0   Byte_Received_P0
//
=VAR   AQW1   Byte_to_be_Transmitted_P1
//
=VAR   Q0.0   Byte_was_Sent
//
=VAR   Q1.0   Start

NETWORK    1 // Enables the interrupt handling and attachs the events to be treated to the respective handles.

LDI   I0.0

LDN   Q1.0
ALD
ENI
ATCH   Receive_Byte,   8
ATCH   Transmit_Byte,   26
SI    Q0.0,   1
S   Q1.0,   1

LDI   I0.0
NOT
R   Q1.0,   1

NETWORK    2 // Handling of the occurrence of reception byte from the Port 0 event

INT   Receive_Byte

LD   Q0.0
RCV   AQW0   0
MOVB   AQW0,   Ve1
ANDB   2#00001111,   Ve1
MOVD   16,   Ve2
*
D   Ve1,   Ve2
MOVB   AQW0,   Ve3
ANDB   2#11110000,   Ve3
/
D   16,   Ve3
+D   Ve3,   Ve2
MOVB    Ve2,   AQW1
// Inverts position of the nibbles of the received byte
XMT   AQW1   1
RI    Q0.0,   1

NETWORK    3 // Handling of the occurrence of conclusion of the transmission byte sent in the Port 1 event

INT   Transmit_Byte

LDN   Q0.0
SI    Q0.0,   1