String an Port senden

justalex

Neues Mitglied
Hallo erstmal an alle! Ich hoffe ich hab die richtige Forenkategorie gefunden!

Zu meinem Problem:

Ich habe eine Klasse "Nulltext" in der an ein Port (in meinem Fall COM1) ein String gesendet werden soll. Dazu noch eine kleine main um den string zu füttern. Mein Problem besteht darin, dass die Klasse Nulltext an der Stelle der Portinitalisierung abgurkt und endloss den selben Printout liefert (Line 57).

Java:
package datatransmissionusb;

public class Main {

	public static void main(String[] args) {

            String Bitsequence = "0x00";

            Nulltext NT = new Nulltext();

            NT.SetPort();
            NT.messageString.concat(Bitsequence);

	}

}

Java:
package datatransmissionusb;

import java.io.*;
import java.util.*;
//import javax.comm.*; // for SUN's serial/parallel port libraries
import gnu.io.*; // for rxtxSerial library

public class Nulltext implements Runnable, SerialPortEventListener {

    static CommPortIdentifier portId;
    static CommPortIdentifier saveportId;
    static Enumeration portList;
    InputStream inputStream;
    SerialPort serialPort;
    Thread readThread;
    static String messageString = "Hello, world!";
    static OutputStream outputStream;
    static boolean outputBufferEmptyFlag = false;

    public void initwritetoport() {
        // initwritetoport() assumes that the port has already been opened and
        //    initialized by "public nulltest()"

        try {
            // get the outputstream
            outputStream = serialPort.getOutputStream();
        } catch (IOException e) {
        }

        try {
            // activate the OUTPUT_BUFFER_EMPTY notifier
            serialPort.notifyOnOutputEmpty(true);
        } catch (Exception e) {
            System.out.println("Error setting event notification");
            System.out.println(e.toString());
            System.exit(-1);
        }

    }

    public void writetoport() {
        System.out.println("Writing \"" + messageString + "\" to " + serialPort.getName());
        try {
            // write string to serial port
            outputStream.write(messageString.getBytes());
        } catch (IOException e) {
        }
    }

    public void SetPort() {
        boolean portFound = false;
        String defaultPort = "COM1"; //Linux /dev/ttyUSB0

        System.out.println("Set default port to " + defaultPort);

        // parse ports and if the default port is found, initialized the reader
        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                if (portId.getName().equals(defaultPort)) {
                    System.out.println("Found port: " + defaultPort);
                    portFound = true;
                    System.out.println(portFound);
                    // init reader thread
                    Nulltext reader = new Nulltext();
                }
            }

        }
        if (!portFound) {
            System.out.println("port " + defaultPort + " not found.");
        }
    }

    public Nulltext() {
        // initalize serial port
        SetPort();
        try {
            serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
        } catch (PortInUseException e) {
        }
        try {
            inputStream = serialPort.getInputStream();
        } catch (IOException e) {
        }
        try {
            serialPort.addEventListener(this);
        } catch (TooManyListenersException e) {
        }

        // activate the DATA_AVAILABLE notifier
        serialPort.notifyOnDataAvailable(
                true);
        try {
            // set port parameters
            serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {
        }

        // start the read thread
        readThread = new Thread(this);
        readThread.start();

    }

    public void run() {
        // first thing in the thread, we initialize the write operation
        initwritetoport();


        try {
            while (true) {
                // write string to port, the serialEvent will read it
                writetoport();
                Thread.sleep(1000);


            }
        } catch (InterruptedException e) {
        }
    }

    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[] readBuffer = new byte[20];


                try {
                    // read data
                    while (inputStream.available() > 0) {
                        int numBytes = inputStream.read(readBuffer);


                    }
                    // print data
                    String result = new String(readBuffer);
                    System.out.println("Read: " + result);


                } catch (IOException e) {
                }

                break;

        }
    }
}

Das Printout:

Set default port to COM1
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
Found port: COM1
true
Set default port to COM1
Found port: COM1
true

Wobei sich "Set default port to COM1 - Found port: COM1" endlos wiederholt bis ich es abbreche.

Ich hoffe mir kann jemand helfen 🙂

Grüße alex
 
Im Konstruktor von NullText rufst du
Code:
SetPort()
auf. In
Code:
SetPort()
wird wieder ein neues NullText-Objekt erzeugt (Zeile 66 im geposteten Code). Da sieht doch sehr nach einer Endlosschleife aus 🙂
 

Zurück
Oben