public void checkStatus() { robotConnect++; if (robotConnect > 2) { robotConnect = 3; recv.put(new RobotEvent(EventEnum.ROBOT_EVENT_NET_STATUS_ERR, (short) 0, 0)); dis.changeRobotStatus(1); } }
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) { } } }