lxbzmy / java-simple-serial-connector

Automatically exported from code.google.com/p/java-simple-serial-connector
0 stars 0 forks source link

readBytes() wrong when get second message #74

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. establish connection to serial device
2. read from it
3. show received message
3. disconnect the serial device
4. go back to step one 

What is the expected output? What do you see instead?
Expected: show the message "3080"
What I see: for the first time of loop i get "3080", after this every step i 
get "R\r" some like encode error 

What version of the product are you using? On what operating system?
JSSC v 2.6, x64. 64 bit windows 7, JDK1.6.

Please provide any additional information below.

Here is my code:

Main:

public static void main(String[] args) {
    ArrayList<String> ports = new ArrayList<String>();
    ports.add("COM3");
    Controller control = new Controller(ports);
    try {
       while(true){ 
       String message = new String();
       message = control.getId("COM3");
       System.out.println(message);
       Thread.sleep(2000);
       }
    } catch (Exception e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }
}

Controller

public String getId(String portName) throws Exception {

        boolean flag = false;
        int i = 0;
        String id = new String();

        while(!flag){

            if (device.get(i).getPortName().equals(portName)) {
                device.get(i).connect();

                byte[] msg = new byte[3];
                msg[0] = (byte) 0x0d;
                msg[1] = (byte) 0x31;
                msg[2] = (byte) 0x0d;

                device.get(i).sendBytes(msg);
                flag = true;

                Thread.sleep(500);

                byte[] buffer = device.get(i).readBytes(4);
                id = new String(buffer);

                device.get(i).disconnect();
            }       
            i++;
        }

        return id;
    }

Robot Code on C language:

e_bt_tranparent_mode();
char received[32];
char buffer3[4];
 while(1){
            i = 0;
            c = 0;
            do{
                if (e_getchar_uart1(&received[i])){
                    c=received[i];
            i++;
                    e_set_led(7,1);
                }

            }while (((char)c != '\n')&&((char)c != '\x0d'));
            received[i]='\0';

if(received[0] == '1'){
                e_set_led(4,1);
                sprintf(buffer3, "3040",2);
                e_send_uart1_char(buffer3, strlen(buffer3));
                while(e_uart1_sending());
                e_set_led(5,1);

            }

}

Original issue reported on code.google.com by edneydas...@gmail.com on 15 Nov 2014 at 10:11