mabue88
Goto Top

Mit RXTX Daten von serieller Schnittstelle lesen

Hallo,

bislang habe ich überwiegend in C# programmiert. Da ich jetzt aber auf Linux umgestiegen bin scheint mir Java die bessere Wahl zu sein.

Ich benötige ein Programm, das kontinuierlich Daten von der seriellen Schnittstelle einliest und sie in der Konsole ausgibt. An die serielle Schnittstelle ist ein Gerät angeschlossen, das selbstständig kontinuierlich Daten an den PC sendet.

Auf http://en.wikibooks.org/wiki/Serial_Programming/Serial_Java habe ich eine Anleitung gefunden. Allerdings habe ich es übers Wochenende noch nicht geschafft Daten einzulesen. Bislang steht der gesamte Code in der Main. Innerhalb der Einarbeitungsphase reicht das auch erstmal.

import gnu.io.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
 

public class RxTxTest {
	public static void main(String args){
		String wantedPortName = "/dev/ttyUSB0";  
		
		Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
		
		CommPortIdentifier portId = null;  // will be set if port found
		while (portIdentifiers.hasMoreElements())
		{
		    CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
		    
		    System.out.println(pid.getName());
		    if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL &&
		       pid.getName().equals(wantedPortName)) 
		    {
		        portId = pid;
		        break;
		    }
		}
		if(portId == null)
		{
		    System.err.println("Could not find serial port " + wantedPortName);  
		    System.exit(1);
		}
		
		SerialPort port = null;
		try {
		    port = (SerialPort) portId.open(
		        "name", // Name of the application asking for the port   
		        10000   // Wait max. 10 sec. to acquire port
		    );
		} catch(PortInUseException e) {
		    System.err.println("Port already in use: " + e);  
		    System.exit(1);
		}
		
		try {
			port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
		} catch (UnsupportedCommOperationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(1);
		}
		
		BufferedReader is = null;  // for demo purposes only. A stream would be more typical.

		try {
		  is = new BufferedReader(new InputStreamReader(port.getInputStream()));
		} catch (IOException e) {
		  System.err.println("Can't open input stream: write-only");  
		  is = null;
		}
		
		String my_buffer = "";  
		
		/////////////////////////////////////////////////////////////////////////////////////////
		/////////////////////////////////////////////////////////////////////////////////////////
		/*try {
			my_buffer = is.readLine();
		} catch (IOException e) {
			System.out.println("Fehler beim Lesen"); 
			//e1.printStackTrace();
		}*/
		/////////////////////////////////////////////////////////////////////////////////////////
		/////////////////////////////////////////////////////////////////////////////////////////
		
		if (is != null)
			try {
				is.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				System.out.println("IS ist bereits geschlossen");  
			}
		if (port != null) port.close();
		System.out.println("Fertig");  
	}
}

Das ist aktuell das Grundgerät. Der Teil zum Einlesen von Daten ist auskommentiert. Entferne ich die Kommentarzeichen, so kommt ein Fehler
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f0c9c737462, pid=6442, tid=139692467468032
#
# JRE version: OpenJDK Runtime Environment (7.0_65-b32) (build 1.7.0_65-b32)
# Java VM: OpenJDK 64-Bit Server VM (24.65-b04 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  [librxtxSerial.so+0x6462]  read_byte_array+0x52
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again  
#
# An error report file with more information is saved as:
# /home/matthias/workspace/SwissphoneClient/hs_err_pid6442.log
#
# If you would like to submit a bug report, please include
# instructions on how to reproduce the bug and visit:
#   http://icedtea.classpath.org/bugzilla
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
Experimental:  JNI_OnLoad called.

Der verwendete Linux-Benutzer hat die Berechtigung auf die serielle Schnittstelle zuzugreifen.
In der angegebenen Error-Report-Datei finde ich mich (noch) nicht wirklich zurecht.

Kennt jemand so ein Problem und weiss an was es liegt?

Danke
mabue

Content-Key: 248573

Url: https://administrator.de/contentid/248573

Printed on: April 24, 2024 at 09:04 o'clock

Member: Lochkartenstanzer
Lochkartenstanzer Sep 08, 2014 at 07:37:23 (UTC)
Goto Top
Moin,

Ist /dev/ttyUSB0 wirklich der richtige Port? Unter linux heißen die seriellen Schnittstellen üblicherweise /dev/ttySX.

hast Du mal ein einfaches cat /dev/ttyS0 oder ein cat /dev/ttyUSB0 in der shell versucht? Ggf. vorher mit stty noch die Parameter richtig setzen.

lks
Member: maretz
maretz Sep 08, 2014 at 07:38:29 (UTC)
Goto Top
Moin,

was steht denn in der Log drin?

Generell ist es allerdings keine gute Idee mit Java auf Hardware zuzugreiffen - da das dann nicht mehr Plattform-Unabhängig ist. Was ich empfehlen kann: Es gibt von VLinx einen Serial2IP-Konverter - schon kannst du das ganz normal per Socket / Netzwerk machen und bleibst unabhängig vom OS... (Falls das eine Alternative ist).

Ich würde aber mal davon ausgehen (als erster Tipp ins Blaue) das du einfach von der entsprechednen SO-File (Treiber) eine falsche Version hast...

Gruß,

Mike
Member: mabue88
mabue88 Sep 08, 2014 updated at 07:55:45 (UTC)
Goto Top
@Lochkartenstanzer:

"/dev/ttyUSB0" ist definitiv richtig. Ich verwende einen USB-RS232-Adapter.
Mit einem kleinen Python-Programm kann ich die Daten vom Gerät auch schon wunderbar empfangen.
Das Öffnen und Schließen des Port funktioniert ja auch problemlos. Die Fehler treten erst auf, wenn ich versuch Daten vom Port zu lesen.


@maretz:

Mit über 500 Zeilen will ich den Inhalt der Log nicht hier einfügen. Ich habe sie aber mal online gestellt: http://www.file-upload.net/download-9496632/hs_err_pid8186.log.html