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:
Mon Sep 17 20:21:31 2012 +0000
Revision:
2:7868220b4fdf
Parent:
1:5f80d8d44549
Child:
3:7cbfd422c98e
Method "baud"

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 #ifndef MBED_SERIALFLOW_H
Decimus 1:5f80d8d44549 24 #define MBED_SERIALFLOW_H
Decimus 1:5f80d8d44549 25
Decimus 1:5f80d8d44549 26 #include "mbed.h"
Decimus 1:5f80d8d44549 27
Decimus 1:5f80d8d44549 28 #define MAX_PACKET_SIZE 5
Decimus 1:5f80d8d44549 29
Decimus 1:5f80d8d44549 30 class SerialFlow {
Decimus 1:5f80d8d44549 31
Decimus 1:5f80d8d44549 32 public:
Decimus 1:5f80d8d44549 33 /** Data format */
Decimus 1:5f80d8d44549 34 enum DataFormat {
Decimus 1:5f80d8d44549 35 SIMPLE /**< Simple one byte (default) */
Decimus 1:5f80d8d44549 36 , COMPLEX_1 /**< Escaped many bytes for one value */
Decimus 1:5f80d8d44549 37 , COMPLEX_2 /**< Escaped many bytes for two values */
Decimus 1:5f80d8d44549 38 };
Decimus 1:5f80d8d44549 39
Decimus 1:5f80d8d44549 40 SerialFlow(PinName tx, PinName rx);
Decimus 1:5f80d8d44549 41
Decimus 2:7868220b4fdf 42 /** Set porta baud rate
Decimus 2:7868220b4fdf 43 *
Decimus 2:7868220b4fdf 44 * @param baud_rate Port baud rate.
Decimus 2:7868220b4fdf 45 */
Decimus 2:7868220b4fdf 46 void baud(int baud_rate);
Decimus 2:7868220b4fdf 47
Decimus 1:5f80d8d44549 48 /** Set data packet format
Decimus 1:5f80d8d44549 49 *
Decimus 1:5f80d8d44549 50 * @param p_format Format type constant.
Decimus 1:5f80d8d44549 51 * @param v_length Length of value in bytes.
Decimus 1:5f80d8d44549 52 * @param p_size Number of values.
Decimus 1:5f80d8d44549 53 */
Decimus 1:5f80d8d44549 54 void setPacketFormat(DataFormat p_format, char v_length, char p_size);
Decimus 1:5f80d8d44549 55
Decimus 1:5f80d8d44549 56 /** Set value to data packet
Decimus 1:5f80d8d44549 57 *
Decimus 1:5f80d8d44549 58 * @param value Value.
Decimus 1:5f80d8d44549 59 */
Decimus 1:5f80d8d44549 60 void setPacketValue(short value);
Decimus 1:5f80d8d44549 61
Decimus 1:5f80d8d44549 62 /** Send packet to serial port
Decimus 1:5f80d8d44549 63 */
Decimus 1:5f80d8d44549 64 void sendPacket();
Decimus 1:5f80d8d44549 65
Decimus 1:5f80d8d44549 66 /** Receive packet from serial port
Decimus 1:5f80d8d44549 67 */
Decimus 1:5f80d8d44549 68 bool receivePacket();
Decimus 1:5f80d8d44549 69
Decimus 1:5f80d8d44549 70 /** Get received packet
Decimus 1:5f80d8d44549 71 * @param idx Index of value from packet.
Decimus 1:5f80d8d44549 72 */
Decimus 1:5f80d8d44549 73 short getPacket( char idx );
Decimus 1:5f80d8d44549 74
Decimus 1:5f80d8d44549 75 protected:
Decimus 1:5f80d8d44549 76 Serial _serial;
Decimus 1:5f80d8d44549 77 char _p_format;
Decimus 1:5f80d8d44549 78 char _p_size;
Decimus 1:5f80d8d44549 79 char _v_length;
Decimus 1:5f80d8d44549 80
Decimus 1:5f80d8d44549 81 short _vs[MAX_PACKET_SIZE];
Decimus 1:5f80d8d44549 82 char _vs_idx;
Decimus 1:5f80d8d44549 83
Decimus 1:5f80d8d44549 84 short _vr[MAX_PACKET_SIZE];
Decimus 1:5f80d8d44549 85 char _vr_val[2];
Decimus 1:5f80d8d44549 86 char _vr_idx;
Decimus 1:5f80d8d44549 87 char _cr_idx;
Decimus 1:5f80d8d44549 88 bool _escape;
Decimus 1:5f80d8d44549 89 bool _collecting;
Decimus 1:5f80d8d44549 90 };
Decimus 1:5f80d8d44549 91
Decimus 1:5f80d8d44549 92 #endif