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:
Sun Sep 09 13:42:25 2012 +0000
Revision:
0:3bc176d9a274
Child:
1:5f80d8d44549
Ability to send several short integer values

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Decimus 0:3bc176d9a274 1 /* mbed Serial Flow Library
Decimus 0:3bc176d9a274 2 * Copyright (c) 2012 Oleg Evsegneev
Decimus 0:3bc176d9a274 3 *
Decimus 0:3bc176d9a274 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
Decimus 0:3bc176d9a274 5 * of this software and associated documentation files (the "Software"), to deal
Decimus 0:3bc176d9a274 6 * in the Software without restriction, including without limitation the rights
Decimus 0:3bc176d9a274 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Decimus 0:3bc176d9a274 8 * copies of the Software, and to permit persons to whom the Software is
Decimus 0:3bc176d9a274 9 * furnished to do so, subject to the following conditions:
Decimus 0:3bc176d9a274 10 *
Decimus 0:3bc176d9a274 11 * The above copyright notice and this permission notice shall be included in
Decimus 0:3bc176d9a274 12 * all copies or substantial portions of the Software.
Decimus 0:3bc176d9a274 13 *
Decimus 0:3bc176d9a274 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Decimus 0:3bc176d9a274 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Decimus 0:3bc176d9a274 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Decimus 0:3bc176d9a274 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Decimus 0:3bc176d9a274 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Decimus 0:3bc176d9a274 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Decimus 0:3bc176d9a274 20 * THE SOFTWARE.
Decimus 0:3bc176d9a274 21 */
Decimus 0:3bc176d9a274 22
Decimus 0:3bc176d9a274 23 #ifndef MBED_SERIALFLOW_H
Decimus 0:3bc176d9a274 24 #define MBED_SERIALFLOW_H
Decimus 0:3bc176d9a274 25
Decimus 0:3bc176d9a274 26 #include "mbed.h"
Decimus 0:3bc176d9a274 27
Decimus 0:3bc176d9a274 28 #define MAX_PACKET_SIZE 5
Decimus 0:3bc176d9a274 29
Decimus 0:3bc176d9a274 30 class SerialFlow {
Decimus 0:3bc176d9a274 31
Decimus 0:3bc176d9a274 32 public:
Decimus 0:3bc176d9a274 33 /** Data format */
Decimus 0:3bc176d9a274 34 enum DataFormat {
Decimus 0:3bc176d9a274 35 SIMPLE /**< Simple one byte (default) */
Decimus 0:3bc176d9a274 36 , COMPLEX_1 /**< Escaped many bytes for one value */
Decimus 0:3bc176d9a274 37 , COMPLEX_2 /**< Escaped many bytes for two values */
Decimus 0:3bc176d9a274 38 };
Decimus 0:3bc176d9a274 39
Decimus 0:3bc176d9a274 40 SerialFlow(PinName tx, PinName rx);
Decimus 0:3bc176d9a274 41
Decimus 0:3bc176d9a274 42 /** Set data packet format
Decimus 0:3bc176d9a274 43 *
Decimus 0:3bc176d9a274 44 * @param format Format type constant.
Decimus 0:3bc176d9a274 45 * @param alen Length of bytes of first value.
Decimus 0:3bc176d9a274 46 * @param blen Length of bytes of second value.
Decimus 0:3bc176d9a274 47 */
Decimus 0:3bc176d9a274 48 void setPacketFormat(DataFormat p_format, char v_length, char p_size);
Decimus 0:3bc176d9a274 49
Decimus 0:3bc176d9a274 50 /** Set value to data packet
Decimus 0:3bc176d9a274 51 *
Decimus 0:3bc176d9a274 52 * @param value Value.
Decimus 0:3bc176d9a274 53 */
Decimus 0:3bc176d9a274 54 void setPacketValue(short value);
Decimus 0:3bc176d9a274 55
Decimus 0:3bc176d9a274 56 /** Send packet to serial port
Decimus 0:3bc176d9a274 57 */
Decimus 0:3bc176d9a274 58 void sendPacket();
Decimus 0:3bc176d9a274 59
Decimus 0:3bc176d9a274 60 protected:
Decimus 0:3bc176d9a274 61 Serial _serial;
Decimus 0:3bc176d9a274 62 char _p_format;
Decimus 0:3bc176d9a274 63 char _p_size;
Decimus 0:3bc176d9a274 64 char _v_length;
Decimus 0:3bc176d9a274 65 short _v[MAX_PACKET_SIZE];
Decimus 0:3bc176d9a274 66 char _v_idx;
Decimus 0:3bc176d9a274 67 };
Decimus 0:3bc176d9a274 68
Decimus 0:3bc176d9a274 69 #endif