Serial Communication

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
adnil
Posts: 16
Joined: Fri Aug 09, 2013 5:12 am

Serial Communication

Post 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,
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Serial Communication

Post by Giovanni »

There should be no time restrictions, it should work regardless the following sleep. What do you mean by truncated?

Giovanni
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: Serial Communication

Post 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.
adnil
Posts: 16
Joined: Fri Aug 09, 2013 5:12 am

Re: Serial Communication

Post 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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Serial Communication

Post 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
Post Reply