You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
3.8 KiB
124 lines
3.8 KiB
/*
|
|
* msp_485_android.c
|
|
*
|
|
* Created on: 2025年12月1日
|
|
* Author: akeguo
|
|
*/
|
|
#include "msp_485_android.h"
|
|
#include "msp_U7.h"
|
|
void decode_android_485(uint8_t *buffer, uint16_t length);
|
|
void android_485_loop(); //inqure data from the, in fact this is a modbus 03 function;
|
|
|
|
struct UARTHandler *android_handler;
|
|
|
|
DispacherController *androidr_dispacherController;
|
|
int android_slave_id = 0x40;
|
|
|
|
int8_t android_slave_iv_start_holiding_register = 100;
|
|
int16_t android_slave_iv_holiding_register_num = 0; //这个数据要随时改变
|
|
|
|
//最多读取多少个字节
|
|
int16_t read_android_holidng_register_count = 18 + 1
|
|
+ PV_struct_define_size / 2; //16个subs通道 1个PV数据长度,
|
|
|
|
|
|
uint16_t Decoded_Android_HoldingReg_Value[100]; //用来对Android的Sbus /PV /IV 的操作
|
|
uint16_t send_pv_android_holdingReg_Value[100]; // /PV /IV 的操作
|
|
|
|
void android_485_intialize(struct UARTHandler *Handler)
|
|
{
|
|
android_handler = Handler;
|
|
android_handler->UART_Decode = decode_android_485;
|
|
|
|
androidr_dispacherController = Handler->dispacherController;
|
|
androidr_dispacherController->Dispacher_Enable = 1;
|
|
//不周期性发送
|
|
androidr_dispacherController->Add_Dispatcher_List(
|
|
androidr_dispacherController, android_485_loop);
|
|
|
|
HardWareErrorController->Add_PCOMHardWare(HardWareErrorController,
|
|
"Android_485", 0, ComError_Android_485);
|
|
LOG("msp_485_android_intialize");
|
|
}
|
|
|
|
void android_485_intialize_with_slaveID(struct UARTHandler *Handler,
|
|
int slave_id)
|
|
{
|
|
android_485_intialize(Handler);
|
|
android_slave_id = slave_id;
|
|
}
|
|
|
|
void android_485_loop()
|
|
{
|
|
//读取安卓数据
|
|
MB_ReadHoldingReg(&android_handler->Tx_Buf, &android_handler->TxCount,
|
|
android_slave_id, 0, read_android_holidng_register_count);
|
|
android_handler->AddSendList(android_handler, android_handler->Tx_Buf,
|
|
android_handler->TxCount, OneLineWaitTime, decode_android_485);
|
|
|
|
//设定安卓数据
|
|
pb_ostream_t IV_o_stream = pb_ostream_from_buffer(
|
|
&send_pv_android_holdingReg_Value[1],
|
|
sizeof(send_pv_android_holdingReg_Value) - 1);
|
|
pb_encode(&IV_o_stream, IV_struct_define_fields, &IV);
|
|
|
|
android_slave_iv_holiding_register_num = (IV_o_stream.bytes_written + 1) / 2
|
|
+ 1;
|
|
send_pv_android_holdingReg_Value[0] = SWAP_ENDIAN_16(
|
|
IV_o_stream.bytes_written);
|
|
MB_WriteNumHoldingReg(&android_handler->Tx_Buf, &android_handler->TxCount,
|
|
android_slave_id, android_slave_iv_start_holiding_register,
|
|
android_slave_iv_holiding_register_num,
|
|
send_pv_android_holdingReg_Value);
|
|
android_handler->AddSendList(android_handler, android_handler->Tx_Buf,
|
|
android_handler->TxCount, OneLineWaitTime, NULL);
|
|
|
|
}
|
|
int PMK32Index = 0;
|
|
int32_t *p_mk32_pointer;
|
|
//读取 的寄存器
|
|
void decode_android_485(uint8_t *buffer, uint16_t length)
|
|
{
|
|
uint8_t data1[100];
|
|
memcpy(data1, buffer, length);
|
|
if (length <= 5)
|
|
{
|
|
return;
|
|
}
|
|
int decoded_result = MB_Decode_HoldingRegs(buffer, length,
|
|
read_android_holidng_register_count,
|
|
&Decoded_Android_HoldingReg_Value[0]);
|
|
if (decoded_result == 1)
|
|
{
|
|
p_mk32_pointer = (int32_t*) P_U7;
|
|
//Sbus 设定
|
|
|
|
for (int PMK32Index = 0; PMK32Index < 18; PMK32Index++)
|
|
{
|
|
//此处必须加类型转换
|
|
*p_mk32_pointer = (int16_t)Decoded_Android_HoldingReg_Value[PMK32Index];
|
|
p_mk32_pointer++;
|
|
}
|
|
|
|
//PV解析
|
|
pb_istream_t i_pv_stream =
|
|
{ 0 };
|
|
|
|
i_pv_stream = pb_istream_from_buffer(
|
|
&Decoded_Android_HoldingReg_Value[19],
|
|
Decoded_Android_HoldingReg_Value[18]);
|
|
pb_decode(&i_pv_stream, PV_struct_define_fields, &_decoded_PV_temp);
|
|
if (decoded_PV.TimeStamp <= _decoded_PV_temp.TimeStamp)
|
|
{
|
|
decoded_PV = _decoded_PV_temp;
|
|
}
|
|
|
|
HardWareErrorController->Set_PCOMHardWare(HardWareErrorController,
|
|
"Android_485", 1);
|
|
|
|
}
|
|
else
|
|
{
|
|
LOGFF(DL_ERROR, "Android_485 decoding failed");
|
|
}
|
|
}
|
|
|