Quantcast
Channel: Forum - Recent Threads
Viewing all articles
Browse latest Browse all 5781

How many characters Uart Read return ?

$
0
0

 Hello,

I am using SK-S7G2 to make serial communication (RS485 Modbus RTU) with the Hal Driver, her is my code :

#define NAB 5
#define DEB_MAPPING 0
#define FIN_MAPPING 80

void hal_entry(void)
{int i, j;
  static ssp_err_t err;              // Create a variable for the error
  uint16_t crc;
  timer_info_t info_timer;
    
 err = g_timer0.p_api->open(g_timer0.p_ctrl, g_timer0.p_cfg);
 err = g_timer0.p_api->periodSet(g_timer0.p_ctrl, 5000, TIMER_UNIT_PERIOD_USEC);
 g_timer0.p_api->stop(g_timer0.p_ctrl);
 g_timer0.p_api->infoGet(g_timer0.p_ctrl, &info_timer);
 
 g_uart0.p_api->open(g_uart0.p_ctrl, g_uart0.p_cfg);
 
 while (1)
    {do
        err = g_uart0.p_api->read(g_uart0.p_ctrl, BufRead, 8);
     while(FlagRequest == 0);
     
         FlagRequest = 0;
         if((BufRead[0] == NAB) && ((BufRead[1] == 3)||(BufRead[1] == 4)))  // Nab == 2 et Fct == 3 ou 4 ?
            {i = (BufRead[2] << 8) + BufRead[3];
             j = (BufRead[4] << 8) + BufRead[5];
             if((i >= DEB_MAPPING) && (i <= FIN_MAPPING) && ((i + j) <= FIN_MAPPING))
                {crc = CalcCRC16 (BufRead, 6);
                 if((BufRead[7] == (crc >> 8) && (BufRead[6] == (crc & 0xFF))))
                 {Buff[0] = BufRead[0];
                  Buff[1] = BufRead[1];
                  Buff[2] = 2*BufRead[5];
                  for(i=0;i<Buff[2];i++)
                    Buff[3+i] = 0;
                  crc = CalcCRC16 (Buff, 3+Buff[2]);
                  Buff[3+Buff[2]] = crc & 0xFF;
                  Buff[4+Buff[2]] = (crc >> 8) & 0xFF;
                  err = R_IOPORT_PinWrite(IOPORT_PORT_04_PIN_13, IOPORT_LEVEL_HIGH);              // Set pin on port 6 bit 0 low
                  g_uart0.p_api->write(g_uart0.p_ctrl, Buff, 5+Buff[2]);
                 }
                }
            }        
         index = 0;
    }
}


void user_uart_callback(uart_callback_args_t * p_args)
{ssp_err_t err;                         // Create a variable for the error

 if (0U == p_args->channel)             // UART0
    {switch (p_args->event)
        {case UART_EVENT_RX_COMPLETE : g_timer0.p_api->start(g_timer0.p_ctrl);  // Use if Transfert Driver on r_dtc Event enabled
                                                                  g_timer0.p_api->reset(g_timer0.p_ctrl);
                                                                  break;
         case UART_EVENT_TX_COMPLETE : err = R_IOPORT_PinWrite(IOPORT_PORT_04_PIN_13, IOPORT_LEVEL_LOW);  // Set PdL low
         default                     :                       break;
        }
    }
}

The code work correctly, but i want to be sure that i receive 8 characters when i go to the callback fonction with

the UART_EVENT_RX_COMPLETE event.

Because i can have the UART_EVENT_RX_COMPLETE event with less than 8 characters

" DR flag (Receive Data Ready Flag
  When FRDRHL contains less data than the specified receive triggering number, and no next data was received after
  15 ETU
*1 from the last stop bit, and the SSR_FIFO.FER and SSR_FIFO.PER flags are 0"

The question is : how know how much characters i have received

    Thank you for your help


Viewing all articles
Browse latest Browse all 5781

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>