void parserRead() { currentTime = millis()/1000.0-lastTime; readSerial(); if(isAvailable) { unsigned char c = serialRead & 0xff; if((c == 0x55) && (isStart == false)) { if(prevc == 0xff) { index=1; isStart = true; } } else { prevc = c; if(isStart) { if(index == 2) { dataLen = c; } else if(index > 2) { dataLen--; } writeBuffer(index,c); } } index++; if(index > 51) { index=0; isStart=false; } if(isStart && (dataLen == 0) && (index > 3)) { isStart = false; parseData(); index=0; } } } unsigned char readBuffer(int index) { if(BluetoothSource == DATA_SERIAL) { return buffer[index]; } else if(BluetoothSource == DATA_SERIAL2) { return bufferBt1[index]; } else if(BluetoothSource == DATA_SERIAL3) { return bufferBt2[index]; } } void writeBuffer(int index,unsigned char c) { if(BluetoothSource == DATA_SERIAL) { buffer[index]=c; } else if(BluetoothSource == DATA_SERIAL2) { bufferBt1[index]=c; } else if(BluetoothSource == DATA_SERIAL3) { bufferBt2[index]=c; } } void writeSerial(unsigned char c) { Serial.write(c); Serial2.write(c); Serial3.write(c); } void readSerial(void) { isAvailable = false; BluetoothSource = DATA_SERIAL; if(Serial.available() > 0) { isAvailable = true; BluetoothSource = DATA_SERIAL; serialRead = Serial.read(); } if(Serial2.available() > 0) { isAvailable = true; BluetoothSource = DATA_SERIAL2; serialRead = Serial2.read(); } if(Serial3.available() > 0) { isAvailable = true; BluetoothSource = DATA_SERIAL3; serialRead = Serial3.read(); } } void writeHead(void) { writeSerial(0xff); writeSerial(0x55); } void writeEnd(void) { Serial.println(); Serial2.println(); Serial3.println(); } /* ff 55 len idx action device port slot data a 0 1 2 3 4 5 6 7 8 */ void parseData(void) { isStart = false; int idx = readBuffer(3); int action = readBuffer(4); int device = readBuffer(5); switch(action) { case GET: { writeHead(); writeSerial(idx); readSensor(device); writeEnd(); } break; case RUN: { runModule(device); //callOK(); } break; case RESET: { //reset resetAll(); } break; case START: { //start callOK(); } break; } } void callOK(void) { writeSerial(0xff); writeSerial(0x55); writeEnd(); } void sendByte(char c) { writeSerial(1); writeSerial(c); } void sendString(String s) { int l = s.length(); writeSerial(4); writeSerial(l); for(int i=0;i 15) { break; } _receiveUint8[i] = readBuffer(idx+i); } return _receiveUint8; }