Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Matlab not reacting to LF terminator of data sent from controller

Status
Not open for further replies.

yefj

Advanced Member level 4
Joined
Sep 12, 2019
Messages
1,190
Helped
1
Reputation
2
Reaction score
3
Trophy points
38
Activity points
7,180
Hello if have built the following code which sends with delay 5 bytes one after the other ans shown bellow.
to connect to matlab i use the shown bellow commands with the shown bellow callback function.
In reality Matlab is not reacting to the streamed data from the controller.
it should have shown me data the moment it saw LF terminator which is 0A.
Instead i get 10 second delay and a huge block of data pored on the terminal as shown bellow.

Where did i go wrong?
Thanks.
Code:
sObject=serial('COM4','BaudRate',115200,'TimeOut',10,'Terminator','LF')

sObject.BytesAvailableFcn={@callbackSerial};

fopen(sObject)
Code:
function callbackSerial(ser,~)
global time;
%val=fread(ser);
val=fscanf(ser,'%x');
%val(1,1);
dec2hex(uint8(val))
end

1604154973799.png

Code:
#include "em_device.h"
#include "em_cmu.h"
#include "em_gpio.h"
#include "em_usart.h"
#include "em_chip.h"
#include <stdint.h>
#include <stdbool.h>
#include "em_emu.h"
#include "bsp.h"
#include "bsp_trace.h"


uint8_t received_flag=0;//
// Receive data buffer
uint8_t buffer;
uint8_t tx_buffer=0x6F;
uint8_t tx_buffer_new=0x06;


void USART0_RX_IRQHandler(void)
{
    received_flag=1;
  // Get the character just received
  buffer = USART0->RXDATA;

  switch (buffer)
        {
        case 'a':

            tx_buffer_new=0x4F;
          break;

        case 'b':
            tx_buffer_new=0x62;
        break;

        case 'c':
                    tx_buffer_new=0x63;
         break;

        default:

        break;

        }//end switch
  // Clear the requesting interrupt before exiting the handler
  USART_IntClear(USART0, USART_IF_RXDATAV);
}



int main(void)
{
  uint32_t i;

  // Chip errata
  CHIP_Init();
  CMU_ClockEnable(cmuClock_GPIO, true);
  CMU_ClockEnable(cmuClock_USART0, true);


  //EFR32fg14 LOC2 page 157 data sheet TX P0
  GPIO_PinModeSet(gpioPortA,2, gpioModePushPull, 1);

//EFR32fg14 LOC2 page 157 data sheet RX P2
  GPIO_PinModeSet(gpioPortA,3, gpioModeInput, 0);

  GPIO_PinModeSet(gpioPortA, 5, gpioModePushPull, 1);


  // Default asynchronous initializer (115.2 Kbps, 8N1, no flow control)
  USART_InitAsync_TypeDef init = USART_INITASYNC_DEFAULT;

  // Configure and enable USART0
  USART_InitAsync(USART0, &init);
  //datasheet page 157 location2  rx portA pin3(P2) ,tx port A pin2(P0)
  USART0->ROUTELOC0 = USART_ROUTELOC0_RXLOC_LOC2 | USART_ROUTELOC0_TXLOC_LOC2;
  USART0->ROUTEPEN |= USART_ROUTEPEN_TXPEN | USART_ROUTEPEN_RXPEN;


  // Enable NVIC USART sources
  NVIC_ClearPendingIRQ(USART0_RX_IRQn);
  NVIC_EnableIRQ(USART0_RX_IRQn);
  //enabling TX interrupt made problems
  //NVIC_ClearPendingIRQ(USART0_TX_IRQn);
  //NVIC_EnableIRQ(USART0_TX_IRQn);



  while (1)
  {
      for(i=0;i<10000;i++)
      {
            GPIO_PinOutSet(gpioPortA,5);
            GPIO_PinOutClear(gpioPortA,5);
       }


      if (received_flag==1)
      {
          USART_Tx(USART0,0x2F);
          USART_Tx(USART0,tx_buffer_new);
          USART_Tx(USART0,0x8F);
          USART_Tx(USART0,0x3F);
          USART_Tx(USART0,'\n');
          tx_buffer=tx_buffer_new;
          received_flag=0;
      }
      else
      {
                  USART_Tx(USART0,0x2F);
                   USART_Tx(USART0,tx_buffer_new);
                   USART_Tx(USART0,0x8F);
                   USART_Tx(USART0,0x3F);
                   USART_Tx(USART0,'\n');

      }
      for(i=0;i<10000;i++)
      {
            GPIO_PinOutSet(gpioPortA,5);
            GPIO_PinOutClear(gpioPortA,5);
       }

    // Enable receive data valid interrupt
    USART_IntEnable(USART0, USART_IEN_RXDATAV);







  }
}
 

hello,

are you sure you don't need to send a specific sequence to your matlab application like:
\r\n
or
\n\r
or
\r

they are no "Standard" rule do define terminator string sequence with any client application..
(or protocole ?)
 

yes i am sending a 0A its '\n' in ASCII if i send one sequence its reacting but if i send 2F 06 FF 3F 0A(\n) wich delay over and over its just not reacting.
you say i should put \n after every data byte?
 

Carriage return or linefeed terminator makes only sense with formatted ASCII data. You are apparently sending binary data.

It's unclear which protocol you are trying to receive in Matlab.
--- Updated ---

fscanf( ,'%x') is specifically awaiting hexadecimal numbers. I guess you know how they look like? In case of doubt, there will be a corresponding printf( ,"%x") in the sending application.
 
Last edited:

Hello FvM,The protocol is UART. i manage to solve the problem but by i dont know why.
it stoped hording recieved data when i sent sequences of \n 1F 2F 3F FF \n in a loop,good news that It gets me the data staright away :)
bad news that it shows the sequence as 1F 2F 3F FF \n\n.
i dont know why it worked and why it showed the \n \n sticked together.
its as if he skips first \n sending 1F 2F 3F then sending \n with the first \n ,its in a while loop.
I would happy to get a good intuition?
Thanks.
 
Last edited:

hello,

if you send Bytes , not ascii value,
what about \n
\n= 0x0A
it means you can not send this value 0x0A !

you Need to clearly define the protocole with Matlab...
 

    yefj

    Points: 2
    Helpful Answer Positive Rating
Hi,

and UART is no protocol, it's a serial interface.

Protocol has to do how and what data you send, how they are coded and acknowledged, how a frame is set up and so in.
Protocol more often is software than hardware.

Klaus
 

    yefj

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top