Java and XON XOFF serial print - java

I m build a java code to use xon xoff protocol to print a fiscal document with cash register Custom K3. This is the setting of this cash register.
This is the code about DriverPrinterSeriale.java
package prove;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.mcsolution.common.LoggerFactory.MyLog4J;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
public class DriverPrinterSeriale {
private SerialPort serialPort;
private OutputStream outStream;
private InputStream inStream;
public static MyLog4J log;
public DriverPrinterSeriale(String portaCOM){
log = new MyLog4J();
try {
this.connect(portaCOM);
} catch (IOException e) {
log.logStackTrace(e);
}
}
public void apriCassetto(){
try {
outStream = serialPort.getOutputStream();
//inStream = serialPort.getInputStream();
//log.information("output acquisito ");
String messageString = "a";
outStream.write(messageString.getBytes());
log.information("cassetto aperto");
} catch (Exception e) {
log.logStackTrace(e);
}
}
public void provaScontrino(String codiceDaInviare){
try {
outStream = serialPort.getOutputStream();
//inStream = serialPort.getInputStream();
log.information("output acquisito ora provo a stampare uno scontrino");
//String messageString = "j";
//outStream.write(messageString.getBytes());
//messageString = "'PANTALONE'10H1R";
//outStream.write(messageString.getBytes());
//String messageString = "\"MAGLIA. 1\"100H1R";
outStream.write(codiceDaInviare.getBytes());
//messageString = "1T";
//outStream.write(messageString.getBytes());
//messageString = "J";
//outStream.write(messageString.getBytes());
log.information("scontrino stampato ora apro il cassetto");
//apriCassetto();
} catch (Exception e) {
log.logStackTrace(e);
}
}
public void connect(String portName) throws IOException {
try {
// Obtain a CommPortIdentifier object for the port you want to open
CommPortIdentifier portId =
CommPortIdentifier.getPortIdentifier(portName);
log.information("apro porta seriale");
//System.out.println("apro porta seriale");
// Get
serialPort =
(SerialPort) portId.open("Demo application", 5000);
// Set the parameters of the connection.
setSerialPortParameters();
log.information("settaggio porta terminato");
} catch (NoSuchPortException e) {
log.logStackTrace(e);
throw new IOException(e.getMessage());
} catch (PortInUseException e) {
log.logStackTrace(e);
throw new IOException(e.getMessage());
} catch (IOException e) {
log.logStackTrace(e);
serialPort.close();
throw e;
}
}
/**
* Get the serial port input stream
* #return The serial port input stream
*/
public InputStream getSerialInputStream() {
return inStream;
}
/**
* Get the serial port output stream
* #return The serial port output stream
*/
public OutputStream getSerialOutputStream() {
return outStream;
}
/**
* Sets the serial port parameters
*/
private void setSerialPortParameters() throws IOException {
int baudRate = 19200; // 57600bps
//int baudRate = 38400; // 57600bps
try {
// Set serial port to 57600bps-8N1..my favourite
serialPort.setSerialPortParams(
baudRate,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE
);
log.information("settaggio porta iniziato");
serialPort.setFlowControlMode(
SerialPort.FLOWCONTROL_NONE);
log.information("settaggio porta eseguito");
} catch (UnsupportedCommOperationException ex) {
log.logStackTrace(ex);
throw new IOException("Unsupported serial port parameter");
}
}
}
This is the code to print a fiscal document:
driverSeriale = new DriverPrinterSeriale("COM3");
String rigaDaStampare = "\"Pippo"+
"\"10"+"*1"+"H1R";
driverSeriale.provaScontrino(rigaDaStampare);
I have no error but I m not able to print fiscal document. nothing comes out of the cash register

I found a copy of your printer manual located here. Page 40 lists the printer parameters supported. From your description, it seems like there is a mismatch of parameters. First, I would try setting it to something other than CUSTOM XON-XOFF and see if the printer will work with hardware flow control. That shouldn't be a problem with today's equipment and may make this work out of the box.
If you can't change to hardware flow control, then I think you need to set flowcontrol in your serial port. Here is a link to Java documentation on setFlowControlMode. From the current configuration, it appears you might want to logical or include both FLOWCONTROL_XONXOFF_IN and FLOWCONTROL_XONXOFF_OUT.

Related

Reading data from embedded controller using COM port

we are receiving data from controller using terminal software and as well as also java program ..
but think is that we get proper data on terminal software but java program can not show like terminal .. actually data come in ASCII and we convert to HEX terminal application show proper data but java program can not show terminal data.....
java program data
import java.io.*; // IOException
import java.util.*; // Scanner
import jssc.*;
/**
*
* #author Emiliarge
*/
public class ComPortSendReceive1 {
private static SerialPort serialPort;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
String[] portNames = SerialPortList.getPortNames();
if (portNames.length == 0) {
System.out.println("There are no serial-ports :( You can use an emulator, such ad VSPE, to create a virtual serial port.");
System.out.println("Press Enter to exit...");
try {
System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return;
}
// выбор порта
System.out.println("Available com-ports:");
for (int i = 0; i < portNames.length; i++){
System.out.println(portNames[i]);
}
System.out.println("Type port name, which you want to use, and press Enter...");
Scanner in = new Scanner(System.in);
String portName = in.next();
// writing to port
serialPort = new SerialPort(portName);
try {
// opening port
serialPort.openPort();
serialPort.setParams(SerialPort.BAUDRATE_9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN |
SerialPort.FLOWCONTROL_RTSCTS_OUT);
serialPort.addEventListener(new PortReader(), SerialPort.MASK_RXCHAR);
// writing string to port
serialPort.writeString("Hurrah!!!");
System.out.println("String wrote to port, waiting for response..");
}
catch (SerialPortException ex) {
System.out.println("Error in writing data to port: " + ex);
}
}
// receiving response from port
private static class PortReader implements SerialPortEventListener {
#Override
public void serialEvent(SerialPortEvent event) {
if(event.isRXCHAR() && event.getEventValue() > 0) {
try {
// получение ответа от порта
String receivedData = serialPort.readString(event.getEventValue());
for (char ch : receivedData.toCharArray()) {
System.out.format("%H ", ch);
} // prints "31 32 2E 30 31 33 "
}
catch (SerialPortException ex) {
System.out.println("Error in receiving response from port: " + ex);
}
}
}
}
}

Arduino Uno in Proteus send � character

I'm writing a java program to read data from COM3 port. Data sent by Arduino [1]: https://imgur.com/a/MiNfTZN
But in Java Program (I'm using JSSC) it return � character. If I use RXTX, it return java.io.IOException: Underlying input stream returned zero bytes. I wonder how I can fix it?
This is my code
Using JSSC:
public class SerialTestJSSC {
private static SerialPort serialPort;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
String[] portNames = SerialPortList.getPortNames();
if (portNames.length == 0) {
System.out.println("There are no serial-ports :( You can use an emulator, such ad VSPE, to create a virtual serial port.");
System.out.println("Press Enter to exit...");
try {
System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return;
}
// выбор порта
System.out.println("Available com-ports:");
for (int i = 0; i < portNames.length; i++){
System.out.println(portNames[i]);
}
System.out.println("Type port name, which you want to use, and press Enter...");
Scanner in = new Scanner(System.in);
String portName = in.next();
// writing to port
serialPort = new SerialPort(portName);
try {
// opening port
serialPort.openPort();
serialPort.setParams(SerialPort.BAUDRATE_9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN |
SerialPort.FLOWCONTROL_RTSCTS_OUT);
serialPort.addEventListener(new PortReader(), SerialPort.MASK_RXCHAR);
// writing string to port
serialPort.writeString("Hurrah!!!");
System.out.println("String wrote to port, waiting for response..");
}
catch (SerialPortException ex) {
System.out.println("Error in writing data to port: " + ex);
}
}
// receiving response from port
private static class PortReader implements SerialPortEventListener {
#Override
public void serialEvent(SerialPortEvent event) {
if(event.isRXCHAR() && event.getEventValue() > 0) {
try {
// получение ответа от порта
String receivedData = serialPort.readString(event.getEventValue());
System.out.println("Received response from port: " + receivedData);
}
catch (SerialPortException ex) {
System.out.println("Error in receiving response from port: " + ex);
}
}
}
}
Using RXTX
public class SerialTest implements SerialPortEventListener {
SerialPort serialPort;
/** The port we're normally going to use. */
private static final String PORT_NAMES[] = {
"COM3", // Windows
};
/**
* A BufferedReader which will be fed by a InputStreamReader
* converting the bytes into characters
* making the displayed results codepage independent
*/
private static BufferedReader input;
/** The output stream to the port */
private OutputStream output;
/** Milliseconds to block while waiting for port open */
private static final int TIME_OUT = 2000;
/** Default bits per second for COM port. */
private static final int DATA_RATE = 9600;
public void initialize() {
// the next line is for Raspberry Pi and
// gets us into the while loop and was suggested here was suggested http://www.raspberrypi.org/phpBB3/viewtopic.php?f=81&t=32186
//System.setProperty("gnu.io.rxtx.SerialPorts", "/dev/ttyACM0");
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
//First, Find an instance of serial port as set in PORT_NAMES.
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) {
portId = currPortId;
break;
}
}
}
if (portId == null) {
System.out.println("Could not find COM port.");
return;
}
try {
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),TIME_OUT);
// set port parameters
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
// open the streams
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
output = serialPort.getOutputStream();
// add event listeners
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
System.err.println(e.toString());
}
}
/**
* This should be called when you stop using the port.
* This will prevent port locking on platforms like Linux.
*/
public synchronized void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}
/**
* Handle an event on the serial port. Read the data and print it.
*/
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
String inputLine=input.readLine();
System.out.println("System "+inputLine);
} catch (Exception e) {
System.err.println(e.toString());
}
}
// Ignore all the other eventTypes, but you should consider the other ones.
}
public static void main(String[] args) throws Exception {
SerialTest main = new SerialTest();
main.initialize();
Thread t=new Thread() {
public void run() {
try {
Thread.sleep(1000000);} catch (InterruptedException ie) {}
}
};
t.start();
System.out.println("Started");
}}

Exception ( java.io.IOException: Underlying input stream returned zero bytes ) while reading from Input Stream Serial Port Java

I am trying to read Input-Stream from Serial Port but getting an exception "java.io.IOException: Underlying input stream returned zero bytes"
Here is the code :
package communication;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import org.apache.commons.io.IOUtils;
public class Serial_0306 implements SerialPortEventListener {
public SerialPort serialPort;
private static final String PORT_NAMES[] = {
"COM3", // Windows
};
enter code herepublic static BufferedReader input;
public static OutputStream output;
public static InputStream inputStream;
public static final int TIME_OUT = 2000;
public static final int DATA_RATE = 9600;
public void initialize() {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) {
portId = currPortId;
break;
}
}
}
if (portId == null) {
System.out.println("Could not find COM port.");
return;
}
try {
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
inputStream = serialPort.getInputStream();
String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8); // Exception over here
System.out.println("Result ::" + result);
output = serialPort.getOutputStream();
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
System.err.println(e.toString());
}
}
public synchronized void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}
#Override
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
System.out.println("Result in Serial Event::" + result);
} catch (Exception e) {
System.err.println(e.toString());
}
}
}
public static synchronized void writeData(String data) {
System.out.println("Sent: " + data);
try {
output.write(data.getBytes());
System.out.println("Received DATA BYTES");
} catch (Exception e) {
System.out.println("could not write to port");
}
}
public static void main(String[] args) throws Exception {
Serial_0306 main = new Serial_0306();
main.initialize();
Thread t = new Thread() {
public void run() {
//the following line will keep this app alive for 1000 seconds,
//waiting for events to occur and responding to them (printing incoming messages to console).
try {
Thread.sleep(1500);
writeData("CP");
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
};
t.start();
System.out.println("Started");
}
}
**OUTPUT :
Stable Library
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
Started
java.io.IOException: Underlying input stream returned zero bytes
Sent: CP
could not write to port**
My actual need is to get input Stream from Serial Port and convert it to String.
But for testing purpose I am myself sending output stream and trying to read the same..But whenever I try to read input Stream I get the Exception.
Is my code correct? What else I need to do to get proper Input Stream and convert it to String.

Serialport read write in java

I have an application where i need to write some data to the serial port and get the response from it. Now the sequence is as follows:
Send a message to the serial port.
wait for the response
Once the response is received i have to process the message and until then nothing should happen to the application.
How should i break this problem and solve it ? I am not good in serial programming. I have tried a code but my application halts when i execute the send message. May be because i am reading the message just after sending a message. I really don't know hot to break this problem down. Do i have to start listen to the port during the application start ? Do i have to keep the port open ? Is it ok if i open the port every time i need that ? and how should i make the program wait to respond until the response message is read form the port ? Please help me solve this problem..
--EDIT--
package testConn;
import forms_helper.global_variables;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.comm.*;
import java.util.*;
/** Check each port to see if it is open. **/
public class openPort implements SerialPortEventListener {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString;
public static SerialPort serialPort;
static OutputStream outputStream;
InputStream inputStream;
static boolean outputBufferEmptyFlag = false;
public void open() {
Enumeration port_list = CommPortIdentifier.getPortIdentifiers();
while (port_list.hasMoreElements()) {
// Get the list of ports
CommPortIdentifier port_id = (CommPortIdentifier) port_list.nextElement();
if (port_id.getName().equals("/dev/ttyS1")) {
// Attempt to open it
try {
SerialPort port = (SerialPort) port_id.open("PortListOpen", 20);
System.out.println("Opened successfully");
try {
int baudRate = 9600; //
port.setSerialPortParams(
baudRate,
SerialPort.DATABITS_7,
SerialPort.STOPBITS_1,
SerialPort.PARITY_EVEN);
port.setDTR(true);
/*
port.setFlowControlMode(
SerialPort.FLOWCONTROL_NONE);
*
*/
System.out.println("properties are set");
} catch (UnsupportedCommOperationException e) {
System.out.println(e);
}
try {
//input = new SerialReader(in);
port.addEventListener(this);
System.out.println("listeners attached" + this);
} catch (TooManyListenersException e) {
System.out.println("too many listeners");
}
port.notifyOnDataAvailable(true);
//port.notifyOnOutputEmpty(true);
//sendMessage(port,"#PL");
//port.close ();
try {
inputStream = port.getInputStream();
System.out.println("inputstream" + inputStream.available());
outputStream = (OutputStream) port.getOutputStream();
} catch (IOException e) {
System.out.println(e);
}
//set the created variables to global variables
global_variables.port = port;
global_variables.inputStream = inputStream;
global_variables.outputStream = outputStream;
} catch (PortInUseException pe) {
System.out.println("Open failed");
String owner_name = port_id.getCurrentOwner();
if (owner_name == null) {
System.out.println("Port Owned by unidentified app");
} else // The owner name not returned correctly unless it is
// a Java program.
{
System.out.println(" " + owner_name);
}
}
}
}
}
public static void sendMessage(SerialPort port, String msg) {
if (port != null) {
try {
global_variables.outputStream.write(msg.getBytes());
global_variables.outputStream.flush();
try {
Thread.sleep(2000); // Be sure data is xferred before closing
System.out.println("read called");
//SimpleRead read = new SimpleRead();
//int read = global_variables.inputStream.read();
//System.out.println("read call ended"+read);
} catch (Exception e) {
}
} catch (IOException ex) {
Logger.getLogger(openPort.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void serialEvent(SerialPortEvent event) {
System.out.println(event.getEventType());
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:
System.out.println("event.getEventType()");
break;
*
*/
case SerialPortEvent.DATA_AVAILABLE:
System.out.println("inside event handler data available");
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
System.exit(1);
} catch (IOException e) {
System.out.println(e);
}
break;
}
}
}
What is wrong with this code ? I cant see the response from the terminal connected to serialport when i send a polling message. I am opening the connection at my application start and i am sending the message when a button inside my application is clicked. How do i solve the problem ??
See this wikibook, section Event Driven Serial Communication.
Previously answered question, here: How do I get Java to use the serial port in Linux?
Depending on your platform (if you are on a *NIX box) you can often get away with using stty to set the baud rate/port settings, and then simply open the /dev/tty* port with a FileInputStream/FileOutputStream. I used to have a chunk of code that did that and worked quite reliably, but now that I go looking for it it appears to have been misplaced. Sad.
Try to retryger the tx again after than the once and when keep the Rx on read for debugg the came back message from the device

Reading file from serial port in Java

i'm beginner in java technology, I have to read file from port. Frst I'll write "FLASH" to outputstream then I'll get response as a "FLASH_OK" from target device, after getting FLASH_OK as response then again i have to write name of the file which i want,but problem is its not writing file name to outputstream, below is my code. Please help me.
package writeToPort;
import java.awt.Toolkit;
import java.io.*;
import java.util.*;
import javax.comm.*;
import javax.swing.JOptionPane;
import constants.Constants;
public class Flashwriter implements SerialPortEventListener {
Enumeration portList;
CommPortIdentifier portId;
String messageString = "\r\nFLASH\r\n";
SerialPort serialPort;
OutputStream outputStream;
InputStream inputStream;
Thread readThread;
String one, two;
String test = "ONLINE";
String[] dispArray = new String[1];
int i = 0;
byte[] readBufferArray;
int numBytes;
String response;
FileOutputStream out;
final int FLASH = 1, FILENAME = 2;
int number;
File winFile;
public static void main(String[] args) throws IOException {
Flashwriter sm = new Flashwriter();
sm.FlashWriteMethod();
}
public void FlashWriteMethod() throws IOException {
portList = CommPortIdentifier.getPortIdentifiers();
winFile = new File("D:\\testing\\out.FLS");
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM2")) {
try {
serialPort = (SerialPort) portId.open("SimpleWriteApp",
1000);
} catch (PortInUseException e) {
}
try {
inputStream = serialPort.getInputStream();
System.out.println(" Input Stream... " + inputStream);
} catch (IOException e) {
System.out.println("IO Exception");
}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
System.out.println("Tooo many Listener exception");
}
serialPort.notifyOnDataAvailable(true);
try {
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
} catch (IOException e) {
}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort
.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
number = FLASH;
sendRequest(number);
} catch (UnsupportedCommOperationException e) {
}
}
}
}
}
public void serialEvent(SerialPortEvent event) {
SerialPort port = (SerialPort) event.getSource();
switch (event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
try {
if (inputStream.available() > 0) {
numBytes = inputStream.available();
readBufferArray = new byte[numBytes];
int readBytes = inputStream.read(readBufferArray);
one = new String(readBufferArray);
System.out.println("readBytes " + one);
}
if (one.indexOf("FLASH_") > -1 & !(one.indexOf("FLASH_F") > -1)) {
System.out.println("got message");
response = "FLASH_OK";
number = FILENAME;
sendRequest(number);
}
out = new FileOutputStream(winFile, true);
out.write(readBufferArray);
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
readBufferArray = null;
// break;
}
}
public void sendRequest(int num) {
switch (num) {
case FLASH:
try {
outputStream.write(messageString.getBytes());
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
break;
case FILENAME:
try {
outputStream.write("\r\n26-02-08.FLS\r\n".getBytes());
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
Have you tested with a Serial Port Emulator software?
When I did this kind of app for college, our professor told us to build the app and test it using an emulator, since it's much cheaper and less error prone.
While searching for google you can find some softwares that do that. I don't remember exactly the one we used at that time, but it worked quite well.
I mean products like this: Eterlogic - Virtual Serial Ports Emulator, but that's just an example (and I haven't tested this software, I just googled it)
You are erroneously assuming that full messages are always going to be received. Instead, when a serial event is triggered, only part of the message may be available. For example, you may get an event and read "FLAS" and a subsequent event will give "H_OK". You need to adapt your code to something like this:
// member variables
byte [] receiveBuffer = new byte[BUFFER_LENGTH];
int receiveIndex = 0;
// Receive code
receiveIndex +=
inputStream.read(receiveBuffer, receiveIndex, BUFFER_LENGTH - receiveIndex);
Sorry I can't help you with the Java code but are you sure the data "FLASH" is
actually being sent on your serial port ? When I'm having this kind of problem I usually use an oscilloscope to look on the TX pin on the serial port and check if I can "see" the data being sent (the data burst will be brief but you will be able to see it). If you
can see it use the scope to look on the RX pin of the serial port and see if you can
see the "FLASH_OK" response actually being sent.
Nine out of ten times the problem isn't the software but a hardware issue often due to handshaking pins being incorrectly connected.
Good luck with this.

Categories