22.07.2009

Java in serial port

Kako komunicirati z napravami preko serijskega porta ter USBja?

Najprej sem mal googlal, vendar nič pametnega… najdem java comm od sun-a, pridem lepo do downloada knjižnice – stran ne obstaja. Potem pa le najdem en forum, kjer predlagajo knjižnico RXTX, ki jo najdeš na http://users.frii.com/jarvi/rxtx/ oz download na http://rxtx.qbang.org/wiki.

Ko stvar preneseš, jo razpakiraš in dobiš knjižnice za win, linux, macos ter solaris + jar datoteka.

Jaz sem delal v win, tako da sem uporabil win dllje ter jar file. Prekopirat pa jih moraš v:

Installation of javax.comm on Linux

copy libLinuxSerialParallel.so to /usr/lib/
copy javax.comm.properties to [JDK-directory]/jre/lib/
copy comm.jar to [JDK-directory]/lib/

Installation of rxtxSerial (from www.rxtx.org) on Linux

copy librxtxSerial.so to /usr/lib
copy RXTXcomm.jar to [JDK-directory]/jre/lib/ext/

Installation of rxtxSerial (from www.rxtx.org) on Windows

copy rxtxSerial.dll to [JDK-directory]\jre\bin\rxtxSerial.dll
copy RXTXcomm.jar to [JDK-directory]\jre\lib\ext\RXTXcomm.jar

Ko imaš te knjižnice pa lahko začneš z delom. Jar file je potrebno še dodati v library v projektu v javi (jaz uporabljam eclipse). Potem pa lahko začnemo s kodo:

Main class:

private void connect(String portName) throws Exception {

try {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
System.out.println(“Error: Port is currently in use”);
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);

if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

System.out.println(“Device connected…”);

InputStream in = serialPort.getInputStream();
SerialReader reader = new SerialReader(in);
reader.run();

OutputStream out3 = serialPort.getOutputStream();
SerialWriter writer = new SerialWriter(out3, serialPort);

writer.setCommand(“xxx”);
writer.run();

//close comport
try{
commPort.close();

}
catch (Exception e) {
System.out.println(“Close port error “+ e);
}
} else
System.out
.println(“Error: Only serial ports are handled by this example.”);
}

} catch (Exception conn) {
System.out.println(“Error connecting:”+conn);

}

}

Branje iz naprave:

import java.io.IOException;
import java.io.InputStream;

public class SerialReader implements Runnable
{
InputStream in;

public SerialReader ( InputStream in )
{
this.in = in;
}

public void run ()
{
byte[] buffer = new byte[1024];
int len = -1;
try
{
while ( ( len = this.in.read(buffer)) > -1 )
{
System.out.print(new String(buffer,0,len));
}
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}

Pisanje v napravo oz pošiljanje komand(class extends SerialPortEventListener):

V konstruktor damo:

try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}

// activate the DATA_AVAILABLE notifier
serialPort.notifyOnDataAvailable(true);

public void run ()
{
System.out.println("Insert command: ");

try {
serialPort.notifyOnOutputEmpty(true);
} catch (Exception e) {
System.out.println(“Error setting event notification”);
System.out.println(e.toString());
}

System.out.println(“Writing \”"+messageString+”\” to “+serialPort.getName());
try {
//write to serial port
out.write(messageString.getBytes());
out.close();

} catch (IOException e) {}
}

// LISTENING TO SERIALPORT EVENTS
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
// we get here if data has been received

byte[] buffer = new byte[1024];
int len = -1;
try
{
while ( ( len = this.in.read(buffer)) > -1 )
{
System.out.print(new String(buffer,0,len));
}
}
catch ( IOException e )
{
e.printStackTrace();
}

break;
}

}

  • Share/Bookmark
  • Share/Bookmark

Dodaj komentar 22.07.2009


Koledar

Julij 2009
P T S Č P S N
     
 12345
6789101112
13141516171819
20212223242526
2728293031  

Objave po mesecih

Objave po kategorijah