public void serialEvent(SerialPortEvent e) { // Determine type of event. switch (e.getEventType()) { case SerialPortEvent.BI: case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: try { while (m_in.available() > 0) { int b = m_in.read(); if (b == 0x000D) { // CR ASCII // Fin de lectura synchronized (this) { m_iStatusScale = SCALE_READY; notifyAll(); } } else if ((b > 0x002F && b < 0x003A) || b == 0x002E) { synchronized (this) { if (m_iStatusScale == SCALE_READY) { m_dWeightBuffer = 0.0; // se supone que esto debe estar ya garantizado m_dWeightDecimals = 1.0; m_iStatusScale = SCALE_READING; } if (b == 0x002E) { m_iStatusScale = SCALE_READINGDECIMALS; } else { m_dWeightBuffer = m_dWeightBuffer * 10.0 + b - 0x0030; if (m_iStatusScale == SCALE_READINGDECIMALS) { m_dWeightDecimals *= 10.0; } } } } else { // caracteres invalidos, reseteamos. m_dWeightBuffer = 0.0; // se supone que esto debe estar ya garantizado m_dWeightDecimals = 1.0; m_iStatusScale = SCALE_READY; } } } catch (IOException eIO) { } break; } }
public void run() { byte[] buf = new byte[256]; int cnt = 0; for (; ; ) { try { cnt = is.read(buf); // Thread.sleep(256); } catch (Exception e) { System.out.println(e); } if (cnt > 0) cntRcv += cnt; } }
/* * Read a single pair of (X,Y) values from the ADXL202EVB board. * TODO: Better return value, or declare as throwing exception for errs */ public static double[] chipRead() { double[] retval = {0.0, 0.0}; byte[] rawBuf = {0, 0, 0, 0}; int[] vals = {0, 0}; try { // Read is triggered by sending 'G' to board outputStream.write('G'); // Wait for all 4 result bytes to arrive // TODO: Finite wait w/error return if data never arrives while (inputStream.available() < 4) { Thread.sleep(1); } inputStream.read(rawBuf, 0, 4); // Convert from raw bytes to 16-bit signed integers, carefully Byte bTmp = new Byte(rawBuf[0]); vals[0] = bTmp.intValue() * 256; bTmp = new Byte(rawBuf[1]); vals[0] += bTmp.intValue(); bTmp = new Byte(rawBuf[2]); vals[1] = bTmp.intValue() * 256; bTmp = new Byte(rawBuf[3]); vals[1] += bTmp.intValue(); // See ADXL202EVB specs for details on conversion retval[0] = (((vals[0] / 100.0) - 50.0) / 12.5); retval[1] = (((vals[1] / 100.0) - 50.0) / 12.5); System.out.println("X: " + retval[0] + " Y: " + retval[1]); } catch (Exception ioe) { System.out.println("Error on data transmission"); } return (retval); }
// Deal with something robot has sent. public void serialEvent(SerialPortEvent events) { switch (events.getEventType()) { case SerialPortEvent.DATA_AVAILABLE: try { final byte[] buffer = new byte[1024]; int len = in.read(buffer); if (len > 0) { String line2 = new String(buffer, 0, len); Log("<span style='color:#FFA500'>" + line2.replace("\n", "<br>") + "</span>"); line3 += line2; // wait for the cue ("> ") to send another command if (line3.lastIndexOf(cue) != -1) { if (ConfirmPort()) { line3 = ""; SendFileCommand(); } } } } catch (IOException e) { } break; } }
public synchronized void serialEvent(SerialPortEvent oEvent) { if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { int available = input.available(); if (count < BUF_SIZE) { count = count + input.read( buf, count, (available > BUF_SIZE - count) ? BUF_SIZE - count : available); } start = newline; for (int i = newline; i <= count; i++) { if (buf[i] == 'U' || buf[i] == '$') { // start byte start = i; break; } } for (int i = start; i <= count; i++) { if (buf[i] == '\n') { // end byte newline = i; break; } } if (newline <= start) { if (count == BUF_SIZE) { // buf full an no valid datagram found, clear the buffer // need to copy the stuff after the start byte to the beginning if ((count - start) <= 128) { System.arraycopy(buf, start, buf, 0, count - start); count = count - start; start = 0; newline = 0; } else { // buf full and last start byte is more than max packet size clear all of the // buffer count = 0; // no need to copy since the buf is grabage start = 0; newline = 0; } } return; } if (buf[start] == '$') { buf[newline] = '\0'; System.out.println(new String(buf, start + 1, newline - start)); } // found valid datagram // parse datagram comma delimited buf[newline] = ','; String newbuf = new String(buf, start, newline - start); StringTokenizer st = new StringTokenizer(newbuf, ","); int[] data = new int[5]; int i = 0; while (st.hasMoreElements() && i < 5) { String temp = st.nextToken(); try { data[i] = Integer.parseInt(temp, 16); } catch (NumberFormatException e) { } i++; } RobotEvent ev = new RobotEvent(); int checksum; ev.setCommand(EventEnum.getEvent(data[1])); ev.setIndex((short) data[2]); ev.setValue((int) data[3]); checksum = data[4]; int checksum2 = (int) (((int) (ev.getCommand().getValue() & 0x000000FF) + (int) (ev.getIndex() & 0x000000FF) + (int) (ev.getValue() & 0x000000FF) + (int) ((ev.getValue() & 0x0000FF00) >> 8)) % 256); if (checksum2 == checksum) { // log event received recv.put(ev); } } catch (Exception e) { } } }