Page 1 of 1

Serial Communication

Posted: Thu Apr 24, 2014 6:01 am
by adnil
Hi to all,

I want to know what is the time restriction for using chSequentialStreamWrite function, I am using the next code, when I put a chThdSleepMilliseconds < 600ms the frame is truncated:

Code: Select all

#include "ch.h"
#include "hal.h"
#include "chprintf.h"
#include <stdio.h>
#include <string.h>
#include "chconf.h"
static int8_t flag = 1;
uint8_t  DI = 0, DO1 = 0, DOE=0;
uint16_t DI1E=0,DO=0;
uint16_t AI1=0, AI2=0, AI3=0, AI4=0;
char message[10];
char receive;


struct frame {
    uint16_t AInput1;
    uint16_t AInput2;
    uint16_t AInput3;
    uint16_t AInput4;
    uint8_t DInput;
    uint8_t  state;
} values;



/* Digital Inputs*/


static void extcb1(EXTDriver *extp, expchannel_t channel) {
  static VirtualTimer vt4;
  (void)extp;
  (void)channel;
   if(flag)
       flag = 0;
   else
       flag = 1;
}

static const EXTConfig extcfg = {
  {
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
  }
};




/*
 * Application entry point.
 */
int main(void) {

    /*
     * System initializations.
     * - HAL initialization, this also initializes the configured device drivers
     *   and performs the board-specific initializations.
     * - Kernel initialization, the main() function becomes a thread and the
     *   RTOS is active.
     */
    halInit();
    chSysInit();

  /*
   * Activates the serial driver 1 using the driver default configuration.
   */
  sdStart(&SD1, NULL);
  extStart(&EXTD1, &extcfg);
  /*
   * Initializes the ADC driver 1 and enable the thermal sensor.
   * The pin PC1 on the port GPIOC is programmed as analog input.
   */
  //adcStart(&ADCD1, NULL);
  //adcSTM32EnableTSVREFE();
  palSetGroupMode(GPIOC, PAL_PORT_BIT(0) | PAL_PORT_BIT(1) | PAL_PORT_BIT(2) | PAL_PORT_BIT(3),0,PAL_MODE_INPUT_ANALOG);
  //palSetPadMode(GPIOC, 0, PAL_MODE_INPUT_ANALOG);

  palClearPad(GPIOE, GPIOE_DO1);
  palClearPad(GPIOE, GPIOE_DO2);
  palClearPad(GPIOE, GPIOE_DO3);
  palClearPad(GPIOE, GPIOE_DO4);
 

    while (TRUE) {
   if (flag) {
     DI1E = palReadPort(GPIOE);
     DI  = ((DI1E & 0x7C)>>2) | (DI1E & 0x80) | ((DI1E & 0x3000)>>7);
         values.DInput = DI;
   }
   else
   {   
   DO = palReadPort(GPIOE);
   DOE = (DO & 0xF00)>>4;
   }
   memcpy(message, &values, sizeof (values));

        chSequentialStreamWrite((BaseSequentialStream *)&SD1,  (const uint8_t *)&message, sizeof (values));
   chThdSleepMilliseconds(600);
    }
}


Thanks a lot,

Re: Serial Communication

Posted: Thu Apr 24, 2014 7:56 am
by Giovanni
There should be no time restrictions, it should work regardless the following sleep. What do you mean by truncated?

Giovanni

Re: Serial Communication

Posted: Thu Apr 24, 2014 1:14 pm
by steved
Possibly the size of structure 'frame' is greater than 10 bytes, depending on whether the compiler aligns the variables on word/double word boundaries. Certainly don't think you can rely on the structure being packed unless you explicitly specify.

Re: Serial Communication

Posted: Fri Apr 25, 2014 3:39 am
by adnil
Thanks a lot for the replies,
I mean tuncated when the data is not complete, I supposed that I was sending a 10 byte data but I only get 8 byte data.

How can I explicitly specify the lenght of the data?

Thans again,

Regards

Re: Serial Communication

Posted: Fri Apr 25, 2014 7:24 am
by Giovanni
The size of data is the third parameter of chSequentialStreamWrite().

It is possible you have another kind of problem: you don't have a protocol, you just send the structure over the serial port without delimiters or other markers.

How you make sure, on the receiving side, that you are reading starting from the first byte of a structure? are you sure that less than 10 bytes are sent? try sending the data as ASCII hex buffers delimited by CRs. This would make easier to inspect the output using a terminal emulator.

Giovanni