Scanning Devices Inc.Sensors, Instruments and Controls | 111 Terrace Hall Avenue
Burlington, Massachusetts (USA) 01803 1-781-272-5135 FAX: 1-781-272-4856 Email us: mail@scanningdevices.com |
Back to Part 1, SD900
Ahead to Part 3, Pocket PC
Contents
The Development section of this Users Guide illustrates the sample application with snippets of code. The complete Metrowerks Code Warrior Project for the SD900 Demonstration Program is available without cost from Scanning Devices via email. It is extensively documented and may form a solid basis for development of your own applications.
If you would like a copy, email Scanning Devices with "Request SD900 Project - Palm OS" in the subject line.
The Palm OS Sample Application distributed for SD900 exercises all commands. It is built with a simple and straightforward user interface. The user generates commands by tapping on the Palm screen to make selections or enter text. The application has no database. User interface and database processing are independent of SD900. The developer can implement these in any favorite style and to any level of complexity.
Sample Application
|
Every session begins by the mobile host initiating a discovery operation. This operation attempts to find and identify bluetooth devices within communication range. Identification is by bluetooth device address, a unique 6-byte identifier. Additonally, the mobile host may ask each responding bluetooth devices for its "friendly name" a user-programmable data string which can uniquely identify the bluetooth device.
Based on the bluetooth device address, the friendly name and/or user input, the mobile host then attempts to establish and configure a wireless connection to the bluetooth device. |
|
Multiple connection types are possible depending on the data to be exchanged. The Scanning Devices Bluetooth Industrial I/O uses an L2CAP connection.
Successful completion of discovery and connection creates a piconet. The mobile client is the master of the piconet and the server is the network slave. Once the piconet is established, the mobile host application can exchange commands and data with the Server.
Commands and Responses, the Client Application
A hypothetical example will demonstrate the SD900 Industrial I/O Server and we can use this example to illustrate the application's development.
Assume a series of discrete sensors are connected to SD900 discrete inputs, representing level in a liquid container. Process Pump-in and Drain-out controls are connected to server's discrete outputs through suitable controllers that allow activation by small signal transistors. Perhaps these are auxiliary or override controls for supervisory adjustments. Finally, flow measurement instrumentation is connected to server's RS232 port.
| The Mobile Handheld client in bluetooth radio range establishes a piconet with SD900 Industrial I/O Server. The mobile client issues a command to read discrete inputs. The server responds with the state of the inputs. The Mobile application interprets the inputs for the user. The user then issues a command to set or clear discrete outputs to adjust pump-in or drain-out controls. The mobile user then issues a command to read RS232 input from flow measurementation. The mobile application interprets the response data. The mobile user then issues a command to send RS232 data to the Flow Instrumentation for control adjustments. |
|
Satisfied with the situation, the mobile handheld user disconnects to end the wireless session. The user has monitored and adjusted a process using bluetooth wireless communications without physical access to the location.
Simple? It seems so. But what the application does not do is also interesting. In the sample application, we have not specified the user interface, not logged the session on the handheld or used a database to check data or generate commands in response to data. All these are application extensions which integrate the wireless capability with the user's system. These extensions can be implmented without special Bluetooth Wireless or SD900 related programming.
In the next session we will show how this application was developed.
Application Development for the Wireless Application
This section presents snippets of code to illustrate command generation and response processing. The complete Metrowerks Code Warrior Project for the SD900 Demo Application is available from Scanning Devices and may provide a basis for your application development. To obtain a copy, email Scanning Devices with "Request SD900 Project" in the subject line.
A major part of application development involves defining and implmenting a user and/or database interface. These tasks are not described here, as we assume that the application developer has their own favorite ways of developing these as well as unique requirements for processing and storage.
The wireless portion of the application depends on the libraries and library support function available on the bluetooth equipped host. Bluetooth options for PC's and Handheld devices are typically provided with software libraries providing access to bluetooth radios for performing discovery, establishing connections, transmitting and receiving data packets with defined formats. Libraries vary from product to product, so you should consult your library provider's documentation for specific information.
Scanning Devices has experience in direct RS232 communications with Bluetooth modules using C. If you need help at that level, please contact us.
We will use the Palm OS bluetooth library to illustrate the structure of the application example. Much detail such as error handling has been omitted for clarity.
The application performs 10 steps. Here they are, with user action and application processing for each step to follow. Back to Menu
Step 1: The application starts by presenting the user with an opening screen an a means of initiating bluetooth discovery.
The application sets up Bluetooth parameters, finds the Bluetooth Library and opens or loads the library. Here is piece of code that does that.
Populate Communications Parameters with default values
Register the callback function. The callback function is the receiver for the server's responses to commands. Responses are asynchronous so are returned as events. The callback function is the interface between the radio and the application these events. This statement associates the callback function we have named "BtLibMECallbackProc" with the library we have opened.
At this point the library is opened and setup is complete. We have not made any transmissions yet.
Back to Menu
Step 2. Discovery
The user starts discovery. The library does the work. This function is an example from the Palm OS library.
Depending on the parameters chosen for this function, the mobile handheld requests the bluetooth device address and local name and presents the user with selection options. The next step is to make a connection to the selected device.
Back to Menu
Step 3. Connect
From the list of bluetooth devices, the user selects the one he wants. In this application it must be a Scanning Devices Bluetooth Industrial I/O Server.
The application attempts to create ACL and L2CAP connections to the selected device.
The response to this command comes as an event handled by the callback function registered when the library was opened. In step 1, we registered the callback function. Here is the declaration for the callback function:
Palm OS calls this function with an event containing information about the response received from the remote bluetooth device. We have to write this function to interpret the response and take action.
Here is the processing for the BtLibLinkConnect response. Error handling has been omitted for clarity. The more complete callback function is described later.
If all goes according to plan, the user is connected. Remember error detection and handling is omitted here.
Back to Menu
Step 4: Establish a function to send data from the example application.
This is the complete function to send data based on the opening of an L2CAP connection to the Scanning Devices server that we did in step 3. This functions sends data in the variable
Back to Menu
Step 5: Request the state of the discrete inputs to examine level sensors (from the example application). We use the SendData function previously defined. BD_GET_DISCRETE_INPUTS is a macro we have defined for readability. It is a single ascii character, the letter E, specified in a macro definition. This function sends 1 byte through the channel.
Back to Menu
Step 6. The callback function handles the response. We don't wait for the response in the application as it would lock up the system whle waiting.
Here is the relevant section of the callback function. The response event delivers two bytes. The first, dataP[0], is the response code, a single ascii character defined in the macro:
After all this, the user sees a graphic representing the discrete inputs, Red for ON, Green for OFF. It is up to your application to interpret and display data in a manner meaningful to the user.
Back to Menu
Step 7. Set the discrete outputs. Since we showed how to interpret the response data in step 6, we won't bother to show how to construct the transmitted data from user input. Assume we have one byte representing the states of the 8 discrete outputs that the user wants to set. Here is the code to send the byte.
The first byte is the command, the ascii character M. Again we defined a macro for readability:
The second byte val has the 8 discrete output states.
Remember that the server generates a response for every command that must be handled. This is done with another case in the callback function.
These macros define two server responses:
The application shows the user that a response has been received.
Back to Menu
Step 8. The user sends some data to instrumentation using the server's RS232 port. The infrastucture is in place. Assume the data to send is dataP (dataP is a pointer to the data) and is of length dataL. This function constructs the message consisting of the command followed by the data and sends it.
The command is a single ascii byte defined by the macro
The callback function handles the response in same way it handled the response to the set discrete output command. It displays the acknowledgement to the user.
Back to Menu
Step 9. By now you are certainly catching on. The user wants to read data transmitted by the instrumentation connected to the server's RS232 port, probably to get a response to the data just transmitted.
Here is the code to send the command.
The response command has been defined:
The response is handled in the callback function. The server reads data from its input buffer and constructs a message with the command followed by data. The callback function puts it in a Palm OS User Interface Field. The application displays it or performs other processing with the response.
Back to Menu
Step 10. The user is finished and want to disconnect from the server. The application has to close the socket and connection. Some housekeeping is required.
The wireless session is done.
This application example demonstrates some of Scanning Devices Bluetooth Industrial I/O server capability. The complete C source code for the SD903STR Palm OS application that exercises all of the server's function is available at Scanning Devices website. You can request it with an email to Scanning Devices with "Request SD900 Project" in the subject line. The 10 step code examples have been extracted from the SD903STR project.
The users guide continues in part 3.