Advertisement
Guest User

Serial or java string conversion bug?

a guest
Jun 13th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.83 KB | None | 0 0
  1. Java code. Requires the gnu.io.rxtx library for serial capabilities.  
  2.  
  3. import gnu.io.CommPortIdentifier;
  4. import gnu.io.SerialPort;
  5. import gnu.io.SerialPortEvent;
  6. import gnu.io.SerialPortEventListener;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.BufferedWriter;
  10. import java.io.FileWriter;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.InputStreamReader;
  14. import java.io.OutputStream;
  15. import java.util.Enumeration;
  16. import java.util.TooManyListenersException;
  17.  
  18.  
  19. public class Arduino {
  20.     private SerialPort serialPort;
  21.     private InputStream input;
  22.     private OutputStream output;
  23.  
  24.     public Arduino(String portname, int baud) throws Exception {
  25.         CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM5");
  26.         if (portIdentifier.isCurrentlyOwned()) {
  27.             throw new Exception("Port currently in use.");
  28.         } else {
  29.             serialPort = (SerialPort)portIdentifier.open("Java", 2000);
  30.             serialPort.setSerialPortParams(baud, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
  31.  
  32.             input = serialPort.getInputStream();
  33.             output = serialPort.getOutputStream();
  34.  
  35.             Thread.sleep(2000);
  36.         }
  37.     }
  38.  
  39.     public synchronized int hasavailable() throws IOException {
  40.         return input.available();
  41.     }
  42.  
  43.     public synchronized byte[] read(int size) throws IOException {
  44.         byte[] data = new byte[size];
  45.         input.read(data);
  46.         return data;
  47.     }
  48.  
  49.     public synchronized boolean write(String data) {
  50.         return write(data.getBytes());
  51.     }
  52.     public synchronized boolean write(byte data) {
  53.         return write(new byte[] {data});
  54.     }
  55.     public synchronized boolean write(byte[] data) {
  56.         try {
  57.             output.write(data);
  58.             output.flush();
  59.         } catch (IOException e) {
  60.             return false;
  61.         }
  62.         return true;
  63.     }
  64.  
  65.     public void attachSerialListener(SerialPortEventListener l) throws TooManyListenersException {
  66.         serialPort.addEventListener(l);
  67.     }
  68.  
  69.     public synchronized void close() throws IOException {
  70.         serialPort.removeEventListener();
  71.         serialPort.close();
  72.         input.close();
  73.         output.close();
  74.     }
  75.  
  76.     public static void main(String[] args) {
  77.         try {
  78.             final Arduino board = new Arduino("COM5", 9600);
  79.             Thread.sleep(2000);
  80.             BufferedWriter rom = new BufferedWriter(new FileWriter("R-TYPE DX.gb"));
  81.             board.write((byte)0);
  82.             System.out.println("Buffering...");
  83.             Thread.sleep(1000);
  84.             System.out.print("Reading...");
  85.             while (board.hasavailable() > 0)
  86.                 rom.write(new String(board.read(board.hasavailable())));
  87.             System.out.println("Done.");
  88.             rom.flush();
  89.             rom.close();
  90.         } catch (Exception e) {
  91.             e.printStackTrace();
  92.         }
  93.         System.exit(0);
  94.     }
  95. }
  96.  
  97. Arduino code - simple burn and leave.
  98. void setup() {
  99.     Serial.begin(9600);
  100. }
  101.  
  102.  
  103. void loop() {
  104.     if (Serial.available() > 0)
  105.         if (Serial.read() == 0)
  106.           for (unsigned int b=0; b<256; b++)
  107.               Serial.write(b);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement