![](/media/cache/profiles/94aef38441efa3380a3bed3faf1f9d5d.50x50_q85.jpg)
RTno/ReceivePacket.cpp@0:3c49891bc39d, 2011-07-29 (annotated)
- Committer:
- nucho
- Date:
- Fri Jul 29 11:23:44 2011 +0000
- Revision:
- 0:3c49891bc39d
- Child:
- 1:7f0fc0d1f777
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nucho | 0:3c49891bc39d | 1 | /******************************************* |
nucho | 0:3c49891bc39d | 2 | * ReceivePacket.h |
nucho | 0:3c49891bc39d | 3 | * @author Yuki Suga |
nucho | 0:3c49891bc39d | 4 | * @copyright Yuki Suga (ysuga.net) Nov, 10th, 2010. |
nucho | 0:3c49891bc39d | 5 | * @license LGPLv3 |
nucho | 0:3c49891bc39d | 6 | *****************************************/ |
nucho | 0:3c49891bc39d | 7 | /******************************************** |
nucho | 0:3c49891bc39d | 8 | It changed a little for mbed. |
nucho | 0:3c49891bc39d | 9 | The serial buffer is not used. |
nucho | 0:3c49891bc39d | 10 | 2011/7/27 @nucho |
nucho | 0:3c49891bc39d | 11 | ********************************************/ |
nucho | 0:3c49891bc39d | 12 | #include "ReceivePacket.h" |
nucho | 0:3c49891bc39d | 13 | #include "Serial.h" |
nucho | 0:3c49891bc39d | 14 | //#include <HardwareSerial.h> |
nucho | 0:3c49891bc39d | 15 | //#include <WConstants.h> |
nucho | 0:3c49891bc39d | 16 | |
nucho | 0:3c49891bc39d | 17 | int ReceivePacket(char* packet) { |
nucho | 0:3c49891bc39d | 18 | int counter = 0; |
nucho | 0:3c49891bc39d | 19 | unsigned char sum = 0; |
nucho | 0:3c49891bc39d | 20 | /* |
nucho | 0:3c49891bc39d | 21 | while (Serial.available() < PACKET_HEADER_SIZE) { |
nucho | 0:3c49891bc39d | 22 | delayMicroseconds(PACKET_WAITING_DELAY); |
nucho | 0:3c49891bc39d | 23 | counter++; |
nucho | 0:3c49891bc39d | 24 | if (counter == PACKET_WAITING_COUNT) { |
nucho | 0:3c49891bc39d | 25 | return -TIMEOUT; |
nucho | 0:3c49891bc39d | 26 | } |
nucho | 0:3c49891bc39d | 27 | }*/ |
nucho | 0:3c49891bc39d | 28 | // char host = Serial.read(); |
nucho | 0:3c49891bc39d | 29 | // char target = Serial.read(); |
nucho | 0:3c49891bc39d | 30 | |
nucho | 0:3c49891bc39d | 31 | packet[INTERFACE] = pc.getc(); |
nucho | 0:3c49891bc39d | 32 | sum += packet[INTERFACE]; |
nucho | 0:3c49891bc39d | 33 | |
nucho | 0:3c49891bc39d | 34 | packet[DATA_LENGTH] = pc.getc(); |
nucho | 0:3c49891bc39d | 35 | sum += packet[DATA_LENGTH]; |
nucho | 0:3c49891bc39d | 36 | |
nucho | 0:3c49891bc39d | 37 | /* |
nucho | 0:3c49891bc39d | 38 | while (Serial.available() < packet[DATA_LENGTH]+1) { |
nucho | 0:3c49891bc39d | 39 | //delayMicroseconds(PACKET_WAITING_DELAY); |
nucho | 0:3c49891bc39d | 40 | wait_us(PACKET_WAITING_DELAY); |
nucho | 0:3c49891bc39d | 41 | counter++; |
nucho | 0:3c49891bc39d | 42 | if (counter == PACKET_WAITING_COUNT) { |
nucho | 0:3c49891bc39d | 43 | return -DATA_TIMEOUT; |
nucho | 0:3c49891bc39d | 44 | } |
nucho | 0:3c49891bc39d | 45 | }*/ |
nucho | 0:3c49891bc39d | 46 | |
nucho | 0:3c49891bc39d | 47 | for (int i = 0; i < packet[DATA_LENGTH]; i++) { |
nucho | 0:3c49891bc39d | 48 | packet[PACKET_HEADER_SIZE+i] = pc.getc(); |
nucho | 0:3c49891bc39d | 49 | sum += packet[PACKET_HEADER_SIZE+i]; |
nucho | 0:3c49891bc39d | 50 | } |
nucho | 0:3c49891bc39d | 51 | packet[PACKET_HEADER_SIZE+packet[DATA_LENGTH]] = pc.getc(); |
nucho | 0:3c49891bc39d | 52 | if (sum != packet[PACKET_HEADER_SIZE+packet[DATA_LENGTH]]) { |
nucho | 0:3c49891bc39d | 53 | return -CHECKSUM_ERROR; |
nucho | 0:3c49891bc39d | 54 | } |
nucho | 0:3c49891bc39d | 55 | return PACKET_HEADER_SIZE + packet[DATA_LENGTH] + 1; |
nucho | 0:3c49891bc39d | 56 | } |
nucho | 0:3c49891bc39d | 57 | |
nucho | 0:3c49891bc39d | 58 | /* |
nucho | 0:3c49891bc39d | 59 | int read_string(char* buffer, int flag=1) { |
nucho | 0:3c49891bc39d | 60 | int counter = 0; |
nucho | 0:3c49891bc39d | 61 | for(int i = 0;i < 100;i++) { |
nucho | 0:3c49891bc39d | 62 | if(Serial.available() > 0) { |
nucho | 0:3c49891bc39d | 63 | buffer[counter] = Serial.read(); |
nucho | 0:3c49891bc39d | 64 | if(buffer[counter] == '^') { |
nucho | 0:3c49891bc39d | 65 | buffer[counter] = 0; |
nucho | 0:3c49891bc39d | 66 | Serial.print("AO^"); |
nucho | 0:3c49891bc39d | 67 | return counter+1; |
nucho | 0:3c49891bc39d | 68 | } |
nucho | 0:3c49891bc39d | 69 | counter++; |
nucho | 0:3c49891bc39d | 70 | } |
nucho | 0:3c49891bc39d | 71 | delayMicroseconds(100); |
nucho | 0:3c49891bc39d | 72 | } |
nucho | 0:3c49891bc39d | 73 | if(flag) |
nucho | 0:3c49891bc39d | 74 | Serial.print("AE^"); |
nucho | 0:3c49891bc39d | 75 | return -1; |
nucho | 0:3c49891bc39d | 76 | } |
nucho | 0:3c49891bc39d | 77 | |
nucho | 0:3c49891bc39d | 78 | */ |