// 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