![](/media/cache/profiles/94aef38441efa3380a3bed3faf1f9d5d.50x50_q85.jpg)
Diff: RTno/ReceivePacket.cpp
- Revision:
- 0:b14546a3cfab
- Child:
- 1:b96a6ff9bb6f
diff -r 000000000000 -r b14546a3cfab RTno/ReceivePacket.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RTno/ReceivePacket.cpp Fri Jul 29 11:23:06 2011 +0000 @@ -0,0 +1,78 @@ +/******************************************* + * ReceivePacket.h + * @author Yuki Suga + * @copyright Yuki Suga (ysuga.net) Nov, 10th, 2010. + * @license LGPLv3 + *****************************************/ +/******************************************** +It changed a little for mbed. +The serial buffer is not used. +2011/7/27 @nucho +********************************************/ +#include "ReceivePacket.h" +#include "Serial.h" +//#include <HardwareSerial.h> +//#include <WConstants.h> + +int ReceivePacket(char* packet) { + int counter = 0; + unsigned char sum = 0; + /* + while (Serial.available() < PACKET_HEADER_SIZE) { + delayMicroseconds(PACKET_WAITING_DELAY); + counter++; + if (counter == PACKET_WAITING_COUNT) { + return -TIMEOUT; + } + }*/ + // char host = Serial.read(); + // char target = Serial.read(); + + packet[INTERFACE] = pc.getc(); + sum += packet[INTERFACE]; + + packet[DATA_LENGTH] = pc.getc(); + sum += packet[DATA_LENGTH]; + + /* + while (Serial.available() < packet[DATA_LENGTH]+1) { + //delayMicroseconds(PACKET_WAITING_DELAY); + wait_us(PACKET_WAITING_DELAY); + counter++; + if (counter == PACKET_WAITING_COUNT) { + return -DATA_TIMEOUT; + } + }*/ + + for (int i = 0; i < packet[DATA_LENGTH]; i++) { + packet[PACKET_HEADER_SIZE+i] = pc.getc(); + sum += packet[PACKET_HEADER_SIZE+i]; + } + packet[PACKET_HEADER_SIZE+packet[DATA_LENGTH]] = pc.getc(); + if (sum != packet[PACKET_HEADER_SIZE+packet[DATA_LENGTH]]) { + return -CHECKSUM_ERROR; + } + return PACKET_HEADER_SIZE + packet[DATA_LENGTH] + 1; +} + +/* +int read_string(char* buffer, int flag=1) { + int counter = 0; + for(int i = 0;i < 100;i++) { + if(Serial.available() > 0) { + buffer[counter] = Serial.read(); + if(buffer[counter] == '^') { + buffer[counter] = 0; + Serial.print("AO^"); + return counter+1; + } + counter++; + } + delayMicroseconds(100); + } + if(flag) + Serial.print("AE^"); + return -1; +} + +*/