The btReceiveData function block is used to receive data from a connected Bluetooth device. The btConnection function block must be assigned to the connection ID obtained from btConnect or btListen and a data buffer where the received data is placed. When the assigned connection ID receives data, the "Ready"-output will be set to TRUE, and the program may access the data in the assigned buffer.
It is important to update the btReceiveData function block often. This is because the Bluetooth module only has a limited buffer capacity, and if a large amount of data is received that is not taken out of the module with btReceiveData, the buffers will eventually be filled and data may be lost.
Input:
ID : SINT
The connection ID obtained with btConnect or btListen.
Data : PTR
Address of the buffer to receive the data in.
maxSize : INT
Maximum number of bytes to receive.
Output:
Ready : BOOL
TRUE when data is available. The output is valid until the next update of the function block.
Size : INT
Number of bytes received.
Declaration:
FUNCTION_BLOCK btReceiveData;
VAR_INPUT
ID : SINT;
Data : PTR;
maxSize : INT;
END_VAR;
VAR_OUTPUT
Ready : BOOL;
Size : INT;
END_VAR;
Example:
INCLUDE rtcu.inc
PROGRAM test;
VAR
rc : INT;
ConID_out : SINT;
ConID_in : SINT;
buffer : ARRAY[1..127] OF SINT;
RXbuffer_out : ARRAY[1..127] OF SINT;
RXbuffer_in : ARRAY[1..127] OF SINT;
btRX_out : btReceiveData;
btRX_in : btReceiveData;
btCon_out : btConnection;
btCon_in : btConnection;
END_VAR;
btOpen(name := "RTCU MX2");
btSetPin(pin := "1234");
ConID_out:=btConnect(address:="00:e0:98:ae:17:23",pin:="0000");
DebugFmt(message:="Outgoing Connection ID =\1", v1:=ConID_out);
ConID_in:=btListen();
DebugFmt(message:="Incoming Connection ID =\1", v1:=ConID_in);
btRX_out.Data:=ADDR(RXbuffer_out);
btRX_out.MaxSize:=SIZEOF(RXbuffer_out);
btRX_out.id:=ConID_out;
btCon_out.id:=ConID_out;
btRX_in.Data:=ADDR(RXbuffer_in);
btRX_in.MaxSize:=SIZEOF(RXbuffer_in);
btRX_in.id:=ConID_in;
btCon_in.id:=ConID_in;
BEGIN
btCon_out();
btCon_in();
btRX_out();
btRX_in();
IF btCon_out.changed THEN
DebugMsg(message:="Connection info changed:");
DebugFmt(message:=" Connected=\1",v1:=INT(btCon_out.connected));
DebugMsg(message:=" Address="+btCon_out.Address);
DebugFmt(message:=" Error=\1", v1:=btCon_out.Errorcode);
END_IF;
IF btCon_in.changed THEN
DebugMsg(message:="Connection info changed:");
DebugFmt(message:=" Connected=\1",v1:=INT(btCon_in.connected));
DebugMsg(message:=" Address="+btCon_in.Address);
DebugFmt(message:=" Error=\1", v1:=btCon_in.Errorcode);
END_IF;
IF btRX_out.ready THEN
DebugMsg(message:="Data Received from outgoing connection");
END_IF;
IF btRX_in.ready THEN
DebugMsg(message:="Data Received from incoming connection");
END_IF;
...
END;
END_PROGRAM;
|