/* * msp_485_android.c * * Created on: 2025年12月1日 * Author: akeguo */ #include "robot_state.h" #include "msp_485_android.h" #include "msp_U7.h" #include "bsp_Error_Detect.h" void decode_android_485(uint8_t *buffer, uint16_t length); void decode_android_PV(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; int time_to_adapt = 100; 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 = 20; //+ 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, time_to_adapt, decode_android_485); } int PMK32Index = 0; int received_android_counter = 0; int received_counter_max = 10/2;// 每次读取多少次遥控器然后读取PV/写IV int32_t *p_mk32_pointer; //读取 的寄存器 void decode_android_485(uint8_t *buffer, uint16_t length) { // uint8_t data1[length]; // 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++; } received_android_counter++; //读取遥控器 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, time_to_adapt, decode_android_485); if (received_android_counter % received_counter_max != 0) return; switch (received_android_counter / received_counter_max % 2) { case 0: { //设定安卓数据 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, 150, NULL); received_android_counter = 0; break; } case 1: { //读取安卓数据 03 PV MB_ReadHoldingReg(&android_handler->Tx_Buf, &android_handler->TxCount, android_slave_id, 19, (Decoded_Android_HoldingReg_Value[18] + 1) / 2); android_handler->AddSendList(android_handler, android_handler->Tx_Buf, android_handler->TxCount, time_to_adapt, decode_android_PV); break; } } } else { LOGFF(DL_ERROR, "Android_485 decoding failed"); } } /*****/ void decode_android_PV(uint8_t *buffer, uint16_t length) { // uint8_t data1[1000]; // memcpy(data1, buffer, length); if (length <= 5) { return; } int decoded_result = MB_Decode_HoldingRegs(buffer, length, (Decoded_Android_HoldingReg_Value[18] + 1) / 2, &Decoded_Android_HoldingReg_Value[19]); if (decoded_result == 1) { //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]); int checkreturn = pb_decode(&i_pv_stream, PV_struct_define_fields, &_decoded_PV_temp); if (checkreturn == 1) { if (GV.PV.TimeStamp <= _decoded_PV_temp.TimeStamp) { GV.PV = _decoded_PV_temp; } } HardWareErrorController->Set_PCOMHardWare(HardWareErrorController, "Android_485", 1); } else { LOGFF(DL_ERROR, "Android_485 decoding failed"); } }