INCLUDE rtcu.inc
VAR_INPUT
SerialPort : SINT;
Din : ARRAY[1..12] OF BOOL;
Ain : ARRAY[1..4] OF INT;
NodeSW : ARRAY[1..3] OF BOOL;
END_VAR;
VAR_OUTPUT
Dout : ARRAY[1..12] OF BOOL;
Aout : ARRAY[1..4] OF INT;
RXLed : BOOL;
END_VAR;
FUNCTION getINT:INT;
VAR_INPUT
adr : ptr;
END_VAR;
memcpy(dst:=ADDR(getInt),src:=adr,len:=SIZEOF(getINT));
END_FUNCTION;
FUNCTION setINT;
VAR_INPUT
adr : ptr;
v : INT;
END_VAR;
memcpy(dst:=adr,src:=ADDR(v),len:=SIZEOF(v));
END_FUNCTION;
PROGRAM Slave;
VAR
MyNodeID : SINT;
RX : serFrameReceiver;
Len : SINT;
RxBuffer : ARRAY[0..63] of SINT;
TxBuffer : ARRAY[0..63] of SINT;
Index : SINT;
END_VAR;
MyNodeID:=SINT(NodeSW[3])*4 + SINT(NodeSW[2])*2 + SINT(NodeSW[1]);
DebugFmt(message:="Slave running, nodeid=\1", v1:=MyNodeID);
serOpen(Port:=SerialPort, Baud:=57600, bit:=8, Parity:=0, RS485:=TRUE);
RX(port:=SerialPort, enable:=TRUE, frame:=ADDR(rxbuffer), maxsize:=SINT(SIZEOF(rxbuffer)), stuffch:=16#10, sof:=16#02, eof:=16#03);
BEGIN
RX();
IF RX.Ready THEN
IF (MyNodeID = RXBuffer[1] AND RX.Size=12) THEN
FOR index:=0 TO 7 DO
DOut[index+1]:=(RXBuffer[2] AND shl8(in:=1, n:=index)) <> 0;
END_FOR;
FOR index:=0 TO 3 DO
DOut[index+9]:=(RXBuffer[3] AND shl8(in:=1, n:=index)) <> 0;
END_FOR;
AOut[1]:=getINT(adr:=ADDR(RXBuffer[4]));
AOut[2]:=getINT(adr:=ADDR(RXBuffer[6]));
AOut[3]:=getINT(adr:=ADDR(RXBuffer[8]));
AOut[4]:=getINT(adr:=ADDR(RXBuffer[10]));
RXLed:=NOT RXLed;
TXBuffer[0]:=MyNodeID;
TXBuffer[1]:=RXBuffer[0];
TXBuffer[2]:=0;
TXBuffer[3]:=0;
FOR index:=0 TO 7 DO
IF DIn[index+1] THEN
TXBuffer[2]:=TXBuffer[2] OR shl8(in:=1, n:=index);
END_IF;
END_FOR;
FOR index:=0 TO 3 DO
IF DIn[index+9] THEN
TXBuffer[3]:=TXBuffer[3] OR shl8(in:=1, n:=index);
END_IF;
END_FOR;
setInt(adr:=ADDR(TXBuffer[4]), v:=Ain[1]);
setInt(adr:=ADDR(TXBuffer[6]), v:=Ain[2]);
setInt(adr:=ADDR(TXBuffer[8]), v:=Ain[3]);
setInt(adr:=ADDR(TXBuffer[10]),v:=Ain[4]);
serSendData(port:=SerialPort, data:=ADDR(TXBuffer), size:=12, stuffch:=16#10, sof:=16#02, eof:=16#03);
ELSE
DebugMsg(message:="Message not for this node !");
END_IF;
serFrameReceiveDone(port:=SerialPort);
END_IF;
END;
END_PROGRAM;
|