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 #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 2:7868220b4fdf 31 void SerialFlow::baud(int baud_rate) {
Decimus 2:7868220b4fdf 32 _serial.baud(baud_rate);
Decimus 2:7868220b4fdf 33 }
Decimus 2:7868220b4fdf 34
Decimus 1:5f80d8d44549 35 void SerialFlow::setPacketFormat(DataFormat p_format, char v_length, char p_size) {
Decimus 1:5f80d8d44549 36 _p_format = p_format;
Decimus 1:5f80d8d44549 37 _p_size = p_size;
Decimus 1:5f80d8d44549 38 _v_length = v_length;
Decimus 1:5f80d8d44549 39 _vs_idx = 0;
Decimus 1:5f80d8d44549 40 _vr_idx = 0;
Decimus 1:5f80d8d44549 41 }
Decimus 1:5f80d8d44549 42
Decimus 1:5f80d8d44549 43 void SerialFlow::setPacketValue(short value) {
Decimus 1:5f80d8d44549 44 if( _vs_idx < _p_size ){
Decimus 1:5f80d8d44549 45 _vs[_vs_idx++] = value;
Decimus 1:5f80d8d44549 46 }
Decimus 1:5f80d8d44549 47 }
Decimus 1:5f80d8d44549 48
Decimus 1:5f80d8d44549 49 void SerialFlow::sendPacket() {
Decimus 1:5f80d8d44549 50 char v;
Decimus 1:5f80d8d44549 51 _serial.putc( 0x12 );
Decimus 1:5f80d8d44549 52 for( char i=0; i<_p_size; i++ ){
Decimus 1:5f80d8d44549 53 // low byte
Decimus 1:5f80d8d44549 54 v = _vs[i] & 0xFF;
Decimus 1:5f80d8d44549 55 if( v==0x12 || v==0x13 || v==0x7D || v==0x10 )
Decimus 1:5f80d8d44549 56 _serial.putc( 0x7D );
Decimus 1:5f80d8d44549 57 _serial.putc( v );
Decimus 1:5f80d8d44549 58
Decimus 1:5f80d8d44549 59 // high byte
Decimus 1:5f80d8d44549 60 v = (_vs[i]>>8) & 0xFF;
Decimus 1:5f80d8d44549 61 if( v==0x12 || v==0x13 || v==0x7D || v==0x10 )
Decimus 1:5f80d8d44549 62 _serial.putc( 0x7D );
Decimus 1:5f80d8d44549 63 _serial.putc( v );
Decimus 1:5f80d8d44549 64
Decimus 1:5f80d8d44549 65 // separate values
Decimus 1:5f80d8d44549 66 if( i<_p_size-1 )
Decimus 1:5f80d8d44549 67 _serial.putc(0x10);
Decimus 1:5f80d8d44549 68 }
Decimus 1:5f80d8d44549 69
Decimus 1:5f80d8d44549 70 _serial.putc( 0x13 );
Decimus 1:5f80d8d44549 71 _vs_idx = 0;
Decimus 1:5f80d8d44549 72 }
Decimus 1:5f80d8d44549 73
Decimus 1:5f80d8d44549 74 bool SerialFlow::receivePacket() {
Decimus 1:5f80d8d44549 75 char c;
Decimus 1:5f80d8d44549 76 while( _serial.readable() ){
Decimus 1:5f80d8d44549 77 c = _serial.getc();
Decimus 1:5f80d8d44549 78 if( _collecting )
Decimus 1:5f80d8d44549 79 if( _escape ){
Decimus 1:5f80d8d44549 80 _vr_val[_cr_idx++] = c;
Decimus 1:5f80d8d44549 81 _escape = 0;
Decimus 1:5f80d8d44549 82 }
Decimus 1:5f80d8d44549 83 // escape
Decimus 1:5f80d8d44549 84 else if( c == 0x7D ){
Decimus 1:5f80d8d44549 85 _escape = 1;
Decimus 1:5f80d8d44549 86 }
Decimus 1:5f80d8d44549 87 // value separator
Decimus 1:5f80d8d44549 88 else if( c == 0x10 ){
Decimus 1:5f80d8d44549 89 _vr[_vr_idx++] = _vr_val[0] | (_vr_val[1] << 8);
Decimus 1:5f80d8d44549 90 _cr_idx = 0;
Decimus 1:5f80d8d44549 91 }
Decimus 1:5f80d8d44549 92 // end
Decimus 1:5f80d8d44549 93 else if( c == 0x13 ){
Decimus 1:5f80d8d44549 94 _vr[_vr_idx++] = _vr_val[0] | (_vr_val[1] << 8);
Decimus 1:5f80d8d44549 95 _collecting = 0;
Decimus 1:5f80d8d44549 96 return 1;
Decimus 1:5f80d8d44549 97 }
Decimus 1:5f80d8d44549 98 else{
Decimus 1:5f80d8d44549 99 _vr_val[_cr_idx++] = c;
Decimus 1:5f80d8d44549 100 }
Decimus 1:5f80d8d44549 101 // begin
Decimus 1:5f80d8d44549 102 else if( c == 0x12 ){
Decimus 1:5f80d8d44549 103 _collecting = 1;
Decimus 1:5f80d8d44549 104 _cr_idx = 0;
Decimus 1:5f80d8d44549 105 _vr_idx = 0;
Decimus 1:5f80d8d44549 106 }
Decimus 1:5f80d8d44549 107 }
Decimus 1:5f80d8d44549 108 return 0;
Decimus 1:5f80d8d44549 109 }
Decimus 1:5f80d8d44549 110
Decimus 1:5f80d8d44549 111 short SerialFlow::getPacket( char idx ) {
Decimus 1:5f80d8d44549 112 return _vr[idx];
Decimus 1:5f80d8d44549 113 }