Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Java code. Requires the gnu.io.rxtx library for serial capabilities.
- import gnu.io.CommPortIdentifier;
- import gnu.io.SerialPort;
- import gnu.io.SerialPortEvent;
- import gnu.io.SerialPortEventListener;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.util.Enumeration;
- import java.util.TooManyListenersException;
- public class Arduino {
- private SerialPort serialPort;
- private InputStream input;
- private OutputStream output;
- public Arduino(String portname, int baud) throws Exception {
- CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM5");
- if (portIdentifier.isCurrentlyOwned()) {
- throw new Exception("Port currently in use.");
- } else {
- serialPort = (SerialPort)portIdentifier.open("Java", 2000);
- serialPort.setSerialPortParams(baud, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
- input = serialPort.getInputStream();
- output = serialPort.getOutputStream();
- Thread.sleep(2000);
- }
- }
- public synchronized int hasavailable() throws IOException {
- return input.available();
- }
- public synchronized byte[] read(int size) throws IOException {
- byte[] data = new byte[size];
- input.read(data);
- return data;
- }
- public synchronized boolean write(String data) {
- return write(data.getBytes());
- }
- public synchronized boolean write(byte data) {
- return write(new byte[] {data});
- }
- public synchronized boolean write(byte[] data) {
- try {
- output.write(data);
- output.flush();
- } catch (IOException e) {
- return false;
- }
- return true;
- }
- public void attachSerialListener(SerialPortEventListener l) throws TooManyListenersException {
- serialPort.addEventListener(l);
- }
- public synchronized void close() throws IOException {
- serialPort.removeEventListener();
- serialPort.close();
- input.close();
- output.close();
- }
- public static void main(String[] args) {
- try {
- final Arduino board = new Arduino("COM5", 9600);
- Thread.sleep(2000);
- BufferedWriter rom = new BufferedWriter(new FileWriter("R-TYPE DX.gb"));
- board.write((byte)0);
- System.out.println("Buffering...");
- Thread.sleep(1000);
- System.out.print("Reading...");
- while (board.hasavailable() > 0)
- rom.write(new String(board.read(board.hasavailable())));
- System.out.println("Done.");
- rom.flush();
- rom.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.exit(0);
- }
- }
- Arduino code - simple burn and leave.
- void setup() {
- Serial.begin(9600);
- }
- void loop() {
- if (Serial.available() > 0)
- if (Serial.read() == 0)
- for (unsigned int b=0; b<256; b++)
- Serial.write(b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement