testing serialport communication

This commit is contained in:
Lucas Pleß 2012-06-06 12:24:09 +02:00
parent cda52cc452
commit d45fea856c
4 changed files with 134 additions and 6 deletions

View File

@ -11,6 +11,7 @@
<orderEntry type="library" name="javampd-4.0" level="project" />
<orderEntry type="library" name="jerklib" level="project" />
<orderEntry type="library" name="commons-codec-1.41" level="project" />
<orderEntry type="library" name="RXTXcomm" level="project" />
</component>
</module>

View File

@ -2,6 +2,10 @@ package de.ctdo.crashtest.domotics;
public interface IRelaisboard {
void setRelais(int relai, boolean state);
void setRelais(final int relais, final boolean state);
void toggleRelais(final int relais, final int milliseconds);
boolean open();
void close();
}

View File

@ -1,12 +1,132 @@
package de.ctdo.crashtest.domotics;
import de.ctdo.crashtest.log.Logger;
import gnu.io.*;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
public class Relaisboard implements IRelaisboard {
private SerialPort serialPort;
private OutputStream outputStream;
private Boolean serialPortGeoeffnet = false;
private String portName = "/dev/ttyUSB0";
public Relaisboard(final String port) {
this.portName = port;
}
private synchronized void send(final char ch) {
if (!serialPortGeoeffnet) return;
try {
outputStream.write(ch);
} catch (IOException e) {
Logger.sLog("Fehler beim Senden");
}
}
private void sendData(final int relais, final boolean state) {
if(relais >= 0 && relais < 8) {
char charsOff[] = { 'a','b','c','d','e','f','g','h' };
char charsOn[] = { 'A','B','C','D','E','F','G','H' };
if(state) {
send(charsOn[relais]);
} else {
send(charsOff[relais]);
}
}
}
@Override
public void setRelais(int relai, boolean state) {
public boolean open()
{
serialPortGeoeffnet = false;
Boolean foundPort = false;
if (serialPortGeoeffnet != false) {
Logger.sLog("Serialport bereits geöffnet");
return false;
}
Enumeration enumComm = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier serialPortId = null;
while(enumComm.hasMoreElements()) {
serialPortId = (CommPortIdentifier) enumComm.nextElement();
if (portName.contentEquals(serialPortId.getName())) {
foundPort = true;
break;
}
}
if (foundPort != true) {
Logger.sLog("Serialport nicht gefunden: " + portName);
return false;
}
try {
// open port and tell the OS who we are. Wait max 500ms for release if port is currently owned.
serialPort = (SerialPort) serialPortId.open("crashtest app", 500);
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
outputStream = serialPort.getOutputStream();
serialPortGeoeffnet = true;
return true;
} catch (PortInUseException e) {
Logger.sLog("Port belegt");
} catch (IOException e) {
Logger.sLog("Keinen Zugriff auf OutputStream");
} catch(UnsupportedCommOperationException e) {
Logger.sLog("Konnte Schnittstellen-Paramter nicht setzen");
}
return false;
}
@Override
public void close() {
if ( serialPortGeoeffnet ) {
serialPort.close();
serialPortGeoeffnet = false;
outputStream = null;
}
}
@Override
public void setRelais(final int relais, final boolean state) {
if(!serialPortGeoeffnet) return;
Runnable r = new Runnable() {
@Override
public void run() {
sendData(relais, state);
}
};
new Thread(r).start();
}
@Override
public void toggleRelais(final int relais, final int milliseconds) {
if(!serialPortGeoeffnet) return;
Runnable r = new Runnable() {
@Override
public void run() {
sendData(relais, true);
try {
Thread.sleep(milliseconds);
} catch (InterruptedException e) { }
sendData(relais, false);
}
};
new Thread(r).start();
}
}

View File

@ -40,7 +40,6 @@ public class MPDController implements IMPDController {
@Override
public void playSong(final String artist, final String title) {
if(mpd != null) {
Runnable r = new Runnable() {
@Override
public void run() {
@ -72,9 +71,11 @@ public class MPDController implements IMPDController {
}
};
synchronized (mpd) {
new Thread(r).start();
}
}
}
private void addToPlayListIfNeeded(final String artist, final String title) {
MPDDatabase db = mpd.getMPDDatabase();
@ -124,7 +125,9 @@ public class MPDController implements IMPDController {
}
};
synchronized (mpd) {
new Thread(r).start();
}
}
}
}