2

I want to write some hex data to the serial port and read back some data. I have a problem in the reading (reads nothing) could someone help???

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

public class Test {
    public static void main(String[] args) throws IOException {
        char c='s';
        ProcessBuilder builder = new ProcessBuilder("c:\\windows\\system32\\mode.com", "portname", 
            "baud=115200", "parity=n", "data=8","stop=1");
        Map<String, String> environ = builder.environment();
        String portname="com6";
        final Process process = builder.start();
        String x="";
        byte data[] = {(byte)0xF5, (byte)0xfa, (byte)0x01, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0xfe, (byte)0x0f};
        FileOutputStream fos = new FileOutputStream(portname );
        BufferedOutputStream bos = new BufferedOutputStream( fos );
        fos.flush();
        fos.write( data);
        fos.close();
        bos.close();
        FileInputStream fstream1 = new FileInputStream(portname);
        DataInputStream in = new DataInputStream(fstream1);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        int n = 0;
        while ((n =(char) in.read()) !=-1 ){
            System.out.println((char)fstream1.read());
        }
    }
}
2

1 Answer 1

0

You pass in a string "portname" istead of the variable portname. See the following change to the code:

String portname="com6"; //Move it here
ProcessBuilder builder = new ProcessBuilder("c:\\windows\\system32\\mode.com", 
             portname, //instead of "portname"
           "baud=115200", "parity=n", "data=8","stop=1");
Map<String, String> environ = builder.environment();

After that change, I had my system block on the read() - because I have nothing connected to that port.

You read from both streams - and that way you skip bytes. Try it with just the most basic streams first:

 try{
   FileOutputStream fos = new FileOutputStream(portname ); 
   fos.write( data);
   fos.flush();
   fos.close();

   FileInputStream    fstream1 = new FileInputStream(portname);
   int n = in.read();
   while (n != -1 ){
       System.out.println(n);
       n = in.read();
   }
   System.out.println("Reading ended");
 } catch(Exception e) {
    e.printStackTrace();
 }

That should at least get back what you've written.

Sign up to request clarification or add additional context in comments.

5 Comments

well all that code does is write some bytes to a file and then read it back though - doesn't connect to serial port in any way
Check again please - with the try-catch around your file logic. I think there might be an Exception involved
and again no error, no nothing? can you share updated code?
It just reads -1 the end of the stream no other data are read
See my latest edit - you might not even be connected to the port.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.