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

Files at this revision

API Documentation at this revision

Comitter:
Decimus
Date:
Sun Oct 21 17:39:20 2012 +0000
Parent:
2:7868220b4fdf
Child:
4:ebf658b28c5f
Commit message:
Optional MODSERIAL support with default 32byte buffer. It's needed to send more then 5 short-type values.

Changed in this revision

SerialFlow.cpp Show annotated file Show diff for this revision Revisions of this file
SerialFlow.h Show annotated file Show diff for this revision Revisions of this file
--- a/SerialFlow.cpp	Mon Sep 17 20:21:31 2012 +0000
+++ b/SerialFlow.cpp	Sun Oct 21 17:39:20 2012 +0000
@@ -1,29 +1,16 @@
 /* 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.
+ * Released under the MIT License: http://mbed.org/license/mit
  */
  
 #include "SerialFlow.h"
 #include "mbed.h"
 
+#ifdef USE_MODSERIAL
+SerialFlow::SerialFlow(PinName tx, PinName rx): _serial(tx, rx, TX_BUFFER_SIZE, RX_BUFFER_SIZE) {
+#else
 SerialFlow::SerialFlow(PinName tx, PinName rx): _serial(tx, rx) {
+#endif
     _escape = 0;
     _collecting = 0;
 }
@@ -34,8 +21,8 @@
 
 void SerialFlow::setPacketFormat(DataFormat p_format, char v_length, char p_size) {
     _p_format = p_format;
+    _v_length = v_length;
     _p_size = p_size;
-    _v_length = v_length;
     _vs_idx = 0;
     _vr_idx = 0;
 }
@@ -46,10 +33,15 @@
     }
 }
 
-void SerialFlow::sendPacket() {
+bool SerialFlow::sendPacket() {
     char v;
+    if( !_serial.writeable() ){
+        _vs_idx = 0;
+        return 0;
+    }
+        
     _serial.putc( 0x12 );
-    for( char i=0; i<_p_size; i++ ){
+    for( char i=0; i<_vs_idx; i++ ){
         // low byte
         v = _vs[i] & 0xFF;
         if( v==0x12 || v==0x13 || v==0x7D || v==0x10 )
@@ -63,12 +55,13 @@
         _serial.putc( v );
             
         // separate values
-        if( i<_p_size-1 )
+        if( i<_vs_idx-1 )
             _serial.putc(0x10);
     }
         
     _serial.putc( 0x13 );
     _vs_idx = 0;
+    return 1;
 }
 
 bool SerialFlow::receivePacket() {
--- a/SerialFlow.h	Mon Sep 17 20:21:31 2012 +0000
+++ b/SerialFlow.h	Sun Oct 21 17:39:20 2012 +0000
@@ -1,31 +1,23 @@
 /* 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.
+ * Released under the MIT License: http://mbed.org/license/mit
  */
 
 #ifndef MBED_SERIALFLOW_H
 #define MBED_SERIALFLOW_H
-  
+
+// optional use of MODSERIAL
+//#define USE_MODSERIAL
+
 #include "mbed.h"
 
-#define MAX_PACKET_SIZE 5
+#ifdef USE_MODSERIAL
+#include "MODSERIAL.h"
+#endif
+
+#define MAX_PACKET_SIZE 10
+#define RX_BUFFER_SIZE 32
+#define TX_BUFFER_SIZE 32
 
 class SerialFlow {
 
@@ -33,13 +25,12 @@
     /** Data format */
     enum DataFormat {
         SIMPLE     /**< Simple one byte (default) */
-        , COMPLEX_1  /**< Escaped many bytes for one value */
-        , COMPLEX_2   /**< Escaped many bytes for two values */
+        , COMPLEX  /**< Packet of many short values */
     };
     
     SerialFlow(PinName tx, PinName rx);
 
-    /** Set porta baud rate
+    /** Set portal baud rate
      *
      * @param baud_rate Port baud rate.
      */
@@ -61,7 +52,7 @@
         
     /**  Send packet to serial port
      */
-    void sendPacket();
+    bool sendPacket();
 
     /**  Receive packet from serial port
      */
@@ -73,7 +64,12 @@
     short getPacket( char idx );
 
 protected:
+    #ifdef USE_MODSERIAL
+    MODSERIAL _serial;
+    #else
     Serial _serial;
+    #endif
+
     char _p_format;
     char _p_size;
     char _v_length;