EasyTranser-Java is an implementation of EasyTransfer in Java.
Lombok jSSC commons-lang3-3.3.2
They are all included in the project and located at lib/jar/.
The test program included in this project checks if the values going back and forth between the java project and arduino is consistent. The java program initiates communication with an arduino board and the board returns the same exact set of value to the java program. The outgoing and incoming serial values are printed in the console. The values of outgoing and incoming serial values are identical to prove that the program is working. To run the test program:
- Open arduino/EasyTransferTest/EasyTransferTest.ino in Arduino IDE.
- Upload the ino program to arduino (I used Arduino UNO for testing).
- Open src/edu.mit.media.of.test.Constants.
- Set the port name for arduino in PORT_NAMES variable. e.g. /dev/tty.usbmodemfd121 for mac.
- Set the DEFAULT_PORT_NAMES_INDEX corresponding to your OS.
- Run TestSerialConnectionManager.java
When you successfully run the program, you should see system prints similar to the following:
Outgoing ================================
Byte: 10
Short: 140
Int: 1230
Long: 104030
Char: ]
Boolean: true
Incoming ================================
Byte: 10
Short: 140
Int: 1230
Long: 104030
Char: ]
Boolean: true
EasyTransfer assumes that the data structures on two programs (i.e. An arduino-like microcontroller and an OS side software) are the same. The receiving data structure on the arduino code must correspond to the transmiting data structure on the java program and vice versa.
For example, in the arduino side test code (arduino/EasyTransferTest/PeripheralsAndProtocols.h), the receiving data structure looks like this:
typedef struct SerialReceiveData {
uint16_t c;
uint8_t bo;
int8_t b;
int16_t s;
int32_t i;
int64_t l;
} SerialReceiveData;
The transmitting data structure on the java program looks like this:
public class TxData extends SerialTransmitData {
private @Getter Character c = ']';
private @Getter Boolean bo = true;
private @Getter Byte b = 10;
private @Getter Short s = 140;
private @Getter Integer i = 1230;
private @Getter Long l = 104030L;
}
If you want to cusomize this program for your own project, modifying these values might be a good starting point. The maximum size of the data structure cannot exceed 256 bytes. Note that the order of each type in the data structure also matters.
SerialEventHandler class is responsible for managing serial data coming in and out of arduino. You will need to use serialEventHandler.event for transmitting serial data and serialEventHandler.getIncomingEvent to wait for data to be received.
In the test code (in arduino/EasyTransferTest/EasyTransferTest.ino), Arduino receives serial messages using:
if(serialEventHandler.getIncomingEvent(&receiveData)){}
In this if statement, you can add your custom functions, switch statements, and etc... based on how you'd like your arduino to behave.
In the test code (arduino/EasyTransferTest/EasyTransferTest.ino), arduino transmits serial data using:
serialEventHandler.event(sendData);
SerialServer and SerialDataReader are responsible for managing serial data coming in and out of java program. SerialServer sends out serial data to arduino using:
serialServer.sendData
The java program receives serial data through SerialDataReader's processInput method:
public void processInput(ByteBuffer message){}
This method decodes incoming serial message and packages up into RxData class.
- The project needs to be tested with Windows and Linux machines.
- More example codes
MIT License applies to this code repository
Copyright (C) 2014 Akito van Troyer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.