#include "Controller.h"

extern UART_HandleTypeDef huart4;

void ReadDataFromModbus(Controller *this)
{
	uint8_t loop_cnt,data_cnt;

	this->MODBUS_Tx_BUF[0]=this->SlaveID;
	this->MODBUS_Tx_BUF[1]=SlaveReadCommand;
	this->MODBUS_Tx_BUF[2]=(uint8_t)((this->StartAdress&0xFF00)>>8);
	this->MODBUS_Tx_BUF[3]=(uint8_t)(this->StartAdress&0x00FF);
	this->MODBUS_Tx_BUF[4]=(uint8_t)((this->NumOfAdress&0xFF00)>>8);
	this->MODBUS_Tx_BUF[5]=(uint8_t)(this->NumOfAdress&0x00FF);
	this->MODBUS_Tx_BUF[6]=0xC1;
	this->MODBUS_Tx_BUF[7]=0x4C;
	//crc low ve crc high olmali 6 ve 7. GetCRC(this->MODBUS_Tx_Buf,0);
	
	
	HAL_UART_Receive_IT(&huart4,&this->MODBUS_Rx_Char,1);
	memset(&this->MODBUS_Rx_BUF,0,BufLen);
	this->MODBUS_Rx_Counter=0;
	DERE_High;
	HAL_Delay(1);
	HAL_UART_Transmit(&huart4,this->MODBUS_Tx_BUF,0,1000);
	HAL_Delay(1);
	DERE_Low;
	HAL_Delay(5000);
	
	if(this->MODBUS_Rx_Counter++)
	{
		if(this->MODBUS_Rx_BUF[0]==this->SlaveID&& this->MODBUS_Rx_BUF[1]==SlaveReadCommand)
		{
			if(this->MODBUS_Rx_BUF[2]==this->NumOfAdress*2)
			{
				for(loop_cnt=0;loop_cnt<this->NumOfAdress;loop_cnt++)
				{
					switch(this->StartAdress+loop_cnt)
					{
						case Temperature_Address:
						{
							this->sv.Temperature=(float)((uint16_t)(this->MODBUS_Rx_BUF[data_cnt])*256+this->MODBUS_Rx_BUF[data_cnt+1]);
							this->sv.Temperature=this->sv.Temperature/10.0f;
							break;
						}
						case Humidity_Address:
						{
							this->sv.Humidity=(float)((uint16_t)(this->MODBUS_Rx_BUF[data_cnt])*256+this->MODBUS_Rx_BUF[data_cnt+1]);
							this->sv.Humidity=this->sv.Humidity/10.0f;
							break;
						}
						case Preasure_Address:
						{
							this->sv.Preasure=(float)((uint16_t)(this->MODBUS_Rx_BUF[data_cnt])*256+this->MODBUS_Rx_BUF[data_cnt+1]);
							break;
						}
						case Slope_Address:
						{
							this->sv.Slope=(float)((uint16_t)(this->MODBUS_Rx_BUF[data_cnt])*256+this->MODBUS_Rx_BUF[data_cnt+1]);
							break;
						}
						case Depth_Address:
						{
							this->sv.Depth=(float)((uint16_t)(this->MODBUS_Rx_BUF[data_cnt])*256+this->MODBUS_Rx_BUF[data_cnt+1]);
							break;
						}
					}
					data_cnt=data_cnt+2;
				}
			}
		}
	}
	
	
	
	

}