SerialFlow allows to send and receive packaged arrays of integer values via serial port.

SerialFlow allows to send and receive packaged arrays of integer(short only) values via serial port.

Packet format:

  1. begin - 0x12
  2. end - 0x13
  3. value separator - 0x10
  4. escape - 0x7D

Simple packet example:
0x12,0x1,0x0,0x10,0x7D,0x12,0x0,0x13
corresponds to: [1,18]

Now handles only short int values. Example:

#include "mbed.h"
#include "SerialFlow.h"
SerialFlow pc(USBTX, USBRX);
AnalogIn gyro_x(p17); // data from gyro x axis
AnalogIn gyro_y(p18); // data from gyro y axis

int main(){
    // two short values
    pc.setPacketFormat(SerialFlow::COMPLEX_1, 2, 2);
    while(1){
        pc.setPacketValue((short)(gyro_x*1023.0));
        pc.setPacketValue((short)(gyro_y*1023.0));
        pc.sendPacket();
        wait(0.01);
    }
}

On the PC side you can use this program to catch data flows: http://www.poprobot.ru/files/sfmonitor_0.9.zip

Committer:
Decimus
Date:
Tue Sep 11 07:38:40 2012 +0000
Revision:
1:5f80d8d44549
Parent:
0:3bc176d9a274
Child:
2:7868220b4fdf
New methods receivePacket and getPacket

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Decimus 1:5f80d8d44549 1 /* mbed Serial Flow Library
Decimus 1:5f80d8d44549 2 * Copyright (c) 2012 Oleg Evsegneev
Decimus 1:5f80d8d44549 3 *
Decimus 1:5f80d8d44549 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
Decimus 1:5f80d8d44549 5 * of this software and associated documentation files (the "Software"), to deal
Decimus 1:5f80d8d44549 6 * in the Software without restriction, including without limitation the rights
Decimus 1:5f80d8d44549 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Decimus 1:5f80d8d44549 8 * copies of the Software, and to permit persons to whom the Software is
Decimus 1:5f80d8d44549 9 * furnished to do so, subject to the following conditions:
Decimus 1:5f80d8d44549 10 *
Decimus 1:5f80d8d44549 11 * The above copyright notice and this permission notice shall be included in
Decimus 1:5f80d8d44549 12 * all copies or substantial portions of the Software.
Decimus 1:5f80d8d44549 13 *
Decimus 1:5f80d8d44549 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Decimus 1:5f80d8d44549 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Decimus 1:5f80d8d44549 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Decimus 1:5f80d8d44549 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Decimus 1:5f80d8d44549 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Decimus 1:5f80d8d44549 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Decimus 1:5f80d8d44549 20 * THE SOFTWARE.
Decimus 1:5f80d8d44549 21 */
Decimus 1:5f80d8d44549 22
Decimus 1:5f80d8d44549 23 #include "SerialFlow.h"
Decimus 1:5f80d8d44549 24 #include "mbed.h"
Decimus 1:5f80d8d44549 25
Decimus 1:5f80d8d44549 26 SerialFlow::SerialFlow(PinName tx, PinName rx): _serial(tx, rx) {
Decimus 1:5f80d8d44549 27 _escape = 0;
Decimus 1:5f80d8d44549 28 _collecting = 0;
Decimus 1:5f80d8d44549 29 }
Decimus 1:5f80d8d44549 30
Decimus 1:5f80d8d44549 31 void SerialFlow::setPacketFormat(DataFormat p_format, char v_length, char p_size) {
Decimus 1:5f80d8d44549 32 _p_format = p_format;
Decimus 1:5f80d8d44549 33 _p_size = p_size;
Decimus 1:5f80d8d44549 34 _v_length = v_length;
Decimus 1:5f80d8d44549 35 _vs_idx = 0;
Decimus 1:5f80d8d44549 36 _vr_idx = 0;
Decimus 1:5f80d8d44549 37 }
Decimus 1:5f80d8d44549 38
Decimus 1:5f80d8d44549 39 void SerialFlow::setPacketValue(short value) {
Decimus 1:5f80d8d44549 40 if( _vs_idx < _p_size ){
Decimus 1:5f80d8d44549 41 _vs[_vs_idx++] = value;
Decimus 1:5f80d8d44549 42 }
Decimus 1:5f80d8d44549 43 }
Decimus 1:5f80d8d44549 44
Decimus 1:5f80d8d44549 45 void SerialFlow::sendPacket() {
Decimus 1:5f80d8d44549 46 char v;
Decimus 1:5f80d8d44549 47 _serial.putc( 0x12 );
Decimus 1:5f80d8d44549 48 for( char i=0; i<_p_size; i++ ){
Decimus 1:5f80d8d44549 49 // low byte
Decimus 1:5f80d8d44549 50 v = _vs[i] & 0xFF;
Decimus 1:5f80d8d44549 51 if( v==0x12 || v==0x13 || v==0x7D || v==0x10 )
Decimus 1:5f80d8d44549 52 _serial.putc( 0x7D );
Decimus 1:5f80d8d44549 53 _serial.putc( v );
Decimus 1:5f80d8d44549 54
Decimus 1:5f80d8d44549 55 // high byte
Decimus 1:5f80d8d44549 56 v = (_vs[i]>>8) & 0xFF;
Decimus 1:5f80d8d44549 57 if( v==0x12 || v==0x13 || v==0x7D || v==0x10 )
Decimus 1:5f80d8d44549 58 _serial.putc( 0x7D );
Decimus 1:5f80d8d44549 59 _serial.putc( v );
Decimus 1:5f80d8d44549 60
Decimus 1:5f80d8d44549 61 // separate values
Decimus 1:5f80d8d44549 62 if( i<_p_size-1 )
Decimus 1:5f80d8d44549 63 _serial.putc(0x10);
Decimus 1:5f80d8d44549 64 }
Decimus 1:5f80d8d44549 65
Decimus 1:5f80d8d44549 66 _serial.putc( 0x13 );
Decimus 1:5f80d8d44549 67 _vs_idx = 0;
Decimus 1:5f80d8d44549 68 }
Decimus 1:5f80d8d44549 69
Decimus 1:5f80d8d44549 70 bool SerialFlow::receivePacket() {
Decimus 1:5f80d8d44549 71 char c;
Decimus 1:5f80d8d44549 72 while( _serial.readable() ){
Decimus 1:5f80d8d44549 73 c = _serial.getc();
Decimus 1:5f80d8d44549 74 if( _collecting )
Decimus 1:5f80d8d44549 75 if( _escape ){
Decimus 1:5f80d8d44549 76 _vr_val[_cr_idx++] = c;
Decimus 1:5f80d8d44549 77 _escape = 0;
Decimus 1:5f80d8d44549 78 }
Decimus 1:5f80d8d44549 79 // escape
Decimus 1:5f80d8d44549 80 else if( c == 0x7D ){
Decimus 1:5f80d8d44549 81 _escape = 1;
Decimus 1:5f80d8d44549 82 }
Decimus 1:5f80d8d44549 83 // value separator
Decimus 1:5f80d8d44549 84 else if( c == 0x10 ){
Decimus 1:5f80d8d44549 85 _vr[_vr_idx++] = _vr_val[0] | (_vr_val[1] << 8);
Decimus 1:5f80d8d44549 86 _cr_idx = 0;
Decimus 1:5f80d8d44549 87 }
Decimus 1:5f80d8d44549 88 // end
Decimus 1:5f80d8d44549 89 else if( c == 0x13 ){
Decimus 1:5f80d8d44549 90 _vr[_vr_idx++] = _vr_val[0] | (_vr_val[1] << 8);
Decimus 1:5f80d8d44549 91 _collecting = 0;
Decimus 1:5f80d8d44549 92 return 1;
Decimus 1:5f80d8d44549 93 }
Decimus 1:5f80d8d44549 94 else{
Decimus 1:5f80d8d44549 95 _vr_val[_cr_idx++] = c;
Decimus 1:5f80d8d44549 96 }
Decimus 1:5f80d8d44549 97 // begin
Decimus 1:5f80d8d44549 98 else if( c == 0x12 ){
Decimus 1:5f80d8d44549 99 _collecting = 1;
Decimus 1:5f80d8d44549 100 _cr_idx = 0;
Decimus 1:5f80d8d44549 101 _vr_idx = 0;
Decimus 1:5f80d8d44549 102 }
Decimus 1:5f80d8d44549 103 }
Decimus 1:5f80d8d44549 104 return 0;
Decimus 1:5f80d8d44549 105 }
Decimus 1:5f80d8d44549 106
Decimus 1:5f80d8d44549 107 short SerialFlow::getPacket( char idx ) {
Decimus 1:5f80d8d44549 108 return _vr[idx];
Decimus 1:5f80d8d44549 109 }