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
Diff: SerialFlow.cpp
- Revision:
- 1:5f80d8d44549
- Parent:
- 0:3bc176d9a274
- Child:
- 2:7868220b4fdf
diff -r 3bc176d9a274 -r 5f80d8d44549 SerialFlow.cpp --- a/SerialFlow.cpp Sun Sep 09 13:42:25 2012 +0000 +++ b/SerialFlow.cpp Tue Sep 11 07:38:40 2012 +0000 @@ -1,67 +1,109 @@ -/* 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. - */ - -#include "SerialFlow.h" -#include "mbed.h" - -SerialFlow::SerialFlow(PinName tx, PinName rx): _serial(tx, rx) { - //Serial _serial(tx,rx); -} - -void SerialFlow::setPacketFormat(DataFormat p_format, char v_length, char p_size) { - _p_format = p_format; - _p_size = p_size; - _v_length = v_length; - _v_idx = 0; -} - -void SerialFlow::setPacketValue(short value) { - if( _v_idx < _p_size ){ - _v[_v_idx] = value; - _v_idx++; - } -} - -void SerialFlow::sendPacket() { - char v; - _serial.putc( 0x12 ); - for( char i=0; i<_p_size; i++ ){ - // low byte - v = _v[i] & 0xFF; - if( v==0x12 || v==0x13 || v==0x7D || v==0x10 ) - _serial.putc( 0x7D ); - _serial.putc( v ); - - // high byte - v = (_v[i]>>8) & 0xFF; - if( v==0x12 || v==0x13 || v==0x7D || v==0x10 ) - _serial.putc( 0x7D ); - _serial.putc( v ); - - // separate values - if( i<_p_size-1 ) - _serial.putc(0x10); - } - - _serial.putc( 0x13 ); - _v_idx = 0; -} +/* 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. + */ + +#include "SerialFlow.h" +#include "mbed.h" + +SerialFlow::SerialFlow(PinName tx, PinName rx): _serial(tx, rx) { + _escape = 0; + _collecting = 0; +} + +void SerialFlow::setPacketFormat(DataFormat p_format, char v_length, char p_size) { + _p_format = p_format; + _p_size = p_size; + _v_length = v_length; + _vs_idx = 0; + _vr_idx = 0; +} + +void SerialFlow::setPacketValue(short value) { + if( _vs_idx < _p_size ){ + _vs[_vs_idx++] = value; + } +} + +void SerialFlow::sendPacket() { + char v; + _serial.putc( 0x12 ); + for( char i=0; i<_p_size; i++ ){ + // low byte + v = _vs[i] & 0xFF; + if( v==0x12 || v==0x13 || v==0x7D || v==0x10 ) + _serial.putc( 0x7D ); + _serial.putc( v ); + + // high byte + v = (_vs[i]>>8) & 0xFF; + if( v==0x12 || v==0x13 || v==0x7D || v==0x10 ) + _serial.putc( 0x7D ); + _serial.putc( v ); + + // separate values + if( i<_p_size-1 ) + _serial.putc(0x10); + } + + _serial.putc( 0x13 ); + _vs_idx = 0; +} + +bool SerialFlow::receivePacket() { + char c; + while( _serial.readable() ){ + c = _serial.getc(); + if( _collecting ) + if( _escape ){ + _vr_val[_cr_idx++] = c; + _escape = 0; + } + // escape + else if( c == 0x7D ){ + _escape = 1; + } + // value separator + else if( c == 0x10 ){ + _vr[_vr_idx++] = _vr_val[0] | (_vr_val[1] << 8); + _cr_idx = 0; + } + // end + else if( c == 0x13 ){ + _vr[_vr_idx++] = _vr_val[0] | (_vr_val[1] << 8); + _collecting = 0; + return 1; + } + else{ + _vr_val[_cr_idx++] = c; + } + // begin + else if( c == 0x12 ){ + _collecting = 1; + _cr_idx = 0; + _vr_idx = 0; + } + } + return 0; +} + +short SerialFlow::getPacket( char idx ) { + return _vr[idx]; +}