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.h

Committer:
Decimus
Date:
2012-09-09
Revision:
0:3bc176d9a274
Child:
1:5f80d8d44549

File content as of revision 0:3bc176d9a274:

/* 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.
 */

#ifndef MBED_SERIALFLOW_H
#define MBED_SERIALFLOW_H
  
#include "mbed.h"

#define MAX_PACKET_SIZE 5

class SerialFlow {

public:
    /** Data format */
    enum DataFormat {
        SIMPLE     /**< Simple one byte (default) */
        , COMPLEX_1  /**< Escaped many bytes for one value */
        , COMPLEX_2   /**< Escaped many bytes for two values */
    };
    
    SerialFlow(PinName tx, PinName rx);

    /** Set data packet format
     *
     * @param format Format type constant.
     * @param alen Length of bytes of first value.
     * @param blen Length of bytes of second value.
     */
    void setPacketFormat(DataFormat p_format, char v_length, char p_size);

    /**  Set value to data packet
     *
     * @param value Value.
     */
    void setPacketValue(short value);
        
    /**  Send packet to serial port
     */
    void sendPacket();
    
protected:
    Serial _serial;
    char _p_format;
    char _p_size;
    char _v_length;
    short _v[MAX_PACKET_SIZE];
    char _v_idx;
};

#endif