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

SerialFlow.cpp

Committer:
Decimus
Date:
2012-09-17
Revision:
2:7868220b4fdf
Parent:
1:5f80d8d44549
Child:
3:7cbfd422c98e

File content as of revision 2:7868220b4fdf:

/* mbed Serial Flow Library
 * Copyright (c) 2012 Oleg Evsegneev
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
 
#include "SerialFlow.h"
#include "mbed.h"

SerialFlow::SerialFlow(PinName tx, PinName rx): _serial(tx, rx) {
    _escape = 0;
    _collecting = 0;
}

void SerialFlow::baud(int baud_rate) {
    _serial.baud(baud_rate);
}

void SerialFlow::setPacketFormat(DataFormat p_format, char v_length, char p_size) {
    _p_format = p_format;
    _p_size = p_size;
    _v_length = v_length;
    _vs_idx = 0;
    _vr_idx = 0;
}

void SerialFlow::setPacketValue(short value) {
    if( _vs_idx < _p_size ){
        _vs[_vs_idx++] = value;
    }
}

void SerialFlow::sendPacket() {
    char v;
    _serial.putc( 0x12 );
    for( char i=0; i<_p_size; i++ ){
        // low byte
        v = _vs[i] & 0xFF;
        if( v==0x12 || v==0x13 || v==0x7D || v==0x10 )
            _serial.putc( 0x7D );
        _serial.putc( v );

        // high byte
        v = (_vs[i]>>8) & 0xFF;
        if( v==0x12 || v==0x13 || v==0x7D || v==0x10 )
            _serial.putc( 0x7D );
        _serial.putc( v );
            
        // separate values
        if( i<_p_size-1 )
            _serial.putc(0x10);
    }
        
    _serial.putc( 0x13 );
    _vs_idx = 0;
}

bool SerialFlow::receivePacket() {
    char c;
    while( _serial.readable() ){
        c = _serial.getc();
        if( _collecting )
            if( _escape ){
                _vr_val[_cr_idx++] = c;
                _escape = 0;
            }
            // escape
            else if( c == 0x7D ){
                _escape = 1;
            }
            // value separator
            else if( c == 0x10 ){
                _vr[_vr_idx++] = _vr_val[0] | (_vr_val[1] << 8);
                _cr_idx = 0;
            }    
            // end
            else if( c == 0x13 ){
                _vr[_vr_idx++] = _vr_val[0] | (_vr_val[1] << 8);
                _collecting = 0;
                return 1;
            }    
            else{
                _vr_val[_cr_idx++] = c;
            }    
        // begin
        else if( c == 0x12 ){
            _collecting = 1;
            _cr_idx = 0;
            _vr_idx = 0;
        }    
    }
    return 0;
}

short SerialFlow::getPacket( char idx ) {
    return _vr[idx];
}