RTno is communicating library and framework which allows you to make your embedded device capable of communicating with RT-middleware world. RT-middleware is a platform software to realize Robotic system. In RTM, robots are developed by constructing robotics technologies\' elements (components) named RT-component. Therefore, the RTno helps you to create your own RT-component with your mbed and arduino. To know how to use your RTno device, visit here: http://ysuga.net/robot_e/rtm_e/rtc_e/1065?lang=en To know about RT-middleware and RT-component, visit http://www.openrtm.org

Dependencies:   EthernetNetIf

Dependents:   RTnoV3_LED RTnoV3_Template RTnoV3_ADC RTnoV3_Timer ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Transport.cpp Source File

Transport.cpp

00001 #define RTNO_SUBMODULE_DEFINE
00002 #include <stdint.h>
00003 #include "mbed.h"
00004 #include "Transport.h"
00005 #include "Packet.h"
00006 
00007 int8_t Transport_init()
00008 {
00009     return 0;
00010 }
00011 
00012 
00013 int8_t Transport_SendPacket(const char interface, const uint8_t data_length, const int8_t* packet_data) {
00014   uint8_t sum = 0;
00015   uint8_t sender[4] = {'U', 'A', 'R', 'T'};
00016   SerialDevice_putc(interface);
00017   sum += interface;
00018   SerialDevice_putc(data_length);
00019   sum += data_length;
00020 
00021   for(uint8_t i = 0;i < 4;i++) {
00022     sum += sender[i];
00023     SerialDevice_putc(sender[i]);
00024   }
00025 
00026   for(uint8_t i = 0;i < data_length;i++) {
00027     sum += packet_data[i];
00028     SerialDevice_putc(packet_data[i]);
00029   }
00030   SerialDevice_putc(sum);
00031   return PACKET_HEADER_SIZE + data_length + 1;
00032 }
00033 
00034 int8_t Transport_ReceivePacket(int8_t* packet) {
00035   uint8_t counter = 0;
00036   uint8_t sum = 0;
00037 
00038   if(SerialDevice_available() == 0) {
00039     return 0;
00040   }
00041 
00042   while(SerialDevice_available() < PACKET_HEADER_SIZE) {
00043     wait(PACKET_WAITING_DELAY/1000.0);
00044     counter++;
00045     if(counter == PACKET_WAITING_COUNT) {
00046       return -TIMEOUT;
00047     }
00048   }
00049   packet[INTERFACE] = SerialDevice_getc();
00050   sum += packet[INTERFACE];
00051   packet[DATA_LENGTH] = SerialDevice_getc();
00052   sum += packet[DATA_LENGTH];
00053 
00054   counter = 0;
00055   while(SerialDevice_available() < 4) {
00056     wait(PACKET_WAITING_DELAY/1000.0);
00057     counter++;
00058     if(counter == PACKET_WAITING_COUNT) {
00059       return -TIMEOUT;
00060     }
00061   }
00062   for(uint8_t i = 0;i < 4;i++) {
00063     uint8_t val = SerialDevice_getc();
00064     sum += val;
00065   }
00066 
00067   for(uint8_t i = 0;i < packet[DATA_LENGTH];i++) {
00068     counter = 0;
00069     while(SerialDevice_available() == 0) {
00070       wait(PACKET_WAITING_DELAY/1000.0);
00071       counter++;
00072       if(counter == PACKET_WAITING_COUNT) {
00073     return -DATA_TIMEOUT;
00074       }
00075     }
00076     packet[PACKET_HEADER_SIZE+i] = SerialDevice_getc();
00077     sum += packet[PACKET_HEADER_SIZE+i];
00078   }
00079   
00080   while(SerialDevice_available() == 0) {
00081     ;
00082   }
00083   uint8_t checksum = SerialDevice_getc();
00084   
00085   if(sum != checksum) {
00086     return -CHECKSUM_ERROR;
00087   }
00088   return PACKET_HEADER_SIZE + packet[DATA_LENGTH] + 1;
00089 }