2023-02-09 03:24 AM
Hello Everyone.
I have one doubt.
Im Try to receive the serial data frame from dock light but .I cant able to receive serially.
my Frame HEX Formate :81 03 00 01
This my code:
#include "main.h"
#define LABEL 0x81
#define SSM 0x03
#define SDI 0x00
#define PARITY 0x01
uint16_t Rx_data[5];
uint16_t Read[1];
uint16_t msg[5];
uint16_t *Receive;
uint16_t RX[5];
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
while(1)
{
Receive=Transmit();
for(int j=0;j<4;j++)
{
HAL_UART_Transmit(&huart1, (uint8_t*)msg, 5, 10);
sprintf(msg,"%d ",Receive[j]);
}
if(Receive[0]==LABEL)
{
if(Receive[1]==SSM)
{
if(Receive[2]==SDI)
{
if(Receive[3]==PARITY)
{
HAL_UART_Transmit(&huart1, (uint8_t*)msg, 5, 10);
sprintf(msg,"%d ",Receive[0]);
}
}
}
}
}
}
int *Transmit()
{
uint8_t Read[5];
uint8_t RX[5];
for(int i=0;i<4;i++)
{
Read[i]=HAL_UART_Receive (&huart1, Rx_data, 4, 100);
}
return Read;
}
Please give the solution.
Output:Docklight
[TX]=81 03 00 01
[RX]=08 40 50 00
this my Experted output:
[TX]=81 03 00 01
[RX]=81 03 00 01
Solved! Go to Solution.
2023-02-09 09:56 PM
its not transmitingthe data actual give your hardware congfiguration
2023-02-09 07:44 AM
So many things.... To start with:
2023-02-09 07:28 PM
#define LABEL 0x81
#define SSM 0x03
#define SDI 0x00
#define PARITY 0x01
#define DATA_SIZE 4
#define ARRAY_SIZE 16
uint8_t rxData[ARRAY_SIZE] = {0};
uint8_t txData[ARRAY_SIZE] = {0};
bool dataRdy = false;
int main(void)
{
// init interrupt
UART_EnInt();
while (1)
{
if (dataRdy)
{
dataRdy = false;
if (rxData[0] == LABEL) {
if (rxData[1] == SSM) {
if (rxData[2] == SDI) {
if (rxData[3] == PARITY) {
memcpy(txData, rxData, DATA_SIZE);
HAL_UART_Transmit(&huart2, txData, DATA_SIZE, 100);
}
}
}
}
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
if(huart->Instance == huart2.Instance)
{
UART_EnInt();
if(Size == DATA_SIZE)
{
dataRdy = true;
}
}
}
// enable interrupt with idle
void UART_EnInt(void)
{
if (HAL_UARTEx_ReceiveToIdle_DMA(&huart2, rxData, ARRAY_SIZE ) != HAL_OK)
{
Error_Handler();
}
}
This code will give you a basic idea of how to receive binary data. When HAL_UARTEx_RxEventCallback() is called, the rxData already holds the data so you can set a flag so you know there is new data ready. Then check the flag in the while loop.
Using HAL_UARTEx_ReceiveToIdle_DMA() allows you to receive a variable length data packet. In this case you are wanting to received 4 bytes. So even though the DMA is expecting 16 bytes, the Idle bit will cause an interrupt after 4 bytes are sent from Docklight. You can check the Size to be sure you did receive 4 bytes for a complete packet and then set a flag.
The 1st packet matches your format so a copy of it is transmitted back to Docklight. The 2nd packet doesn't match so the code ignores it
2023-02-09 09:09 PM
Thank you bro.....i will compile
2023-02-09 09:10 PM
thank you bro
2023-02-09 09:56 PM
its not transmitingthe data actual give your hardware congfiguration
2023-02-09 09:57 PM