Skip to content

Commit

Permalink
modified serial-port classes to comply with pi4j-SNAPSHOT-1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
tastyminerals committed Aug 25, 2015
1 parent c428dfb commit 23bd129
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 96 deletions.
33 changes: 25 additions & 8 deletions src/main/java/ElevatorController.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import com.pi4j.io.serial.Baud;
import com.pi4j.io.serial.DataBits;
import com.pi4j.io.serial.FlowControl;
import com.pi4j.io.serial.Parity;
import com.pi4j.io.serial.StopBits;

/* Some code for this class was taken directly from sources.
Original file created on January 2, 2000 by Ben Resner, benres@media.mit.edu
Modified for talking elevator by T.Liadal July 2007.*/
Expand All @@ -7,21 +13,32 @@
*/
public class ElevatorController implements ElevatorControllerInterface {
// floorX -- where X is the number of the floor
final int[] floor0 = { 0x7e, 0x0, 0x3, 0x42, 0x1, 0xff, 0xff, 0x0, 0x20, 0x40, 0x0, 0x19, 0x9, 0x7e };
final int[] floor0_5 = { 0x7e, 0x0, 0x3, 0x42, 0x1, 0xff, 0xff, 0x0, 0x20, 0x41, 0x1, 0x48, 0x1, 0x7e };
final int[] floor1 = { 0x7e, 0x0, 0x3, 0x42, 0x1, 0xff, 0xff, 0x0, 0x20, 0x42, 0x0, 0xa9, 0x3a, 0x7e };
final int[] floor1_5 = { 0x7e, 0x0, 0x3, 0x42, 0x1, 0xff, 0xff, 0x0, 0x20, 0x43, 0x1, 0xf8, 0x32, 0x7e };
final int[] floor2 = { 0x7e, 0x0, 0x3, 0x42, 0x1, 0xff, 0xff, 0x0, 0x20, 0x44, 0x0, 0x79, 0x6e, 0x7e };
final int[] floor3 = { 0x7e, 0x0, 0x3, 0x42, 0x1, 0xff, 0xff, 0x0, 0x20, 0x45, 0x0, 0xa1, 0x77, 0x7e };
private final int[] floor0 = { 0x7e, 0x0, 0x3, 0x42, 0x1, 0xff, 0xff, 0x0,
0x20, 0x40, 0x0, 0x19, 0x9, 0x7e };
private final int[] floor0_5 = { 0x7e, 0x0, 0x3, 0x42, 0x1, 0xff, 0xff, 0x0,
0x20, 0x41, 0x1, 0x48, 0x1, 0x7e };
private final int[] floor1 = { 0x7e, 0x0, 0x3, 0x42, 0x1, 0xff, 0xff, 0x0,
0x20, 0x42, 0x0, 0xa9, 0x3a, 0x7e };
private final int[] floor1_5 = { 0x7e, 0x0, 0x3, 0x42, 0x1, 0xff, 0xff, 0x0,
0x20, 0x43, 0x1, 0xf8, 0x32, 0x7e };
private final int[] floor2 = { 0x7e, 0x0, 0x3, 0x42, 0x1, 0xff, 0xff, 0x0,
0x20, 0x44, 0x0, 0x79, 0x6e, 0x7e };
private final int[] floor3 = { 0x7e, 0x0, 0x3, 0x42, 0x1, 0xff, 0xff, 0x0,
0x20, 0x45, 0x0, 0xa1, 0x77, 0x7e };

// if you send this message to the elevator it will respond with dynamic information, including the floor number
final int[] heartbeat = {0x7E, 0x00, 0x03, 0xe0, 0x01, 0xFF, 0xFF, 0x00, 0x20, 0xc3, 0x1e, 0x64, 0x02, 0x08, 0x3a, 0x7E };
final int[] heartbeat = {0x7E, 0x00, 0x03, 0xe0, 0x01, 0xFF, 0xFF, 0x00,
0x20, 0xc3, 0x1e, 0x64, 0x02, 0x08, 0x3a, 0x7E };

private SerialPortController serialPort = null;

// class constructor
public ElevatorController() {
serialPort = new SerialPortController();
serialPort = new SerialPortController(Baud._115200,
DataBits._8,
Parity.NONE,
StopBits._1,
FlowControl.NONE);
}

// we ask the elevator to send us current floor number
Expand Down
110 changes: 57 additions & 53 deletions src/main/java/SerialPortController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,70 @@
Modified for talking elevator by T.Liadal July 2007.
*/

import com.pi4j.io.serial.Baud;
import com.pi4j.io.serial.DataBits;
import com.pi4j.io.serial.FlowControl;
import com.pi4j.io.serial.Parity;
import com.pi4j.io.serial.Serial;
import com.pi4j.io.serial.SerialDataEvent;
import com.pi4j.io.serial.SerialDataListener;
import com.pi4j.io.serial.SerialDataEventListener;
import com.pi4j.io.serial.SerialFactory;
import com.pi4j.io.serial.SerialPortException;
import com.pi4j.io.serial.StopBits;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


public class SerialPortController {
// creating instance of the serial communication class
final Serial serialPort = SerialFactory.createInstance();
String receivedData = "";

public SerialPortController(){
private final Serial serialPort = SerialFactory.createInstance();
private Baud baud;
private DataBits dbits;
private Parity parity;
private StopBits sbits ;
private FlowControl flow;
private byte[] receivedBytes;

// serialPort constructor
public SerialPortController(Baud baud, DataBits dbits, Parity parity, StopBits sbits, FlowControl flow){
// creating and registering serial data listener
serialPort.addListener(new SerialDataListener() {
@Override
public void dataReceived(SerialDataEvent event) {
receivedData = event.getData();
throw new UnsupportedOperationException("Not supported yet.");
this.baud = baud;
this.dbits = dbits;
this.parity = parity;
this.sbits = sbits;
this.flow = flow;
serialPort.addListener(new SerialDataEventListener() {
@Override
public void dataReceived(SerialDataEvent event) {
try {
receivedBytes = event.getBytes();
} catch (IOException ex) {
Logger.getLogger(SerialPortController.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}

//a few printing methods for debugging
public void printMessage(int[] message) {
for(int i = 0; i<message.length; i++ ) {
System.out.println("message i: " + i + " = " + message[i]);
}
}
public void printMessageAndHex(byte[] message) {
for(int i = 0; i<message.length; i++ ) {
System.out.println("message i: " + i + " = " + message[i] +
" HEX: " + Integer.toHexString(message[i]));
}
}

// writing data to serial port
public void writeToPort(int[] message) {
// open rasppi default serial port provided on the GPIO header with
// DEFAULT_COM_PORT = /dev/ttyAMA0, speed = 38400
if (serialPort.isOpen()) {
serialPort.close();
} else {
try {
serialPort.open(serialPort.DEFAULT_COM_PORT, 38400);
} catch (SerialPortException ex) {
ex.printStackTrace(); // we could use log4j here
} finally {
serialPort.close();
}

try {
for (int i=0; i < message.length; i++){
serialPort.write((byte)message[i]);
}
} catch (IllegalStateException ex) {
ex.printStackTrace(); // we could use log4j here
} finally {
try {
if (serialPort.isOpen()) {
serialPort.close();
}
serialPort.open(Serial.DEFAULT_COM_PORT, baud, dbits, parity, sbits, flow);
for (int i=0; i<message.length; i++) {
serialPort.write((byte)message[i]);
}
} catch (IOException ex) {
System.out.println("EXCEPTION! IOException: " + ex.getMessage());
throw new RuntimeException(ex);
}
}

/**
* Reads all bytes that are being sent from the elevator, chops it up into strings that
* start and end in 0x7E (126), and looks through these for messages with message-ID C9,
Expand All @@ -81,33 +81,37 @@ public int askFloorInformation(int[] heartbeat) {
byte[] byteString = readPortData(heartbeat);
// fills the arraylist with valid substrings
ArrayList messages = findValidSubstrings(byteString);

System.out.println("messages ArrayList has " + messages.size() + " elements now.");
//returns -1 if no floor is set
int floor = -1;
for(int i=0; i<messages.size(); i++) {
int temp = checkMessage((byte[])messages.get(i));
if(temp!=-1){ floor = temp;}
int temp = checkMessage((byte[])messages.get(i));
if(temp!=-1){ floor = temp;}
}
return floor;
}


/**
* Sends a heartbeat to the elevator, who writes status information back.
* We wait until we have gotten a long enough message, then return it.
* @return
*/
private byte[] readPortData(int[] heartbeat) {
writeToPort(heartbeat); // writing the heartbeat to get a reply

try {
Thread.sleep(100);
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
System.out.println("EXCEPTION! InterruptedException: " + ex.getMessage());
throw new RuntimeException(ex);
}
try {
receivedBytes = serialPort.read();
} catch (IllegalStateException ex) {
Logger.getLogger(SerialPortController.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(SerialPortController.class.getName()).log(Level.SEVERE, null, ex);
}
//returns data collected by Listener
return receivedData.getBytes(Charset.forName("UTF-8"));
return receivedBytes;
}

/**
Expand Down Expand Up @@ -194,4 +198,4 @@ private int checkMessage(byte[] message){
return -1;
}
}
}
}
35 changes: 0 additions & 35 deletions src/test/groovy/SerialPortTest.groovy

This file was deleted.

0 comments on commit 23bd129

Please sign in to comment.