*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com_test;
import com.fazecast.jSerialComm.SerialPort;
import java.awt.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Com_test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Com_test gestartet");
get_serial_data();
}
public static void get_serial_data()
{
System.out.println("get_serial_data gestartet");
SerialPort comPort = null;
int ap = 0;
try
{
while(ap < 1) {
ap = SerialPort.getCommPorts().length;
}
//ap = SerialPort.getCommPorts().length;
comPort = SerialPort.getCommPorts()[1];
}
catch(java.lang.ArrayIndexOutOfBoundsException ioe)
{
System.out.println("USB not connected ?");
System.exit(1);
}
catch (Exception e) {
System.out.println( "STeP 1 anderer Fehler..."+e.toString());
e.printStackTrace();
System.exit(1);
}
try
{
System.out.println("USB-Port "+comPort.getDescriptivePortName());
comPort.setBaudRate(115200);
//comPort.setBaudRate(9600);
comPort.openPort();
while(! comPort.isOpen()) {
System.out.println("USB-Port closed !?");
}
}
catch (Exception e1) {
System.out.println( "STeP 2 anderer Fehler..."+e1.toString());
e1.printStackTrace();
System.exit(1);
}
int readValue = 0;
String sPrefix = "";
String sBuf = "";
int decNumber = 0;
String wholeString = "";
boolean success_flag = true;
//byte btMarks[] = null;
//byte btMark = btMarks[0];
String[] sParts = {"","",""};
try {
while (success_flag)
{
while (comPort.bytesAvailable() == 0)
Thread.sleep(1);
String inputLine=null;
BufferedReader input = new BufferedReader(new InputStreamReader(comPort.getInputStream()));
inputLine = input.readLine();
System.out.println(inputLine);
sPrefix = inputLine.substring(0, 1);
sPrefix = sPrefix.trim();
//System.out.println("*** Prefix : "+sPrefix);
sBuf=inputLine.substring(2).trim();
System.out.println("sBuf : "+sBuf);
if(sBuf.length() > 0)
{
decNumber = Integer.parseInt(sBuf);
byte btMarks[] = sPrefix.getBytes();
byte btMark = btMarks[0];
if(btMark == 'S')
{
//- System ausschalten **********************************************
System.out.println( "***** S ****"+decNumber);
if(!(decNumber == 1))
System.exit(0);
}
}
}
}
catch (Exception e) {
System.out.println( "STEP 3 Fehler..."+e.toString());
e.printStackTrace();
success_flag = false;
}
comPort.closePort();
}
}