btConnection (Functionblock)

Top  Previous  Next

Architecture:

X32 / NX32

Device support:

MX2 pro, DX4 pro, AX9 pro, MX2 turbo/encore/warp, AX9 turbo

Firmware version:

1.05


The btConnection function block is used to obtain information about a connection. The btConnection function block is assigned to the connection ID obtained from btConnect or btListen. Each connection must have its own btConnection function block. When the assigned connection ID is connected or disconnected by the connected device, the "changed"-output will be set to TRUE, and the program may access the other available parameters and handle the change.

The error code parameter will show the error code - if any. For example, if a connection to a device is initiated with btConnect and the PIN is wrong, btConnection will output "connected:=FALSE" and an "invalid Pin"-error code will be shown.

 

 

Input:

ID : SINT

The connection ID obtained with btConnect or btListen.

 

Output:

Changed : BOOL

TRUE when any change has occurred. The output is valid until the next update of the function block.

 

Connected : BOOL

TRUE when a connection is active. False if disconnected.

 

Address : STRING

The address of the connected device.

 

serialport : SINT

The virtual serial port assigned to this connection.

 

Headset : BOOL

TRUE when the connected device is a headset.

 

Errorcode : INT

Contains the error code when connection is refused, disconnected etc.

261

Authorization failed.

264

Connection timeout.

272

Host timeout.

770

Search failed.

1027

Connection rejected due to security.

1030

Connection failed.

1031

Connection timeout.

1033

Abnormal disconnect.

1041

Connection refused.

 

Declaration:

FUNCTION_BLOCK btConnection;
VAR_INPUT
  ID : SINT;
END_VAR;
VAR_OUTPUT
  Changed   : BOOL;
  Connected : BOOL;
  Address   : STRING;
  SerialPort : SINT;
  HeadSet   : BOOL;
  Errorcode : 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;
 
// Open the Bluetooth library
btOpen(name := "RTCU MX2");
 
// Set the Pin for incoming connections
btSetPin(pin := "1234");
 
// Initiate the outgoing connection
ConID_out:=btConnect(address:="00:e0:98:ae:17:23",pin:="0000");
DebugFmt(message:="Outgoing Connection ID =\1", v1:=ConID_out);
 
// Initiate the incoming connection
ConID_in:=btListen();
DebugFmt(message:="Incoming Connection ID =\1", v1:=ConID_in);
 
// setup the receive and connection function blocks
btRX_out.Data:=ADDR(RXbuffer_out);
btRX_out.MaxSize:=SIZEOF(RXbuffer_out);
btRX_out.id:=ConID_out;
btCon_out.id:=ConID_out;
 
// setup the receive and connection function blocks
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;