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:
- begin - 0x12
- end - 0x13
- value separator - 0x10
- 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@0:3bc176d9a274, 2012-09-09 (annotated)
- 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?
User | Revision | Line number | New 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 | #include "SerialFlow.h" |
Decimus | 0:3bc176d9a274 | 24 | #include "mbed.h" |
Decimus | 0:3bc176d9a274 | 25 | |
Decimus | 0:3bc176d9a274 | 26 | SerialFlow::SerialFlow(PinName tx, PinName rx): _serial(tx, rx) { |
Decimus | 0:3bc176d9a274 | 27 | //Serial _serial(tx,rx); |
Decimus | 0:3bc176d9a274 | 28 | } |
Decimus | 0:3bc176d9a274 | 29 | |
Decimus | 0:3bc176d9a274 | 30 | void SerialFlow::setPacketFormat(DataFormat p_format, char v_length, char p_size) { |
Decimus | 0:3bc176d9a274 | 31 | _p_format = p_format; |
Decimus | 0:3bc176d9a274 | 32 | _p_size = p_size; |
Decimus | 0:3bc176d9a274 | 33 | _v_length = v_length; |
Decimus | 0:3bc176d9a274 | 34 | _v_idx = 0; |
Decimus | 0:3bc176d9a274 | 35 | } |
Decimus | 0:3bc176d9a274 | 36 | |
Decimus | 0:3bc176d9a274 | 37 | void SerialFlow::setPacketValue(short value) { |
Decimus | 0:3bc176d9a274 | 38 | if( _v_idx < _p_size ){ |
Decimus | 0:3bc176d9a274 | 39 | _v[_v_idx] = value; |
Decimus | 0:3bc176d9a274 | 40 | _v_idx++; |
Decimus | 0:3bc176d9a274 | 41 | } |
Decimus | 0:3bc176d9a274 | 42 | } |
Decimus | 0:3bc176d9a274 | 43 | |
Decimus | 0:3bc176d9a274 | 44 | void SerialFlow::sendPacket() { |
Decimus | 0:3bc176d9a274 | 45 | char v; |
Decimus | 0:3bc176d9a274 | 46 | _serial.putc( 0x12 ); |
Decimus | 0:3bc176d9a274 | 47 | for( char i=0; i<_p_size; i++ ){ |
Decimus | 0:3bc176d9a274 | 48 | // low byte |
Decimus | 0:3bc176d9a274 | 49 | v = _v[i] & 0xFF; |
Decimus | 0:3bc176d9a274 | 50 | if( v==0x12 || v==0x13 || v==0x7D || v==0x10 ) |
Decimus | 0:3bc176d9a274 | 51 | _serial.putc( 0x7D ); |
Decimus | 0:3bc176d9a274 | 52 | _serial.putc( v ); |
Decimus | 0:3bc176d9a274 | 53 | |
Decimus | 0:3bc176d9a274 | 54 | // high byte |
Decimus | 0:3bc176d9a274 | 55 | v = (_v[i]>>8) & 0xFF; |
Decimus | 0:3bc176d9a274 | 56 | if( v==0x12 || v==0x13 || v==0x7D || v==0x10 ) |
Decimus | 0:3bc176d9a274 | 57 | _serial.putc( 0x7D ); |
Decimus | 0:3bc176d9a274 | 58 | _serial.putc( v ); |
Decimus | 0:3bc176d9a274 | 59 | |
Decimus | 0:3bc176d9a274 | 60 | // separate values |
Decimus | 0:3bc176d9a274 | 61 | if( i<_p_size-1 ) |
Decimus | 0:3bc176d9a274 | 62 | _serial.putc(0x10); |
Decimus | 0:3bc176d9a274 | 63 | } |
Decimus | 0:3bc176d9a274 | 64 | |
Decimus | 0:3bc176d9a274 | 65 | _serial.putc( 0x13 ); |
Decimus | 0:3bc176d9a274 | 66 | _v_idx = 0; |
Decimus | 0:3bc176d9a274 | 67 | } |