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