I am trying to create a dynamic chart from the JFreeChart library in order to visualize raw rs232 data. The program works fine at lower sample rates. But unfortunately I am sampling with a very high sampling frequency of 9 KHz. Printing the received rs232 data alone is no problem. After I insert the chart and adding the values to the chart, the program does not work anymore correctly. Sometimes the values are incorrect, upset and without a sense. I have added the code below. I would appreciate any kind of help.
Regards,
Ethernut
Code: Select all
import gnu.io.*;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
//import java.io.OutputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import org.jfree.ui.RefineryUtilities;
//import OeffnenUndSenden.serialPortEventListener;
/**
* Download RXTX from: http://jlog.org/rxtx-win.html
* @author Fadi
*
*/
public class ReadFromRS232 implements Runnable
{
static DynamicDataDemoRPI demo;
/**
* @param args
* @throws InterruptedException
* @throws IOException
*/
public static void main(String[] args) throws IOException,
InterruptedException
{
//TODO Diagram
System.out.println("Creating RS232 Terminal");
demo = new DynamicDataDemoRPI("RS232");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
demo.AddNewData(0.0);//Initialize diagram
Runnable runnable = new ReadFromRS232();
Thread.sleep(300);
new Thread(runnable).start();
System.out.println("main finished");
}
/**
*
*/
CommPortIdentifier serialPortId;
Enumeration enumComm;
SerialPort serialPort;
//OutputStream outputStream;
InputStream inputStream;
Boolean serialPortGeoeffnet = false;
ArrayList<String> stockList = new ArrayList<String>();
String cat = "\0";
// int baudrate = 9600;
int baudrate = 38400;
int dataBits = SerialPort.DATABITS_8;
int stopBits = SerialPort.STOPBITS_1;
int parity = SerialPort.PARITY_NONE;
String portName = "COM3"; //COM Port
int secondsRuntime = 60;
public ReadFromRS232()
{
System.out.println("Konstruktor: EinfachSenden");
}
public void run()
{
Integer secondsRemaining = secondsRuntime;
if (oeffneSerialPort(portName) != true)
return;
while (secondsRemaining > 0)
{
// System.out.println("Sekunden verbleiben: " + secondsRemaining.toString());
secondsRemaining--;
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
}
}
schliesseSerialPort();
}
boolean oeffneSerialPort(String portName)
{
Boolean foundPort = false;
if (serialPortGeoeffnet != false)
{
System.out.println("Serialport bereits geöffnet");
return false;
}
System.out.println("Öffne Serialport");
enumComm = CommPortIdentifier.getPortIdentifiers();
while (enumComm.hasMoreElements())
{
serialPortId = (CommPortIdentifier) enumComm.nextElement();
if (portName.contentEquals(serialPortId.getName()))
{
foundPort = true;
break;
}
}
if (foundPort != true)
{
System.out.println("Serialport nicht gefunden: " + portName);
return false;
}
try
{
serialPort = (SerialPort) serialPortId.open("Öffnen und Senden",
500);
}
catch (PortInUseException e)
{
System.out.println("Port belegt");
}
/*
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
System.out.println("Keinen Zugriff auf OutputStream");
}
*/
try
{
inputStream = serialPort.getInputStream();
}
catch (IOException e)
{
System.out.println("Keinen Zugriff auf InputStream");
}
try
{
serialPort.addEventListener(new serialPortEventListener());
}
catch (TooManyListenersException e)
{
System.out.println("TooManyListenersException für Serialport");
}
serialPort.notifyOnDataAvailable(true);
try
{
serialPort
.setSerialPortParams(baudrate, dataBits, stopBits, parity);
}
catch (UnsupportedCommOperationException e)
{
System.out.println("Konnte Schnittstellen-Paramter nicht setzen");
}
serialPortGeoeffnet = true;
return true;
}
void schliesseSerialPort()
{
if (serialPortGeoeffnet == true)
{
System.out.println("Schließe Serialport");
serialPort.close();
serialPortGeoeffnet = false;
}
else
{
System.out.println("Serialport bereits geschlossen");
}
}
//TODO Read Data over RS232
void serialPortDatenVerfuegbar() throws NumberFormatException,
InterruptedException
{
try
{
byte[] data = new byte[150];
int num;
while (inputStream.available() > 0)
{
num = inputStream.read(data, 0, data.length);
//System.out.println("Array: " + stockList.get(0));
String string = new String(data, 0, num);
stockList.add(string);
// System.out.println("Empfange: " + string);
if (string.indexOf("\n") != -1)
{
//System.out.println("Enter angekommen");
for (String string2 : stockList)
{
cat = cat + string2;
}
stockList.clear();
stockList.remove(stockList);
System.out.println("empfangen: " + cat + ", length: " + cat.length());
if (cat.length() < 15 && cat.length() > 6)
{
demo.AddNewData(Double.parseDouble(cat));
}
cat = "\0";
}
}
// String[] stockArr = new String[stockList.size()];
// stockArr = stockList.toArray(stockArr);
//
// cat=stockArr[0] += stockArr[1] += stockArr[2];
// System.out.println(cat);
}
catch (IOException e)
{
System.out.println("Fehler beim Lesen empfangener Daten");
}
}
class serialPortEventListener implements SerialPortEventListener
{
public void serialEvent(SerialPortEvent event)
{
// System.out.println("serialPortEventlistener");
switch (event.getEventType())
{
case SerialPortEvent.DATA_AVAILABLE:
try
{
serialPortDatenVerfuegbar();
}
catch (NumberFormatException e)
{
System.out.println("Fehler bei NumberFormatException");
System.out.println("cat = " + cat + "Ende");
e.printStackTrace();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
break;
case SerialPortEvent.BI:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.FE:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
case SerialPortEvent.PE:
case SerialPortEvent.RI:
default:
}
}
}
}