Raspberry Pi Experiment TinkerForge RS232 Serial Communication


Introduction
Serial Communication between a PC and a Raspberry Pi using TinkerForge RS232 Bricklet.
  • The RS232 Bricklet is connected to a TinkerForge Master Brick.
  • The Master Brick is connected, via USB, to a Raspberry Pi 2.
  • The PC uses an USB-to-Serial Converter to connect to the RS232 Bricklet.
This is a B4J UI Application on the PC and a B4J Non-UI Application with jSwing Components on the Raspberry Pi.
Functionality
Ready to develop?
Before starting, ensure to read Get Ready first.

The Solution

Components
  • Hardware: Raspberry Pi, TinkerForge Master Brick, TinkerForge RS232 Bricklet, USB-to-Serial Converter
  • Software: B4J, B4J-Bridge, TinkerForge Brick Daemon, TinkerForge Brick Viewer
Setup the Hardware Components
The TinkerForge Master Brick is connected via USB to the Raspberry Pi. The TinkerForge RS232 Bricklet is connected to the Master Brick.
The PC has an USB-to-Serial Converter connected from an USB Port to the RS232 Terminal Block.
The RS232 Pins RS232 Pins:2 = TX = Red, 3 = RX = Orange and 5 = GND = Green are used
The TinkerForge Brick Daemon and the Brick Viewer must be installed on the Raspberry Pi.
B4J Application Concept
PC

The PC uses a B4J UI App with the libraries jSerial and jRandomAccess. After the Serial Port has been initialized, communication is handled via asyncronous streams (async). Messages are send to the Raspberry Pi via the async stream and the same applies for receiving data. Look at the project source PCClientB4J for further information.

Raspberry Pi - Brick Viewer

The TinkerForge Brick Viewer enables to send and receive data. This is a very good way to test the communication. It is recommended to read the Brick Viewer Documentation.
There are two B4J classes defined, the main project (in tfio16mqtt.b4j) and the Server Websocket Class.

Raspberry Pi - B4J Non-UI App with jSwing Components

Bit of background first: The newer Java versions (> 1.8.0_6) do not support JavaFX apps anymore, but Java apps with frameworks like jSwing do run on the Raspberry Pi. As a result B4J UI apps do not work on the newer Java versions, but B4J Non-UI apps still do. As B4J supports Inline Java, a solution is to build a B4J Non-UI with the jSwing Framework in the Inline Java Code and call B4J events from there.
This B4J Non-UI app makes further more use of the TinkerForge Java Library to communicate via TCP/IP with the RS232 Bricklet. Again defined as Inline Java.
The UI (jSwing components) of the B4J Non-UI app has a Messages Received textarea and a Messages Send Text Area. Message are send via a Button.
The B4J Non-UI app is started from a terminal (which has been opened from the Raspberry Pi Desktop UI). The command is java -jar rpiclientb4j.jar. Instead of a terminal, a shortcut can also be defined).
Note: This application can also be run from any other PC as the communication is via TCP/IP. The Raspberry Pi takes care of the communication. This is very helpfull when building and testing the application.

Run the Experiment
Connect all the components. See picture at the right. Download the project source.
PC

Build the B4J UI application. The COM port is hardcoded in the source, so need to change accordingly.
Run the application from the B4J IDE (press F5).

Raspberry Pi - Brick Viewer

Start the Brick Viewer from the Raspberry Pi Desktop > Menu > Electronics > Brick Viewer. Connect to the Master Brick, select RS232.
Type some text in the Input field and press return. The text should be displayed on the PC in the Message Received window.
On the PC, type some text in the Input field and press send. The text should be displayed on the Raspberry Pi in the Brick Viewer Message window.

Raspberry Pi - B4J Non-UI App with jSwing Components

First test on a PC
Build the B4J UI application. The communication settings are defined in the Objects folder file settings.txt. Adjust accordingly.
Run the application from the B4J IDE (press F5).
Type some text in the Input field and press return. The text should be displayed on the connected PC in the Message Received window.
On the connected PC, type some text in the Input field and press send. The text should be displayed on the PC in the Message Received window.

Test on the Raspberry Pi
Create a folder, like /home/pi/b4j/rs232. Copy the B4J Non-UI app created earlier to that folder and run from a Terminal (opened via the Raspberry Pi Desktop).
The command is java -jar rpiclientb4j.jar. Instead of a terminal, a shortcut can also be defined).

Download
ChangeLog
  • Dec 23, 2015: First Version.
ToDo
  • None
Additional Information
  • Enjoy Tinkering
Hardware Setup
Software Setup
Software Setup
How it looks