/*
 * msp_Force_Sensor.c
 *
 *  Created on: Jul 9, 2024
 *      Author: Administrator
 */



#include "MSP/msp_Force_Sensor.h"
#include "MSP/msp_Modbus_crc16.h"


UART_HandleTypeDef* Huart_Force_Sensor;
uint8_t FS_stas[14];
SP_MSP_Force_Value *FS_Va_BING;
int32_t FS_Falg=1;
void GF_FS_Unscramble_Callback();
void FS_Compute();

uint8_t GF_MSP_FS_Init(UART_HandleTypeDef* huart,SP_MSP_Force_Value *_FS_Val)
{

	Huart_Force_Sensor=huart;
	GF_BSP_Interrupt_Add_CallBack(DF_BSP_InterCall_RS485_1_RxCpltCallback, GF_FS_Unscramble_Callback);//这句话的理解有问题
	HAL_UART_Receive_IT(Huart_Force_Sensor,FS_stas,1);
	FS_Va_BING= _FS_Val;
	return 1;
}

void GF_FS_Inquiry()
{
	uint8_t Inquiry_Order[8]={0X01, 0X03, 0X07, 0XD0, 0X00, 0X02, 0XC4, 0X86};
	GF_BSP_UART_Transmit(1, Inquiry_Order, 8);//这句话的关联有问题
}

uint8_t FS_stas_All_Data[14]={0X01, 0X03, 0X04, 0X00, 0X00, 0X00, 0XE8, 0X86,0X00 };
void  GF_FS_Unscramble_Callback()
{
	unsigned short CRC_Value;
	switch(FS_Falg)
	{
	case 1:
		if(FS_stas[0]==0X01)
		{
		HAL_UART_Receive_IT(Huart_Force_Sensor,FS_stas,2);
		FS_Falg=2;
		}
		else
		{
		HAL_UART_Receive_IT(Huart_Force_Sensor,FS_stas,1);
		FS_Falg=1;
		}
		break;
	case 2:
		if((FS_stas[0]==0X03)&&(FS_stas[1]==0X04))
		{
		HAL_UART_Receive_IT(Huart_Force_Sensor,FS_stas,4);
		FS_Falg=3;
		}
		else
		{
		HAL_UART_Receive_IT(Huart_Force_Sensor,FS_stas,1);
		FS_Falg=1;
		}
		break;
	case 3:
		FS_stas_All_Data[3]=FS_stas[0];
		FS_stas_All_Data[4]=FS_stas[1];
		FS_stas_All_Data[5]=FS_stas[2];
		FS_stas_All_Data[6]=FS_stas[3];

		CRC_Value=ModBusCRC16(FS_stas_All_Data, 7); // CRC校验
		FS_stas_All_Data[7]= (uint8_t)CRC_Value;
		FS_stas_All_Data[8]= (uint8_t)( CRC_Value>>8);
		HAL_UART_Receive_IT(Huart_Force_Sensor,FS_stas,2);
		FS_Falg=4;
		break;
	case 4:
		if((FS_stas_All_Data[7]==FS_stas[0])&&(FS_stas_All_Data[8]==FS_stas[1]))
		{
			FS_Compute(FS_stas);
		}
		else
		{
			FS_Va_BING->Force_Value=0;
		}
		HAL_UART_Receive_IT(Huart_Force_Sensor,FS_stas,1);
		FS_Falg=1;
		break;

	}
}





void FS_Compute()
{
	FS_Va_BING->Force_Value=(int32_t)((int32_t)FS_stas_All_Data[3]<<24|FS_stas_All_Data[4]<<16|FS_stas_All_Data[5]<<8|FS_stas_All_Data[6]);
	if(FS_Va_BING->Force_Value<0)
	{
		FS_Va_BING->Force_Value=0;
	}
}













