Problem with multiple byte writes

39 views
Skip to first unread message

Lee Bethune

unread,
Jan 27, 2016, 11:09:11 AM1/27/16
to Pi4J
Trying to get serial communication to work between a Pi and a robot.

Serial writes work well with single bytes. Cannot get multiple byte writes to work. 
I am trying to duplicate Python script. See below. 
Most of the code was borrowed from Serial.Example.java

Can anyone tell me what is the problem as outlined below.

// START SNIPPET: serial-snippet

import java.util.Date;

import com.pi4j.io.serial.Serial;
import com.pi4j.io.serial.SerialDataEvent;
import com.pi4j.io.serial.SerialDataListener;
import com.pi4j.io.serial.SerialFactory;
import com.pi4j.io.serial.SerialPortException;

public class Song02 {
    
    public static void main(String args[]) throws InterruptedException {
                      
       // create an instance of the serial communications class
        final Serial serial = SerialFactory.createInstance();
        serial.open(Serial.DEFAULT_COM_PORT, 115200);

        Thread.sleep(2000);
        System.out.println("Safe irobot silent");
        Thread.sleep(2000);
      
        //Python code below works for single byte writes
        //ser.write('\x83')  # Assuming the robot is awake, start safe mode so we can hack.
        // Corresponding java code below compiles, runs, and works
        serial.write((byte)  0x83); 
        Thread.sleep(2000);

        //Python code below works well for multiple byte writes
        //python ser.write('\0x8c \0x00 \0x02 \0x1F \0x40 \0x37 \0x40') 
        //java code below compiles, runs, but does not work. Likely cockpit error.        
        byte[] songsheet={(byte) 0x8c, (byte) 0x00, (byte) 0x02, (byte) 0x1F, (byte) 0x40, (byte) 0x37, (byte) 0x40};
        serial.write((byte[])  songsheet);
}
}

// END SNIPPET: serial-snippet


Lee Bethune

unread,
Jan 27, 2016, 2:50:37 PM1/27/16
to Pi4J
Solved
Reply all
Reply to author
Forward
0 new messages