Vous êtes sur la page 1sur 11

AN_BluetoothDriver.

PDF
Beck IPC GmbH
E-Mail: info@beck-ipc.com
www.beck-ipc.com
A member of the Festo group
Beck IPC GmbH
Grninger Weg 24
35415 Pohlheim/Germany
Application note: Universal Bluetooth driver
Bluetooth as replacement for the serial ports used in applications,
demonstrated with CoDeSys.

Introduction

This Application Note describes the software package for a universal Bluetooth
driver for the IPC@CHIP

with the Bluetooth add on BT01. The corresponding


software can be found in AN_Uni ver sal Bl uet oot hDr i ver _V01. zi p.

Usually the programmer uses the Bluetooth library provided with the BT01
examples to integrate Bluetooth support into an application. But there are
different applications (e.g. CoDeSys) that can not or should not be changed or
extended. However there are scenarios where those applications should use
the Bluetooth interface instead of the serial port. For those cases this driver
initializes the Bluetooth module (that in fact is connected to a serial port of the
IPC@CHIP

), configures the serial port and terminates. After that the Bluetooth
module is in a transparent data mode. An application can still use the standard
fossil API commands for the serial port and use the serial Bluetooth connection.
The application does not have to be changed.
This method is very fast and no permanent driver creating CPU load is needed.
The BT01 itself provides the possibility to ensure a permanent connection.
The driver is provided in source code and can be extended for your special
needs.


Configuration

Usually BT01 applications read their configuration from the chi p. i ni section
[ BLUETOOTH] . In addition the BTdr v. exe reads its configuration from a
second section called [ BT_DRV] into a struct called Bl uet oot hDr i ver .
The [ BT_DRV] has to contain the communication parameters (baud rate,
parity etc.) of the application which will use the Bluetooth connection as
replacement for the wired serial connection.
Please note: Dont mismatch this section with the [ BLUETOOTH] section used
by the BT01 Library. Some of the entries are similar (baud rate, parity etc.) but
in [ BLUETOOTH] the values define the communication parameters between
BT01 and IPC@CHIP

if a BT01 is permanently switched to non-default




AN_BluetoothDriver.PDF
Beck IPC GmbH
E-Mail: info@beck-ipc.com
www.beck-ipc.com
A member of the Festo group
Beck IPC GmbH
Grninger Weg 24
35415 Pohlheim/Germany
standard values (for further information please refer to the BT01
documentation).

Supposed there is an application that uses the serial port with 38400 baud
(8N1, no flow control). The serial port communication shall now be replaced by
a Bluetooth connection to a Bluetooth Device with the device address
0012f 304f c05. Then the [ BT_DRV] section in chi p. i ni has to look like
this:
[ BT_DRV]
BT_DRV_BAUDRATE=38400
BT_DRV_PARI TY=NO
BT_DRV_WORDLENGTH=8
BT_DRV_STOPBI TS=1
BT_DRV_FLOWCONTROL=OFF
BT_DRV_REMOTEDEVI CE=0012f 304f c05
BT_DRV_LOCALNAME=MYNAME
BT_DRV_PI N=1234

Possible values for these items in chi p. i ni are:
Item Possible values
BT_DRV_BAUDRATE All allowed baud rates that work with the
BT01 in conjunction with the IPC@CHIP.
Refer to BT01 documentation for further
details.
BT_DRV_PARI TY NO, ODD, EVEN
BT_DRV_WORDLENGTH 7,8
BT_DRV_STOPBI TS 1,2
BT_DRV_FLOWCONTROL OFF, RTSCTS
BT_DRV_REMOTEDEVI CE Optional. A 12 digit Bluetooth device address
of a remote Bluetooth device that the driver
shall connect to
BT_DRV_LOCALNAME Optional. Specify a name for your BT01.
Remote devices will see this name for
identification.
BT_DRV_PI N Optional: A PIN for Bluetooth pairing.



Connection establishment and maintenance

There are two ways on how the connection can be established:
1. The connection is established by the BT01
In this case the Bluetooth device address (e.g. 0012f 304f c05) has to be
defined in the chi p. i ni item entry BT_DRV_REMOTEDEVI CE. The address is
defined as default remote peer of the BT01 and the BT01 will automatically


AN_BluetoothDriver.PDF
Beck IPC GmbH
E-Mail: info@beck-ipc.com
www.beck-ipc.com
A member of the Festo group
Beck IPC GmbH
Grninger Weg 24
35415 Pohlheim/Germany
connect to this peer. If the connection terminates unintentionally (e.g. due to
environmental influences) the BT01 will automatically try to reconnect to the
remote device.
Example 1 and 2 demonstrate this scenario.

2. The connection is established by a remote device
In this case you dont have to specify a remote device address in the
chi p. i ni item entry BT_DRV_REMOTEDEVI CE. The driver initializes the
Bluetooth module and terminates. Then the BT01 waits for an incoming
connection that has to be initiated and established by the remote device (e.g.
PC).
If the connection terminates unintentionally the remote device is responsible for
the reconnection.
Example 3 demonstrates this scenario.


Data exchange

After the driver application has terminated and the connection is established
any other program can open the serial port connected to the BT01 with
standard fossil commands and communicate transparently as if it was directly
connected to the COM port of the remote device. Remember: The fossil port is
defined in the chip.ini section [BLUETOOTH] with the item PORT.



AN_BluetoothDriver.PDF
Beck IPC GmbH
E-Mail: info@beck-ipc.com
www.beck-ipc.com
A member of the Festo group
Beck IPC GmbH
Grninger Weg 24
35415 Pohlheim/Germany

Examples

The following chapters describe the provided examples. They are both
available in source code and ready-to-run compiled applications. The directory
Images contains all files necessary for the according example. Copy these
images to your DK60. The images are configured for the DK60 development
board. There the BT01 is connected to COM2. If you use other hardware
(DB54, own hardware, etc.) the chi p. i ni settings and the C files might
possibly be edited.


Example 1 Simple data transfer

The first example demonstrates a simple data transfer from a BT01 to another
BT01 using the Bluetooth driver. Scenario: An application (sender . exe) is
given that writes data to serial port COM2 with baud rate 19200. The serial
connection ought to be replaced by Bluetooth communication without changing
the application sender . exe.
You can use the following applications:
System 1:
- BTdrv.exe (the driver, of course)
- Sender.exe (writes data with fossil commands directly to SER2)
System 2:
- Remote.exe (receives data from Bluetooth BT01 and prints them to the
console)

BTdrv.exe should be started first. The values used by sender . exe (19200,
8N1) have to be used as item entries in chi p. i ni :
[ BT_DRV]
BT_DRV_BAUDRATE=19200
BT_DRV_PARI TY=NO
BT_DRV_WORDLENGTH=8
BT_DRV_STOPBI TS=1
BT_DRV_FLOWCONTROL=RTSCTS
BT_DRV_REMOTEDEVI CE=0012f 304f c05

The BT_DRV_REMOTEDEVI CE defines the Bluetooth device address of the
remote System 2 Bluetooth adapter, BTdr v. exe will try to connect to this
device. Please refer to the BT01 documentation on how to find out the address
of your devices and modify BT_DRV_REMOTEDEVI CE according to your
address.



AN_BluetoothDriver.PDF
Beck IPC GmbH
E-Mail: info@beck-ipc.com
www.beck-ipc.com
A member of the Festo group
Beck IPC GmbH
Grninger Weg 24
35415 Pohlheim/Germany
Please wait until BTdr v. exe has started and then start Sender.exe that
simply writes data to the serial port COM2 after pressing 0 to 9:
i nt f ossi l Por t = 2;

f ossi l _i ni t ( f ossi l Por t ) ;
f ossi l _set baud( f ossi l Por t , 19200, FOSSI L_PARI TY_NO, 8, 1) ;

. . .

i f ( ( key>' 0' ) && ( key<' 9' ) )
{
spr i nt f ( buf f er , " Hel l o %d" , key) ;
f ossi l _wr i t ebl ock( f ossi l Por t , buf f er , st r l en( buf f er ) ) ;
}

Because BTdr v. exe initialized the Bluetooth interface earlier, all data written to
the serial port is automatically transferred transparently to the second system
remote device via Bluetooth.
On the second system you can run remote.exe for monitoring the sent data or
any other application that handles the received data:


Figure 1: remote.exe on system 2


AN_BluetoothDriver.PDF
Beck IPC GmbH
E-Mail: info@beck-ipc.com
www.beck-ipc.com
A member of the Festo group
Beck IPC GmbH
Grninger Weg 24
35415 Pohlheim/Germany

Example 2 CoDeSys Data exchange

Example 2 is similar to Example 1. The BT01 connects to another Bluetooth
device. But this time a CoDeSys project will access the serial port and send
data via Bluetooth to the second system.

Attention: Please note that in CoDeSys the enumeration of the COM ports
starts with 1 while the IPC@CHIP

starts with 0.

CoDeSys IPC@CHIP


COM 1 COM0 (EXT)
COM 2 COM1 (COM)
COM 3 COM2
COM 4 COM3

For this example on DK60 the BT01 is connected to IPC@CHIPCOM2 so in
CoDeSys programming language COM3 has to be used!

System 1: You need a CoDeSys run time system (RTS) on your IPC@CHIP

.
For further information please refer to the CoDeSys manuals.
If you create the RTS for the IPC@CHIP

you have to specify the serial port


used for the BT01 in myr t s. c. Please make sure that in RHI Ser Por t Types
the third entry (COM2) is set to RHI _SERPORT_COM:

RHI Ser Por t Types s_Ser i al Por t Types =
{
RHI _MAXCOMPORTS, / * NumComPor t s */
{ / * ComPor t Use[ RHI _MAXCOMPORTS] */
RHI _SERPORT_COM, / * EXT ComPor t Use: RHI _SERPORT_NAV or RHI _SERPORT_COM */
RHI _SERPORT_COM, / * COM ComPor t Use: RHI _SERPORT_NAV or RHI _SERPORT_COM */
RHI _SERPORT_COM, / * COM2 ComPor t Use: RHI _SERPORT_NAV or RHI _SERPORT_COM */
RHI _SERPORT_NAV, / * COM3 ComPor t Use: RHI _SERPORT_NAV or RHI _SERPORT_COM */
RHI _SERPORT_NAV, / * COM4 ComPor t Use: RHI _SERPORT_NAV */
RHI _SERPORT_NAV, / * COM5 ComPor t Use: RHI _SERPORT_NAV */
RHI _SERPORT_NAV, / * COM6 ComPor t Use: RHI _SERPORT_NAV */
RHI _SERPORT_NAV, / * COM7 ComPor t Use: RHI _SERPORT_NAV */
},
RHI _SERPORT_SPI , / * SPI Por t Use: RHI _SERPORT_NAV or RHI _SERPORT_SPI */
RHI _SERPORT_I 2C, / * I 2CPor t Use: RHI _SERPORT_NAV or RHI _SERPORT_I 2C */
};

After compiling the RTS, transfer the myr t s. exe to the IPC@CHIP

of system
1. The chi p. i ni has to contain the communication parameters that are used
by the CoDeSys program (see Example2.pro). As in Example 1 the
Bluetooth device address (BT_DRV_REMOTEDEVI CE) has to be modified to
your constellation:

[ BT_DRV]
BT_DRV_BAUDRATE=19200
BT_DRV_PARI TY=NO
BT_DRV_WORDLENGTH=8


AN_BluetoothDriver.PDF
Beck IPC GmbH
E-Mail: info@beck-ipc.com
www.beck-ipc.com
A member of the Festo group
Beck IPC GmbH
Grninger Weg 24
35415 Pohlheim/Germany
BT_DRV_STOPBI TS=1
BT_DRV_FLOWCONTROL=RTSCTS
BT_DRV_REMOTEDEVI CE=0012f 304f c05

Start the BTdr v. exe and afterwards the myr t s. exe. The BT01 connects to
the remote device of the second system.

Load the project Example2.pro in CoDeSys.
At the beginning of PLC_PRG the communication parameters are defined.
They have to match the entries in chi p. i ni defined earlier. Remember that
COM3 in CoDeSys is COM2 on the IPC@CHIP

.

ComSet t . Por t : =COM3;
ComSet t . dwBaudRat e: =19200;
ComSet t . bySt opBi t s: =1;
ComSet t . byPar i t y: =0;

If you want to change the parameters here, dont forget to change them in
chi p. i ni as well. The string sText is written to the COM Port with
SysComWr i t e and transferred to the remote Bluetooth device of System 2:

sText : = ' Thi s i s a t est st r i ng. ' ;
SysComWr i t e( g_dwPor t Handl e, ADR( sText ) , SI ZEOF( sText ) , 100) ;

System 2: Can be configured like in Example 1. Remote.exe will echo the
received data:


Figure 2: r emot e. exe receiving data from CoDeSys


AN_BluetoothDriver.PDF
Beck IPC GmbH
E-Mail: info@beck-ipc.com
www.beck-ipc.com
A member of the Festo group
Beck IPC GmbH
Grninger Weg 24
35415 Pohlheim/Germany

Example 3 CoDeSys Workbench

The third example demonstrates the communication between a PC with one
BT01 connected to the IPC@CHIP

. In this example the connection is


established/initiated from the PC.
Usually the CoDeSys workbench communication is available for TCP/IP or
serial communication. In this example the communication over Bluetooth is
added as another alternative. The CoDeSys serial workbench communication
with the target PLC is replaced by Bluetooth.

Set up your IPC@CHIPwith the following values for chip.ini.
[ BT_DRV]
BT_DRV_BAUDRATE=38400
BT_DRV_PARI TY=NO
BT_DRV_WORDLENGTH=8
BT_DRV_STOPBI TS=1
BT_DRV_FLOWCONTROL=OFF
BT_DRV_LOCALNAME=EXAMPLE 3
BT_DRV_PI N=1234

Please note that for the CoDeSys / PCL communication the baud rate is fixed
and the flow control must be shut off! These values are given and can not be
changed in the runtime system.

In IEC Platform Builder the communication between CoDeSys and PLC is
defined under the tab Device (see Figure 3). Beside TCP/IP you can define a
serial port for communication. Chose port 2 here, it is the COM2 of the
IPC@CHIP where the BT01 is connected on the DK60 for example. For further
information please refer to the CoDeSys Platform builder manual.


Figure 3: Defining serial port for workbench communication


AN_BluetoothDriver.PDF
Beck IPC GmbH
E-Mail: info@beck-ipc.com
www.beck-ipc.com
A member of the Festo group
Beck IPC GmbH
Grninger Weg 24
35415 Pohlheim/Germany
In the generated myr t s. c file, the serial port has to be defined too. Please
make sure that in RHI Ser Por t Types the third entry (COM2) is set to
RHI _SERPORT_COM ( as in example 2) :

RHI Ser Por t Types s_Ser i al Por t Types =
{
RHI _MAXCOMPORTS, / * NumComPor t s */
{ / * ComPor t Use[ RHI _MAXCOMPORTS] */
RHI _SERPORT_COM, / * ComPor t Use: RHI _SERPORT_NAV or RHI _SERPORT_COM */
RHI _SERPORT_COM, / * ComPor t Use: RHI _SERPORT_NAV or RHI _SERPORT_COM */
RHI _SERPORT_COM, / * ComPor t Use: RHI _SERPORT_NAV or RHI _SERPORT_COM */
RHI _SERPORT_NAV, / * ComPor t Use: RHI _SERPORT_NAV or RHI _SERPORT_COM */
RHI _SERPORT_NAV, / * ComPor t Use: RHI _SERPORT_NAV */
RHI _SERPORT_NAV, / * ComPor t Use: RHI _SERPORT_NAV */
RHI _SERPORT_NAV, / * ComPor t Use: RHI _SERPORT_NAV */
RHI _SERPORT_NAV, / * ComPor t Use: RHI _SERPORT_NAV */
},
RHI _SERPORT_SPI , / * SPI Por t Use: RHI _SERPORT_NAV or RHI _SERPORT_SPI */
RHI _SERPORT_I 2C, / * I 2CPor t Use: RHI _SERPORT_NAV or RHI _SERPORT_I 2C */
};

After compiling the RTS, transfer the myr t s. exe to the IPC@CHIP

. Start
the BTdr v. exe. After BTdr v. exe has initialized the BT01 myr t s. exe can
be started. The BT01 is now waiting for an incoming connection from your PC.

The next step is setting up a virtual COM port for your Bluetooth adapter on
the PC. Because there are many different PC-Bluetooth adapters available,
the following screenshots are only examples. The Windows driver for your
system might be different (an ASUS BTD202 USB dongle was used for this
example).
First search for available BT devices and try to connect with the Serial Port
Profile (SPP) to the BT01 (see Figure 4). The name of the BT01 is the defined
name in chip.ini (here EXAMPLE 3). Enter the PIN, if defined. In this example
it is set to 1234.

Figure 4:Searching and connecting to Bluetooth devices


AN_BluetoothDriver.PDF
Beck IPC GmbH
E-Mail: info@beck-ipc.com
www.beck-ipc.com
A member of the Festo group
Beck IPC GmbH
Grninger Weg 24
35415 Pohlheim/Germany
After connecting, Windows will assign a COM Port (here COM4, this may vary
for your PC) to the Bluetooth connection (Figure 5):


Figure 5: A virtual comport has been created on COM4

Please note, that CoDeSys only supports connections until COM32, so make
sure your virtual COM port is 32 or below.

Once a virtual COM port has been defined for your PC Bluetooth adapter you
can connect to the BT01. In CoDeSyS go to Online|Communictaion
Parameters (Figure 6). Please choose the COM Port (here COM4) that
Windows has assigned to your virtual COM port earlier. As stated above, the
other values may not be changed and have to be like:


Figure 6: CoDeSys communication parameters


AN_BluetoothDriver.PDF
Beck IPC GmbH
E-Mail: info@beck-ipc.com
www.beck-ipc.com
A member of the Festo group
Beck IPC GmbH
Grninger Weg 24
35415 Pohlheim/Germany
After the communication is set up you can log on to the IPC@CHIP (via Bluetooth),
download the small CoDeSys example3.pro program and start it (Figure 7).


Figure 7: CoDeSys workbench communication via Bluetooth

Vous aimerez peut-être aussi