LORA通过扫描连接

This commit is contained in:
haifeng.wang 2025-05-21 14:04:38 +08:00
parent 38986bf14b
commit ea04ecf547
7 changed files with 129 additions and 74 deletions

View File

@ -29,9 +29,9 @@ const uint8_t HARDWARE_VER_MINOR = 137; //
const uint8_t SOFTWARE_VER_MAJOR = 1; const uint8_t SOFTWARE_VER_MAJOR = 1;
const uint8_t SOFTWARE_VER_MINOR = 1; // 罐箱RF网关 const uint8_t SOFTWARE_VER_MINOR = 1; // 罐箱RF网关
// 软件日期 // 软件日期
const uint8_t SOFTWARE_DATE_YEAR = 22; const uint8_t SOFTWARE_DATE_YEAR = 25;
const uint8_t SOFTWARE_DATE_MONTH = 11; const uint8_t SOFTWARE_DATE_MONTH = 5;
const uint8_t SOFTWARE_DATE_DAY = 17; const uint8_t SOFTWARE_DATE_DAY = 16;
// 存储格式版本号=1.1 // 存储格式版本号=1.1
const uint8_t CONFIG_FORMAT_VER_MAJOR = 1; const uint8_t CONFIG_FORMAT_VER_MAJOR = 1;
@ -190,7 +190,7 @@ void Config_setBottleDefParam(data_buff_t *dataBuff)
dataBuff->configBottle.warnTempr = -162; dataBuff->configBottle.warnTempr = -162;
dataBuff->configBottle.warnTemprH = 50; dataBuff->configBottle.warnTemprH = 50;
dataBuff->configBottle.offlineSeconds = 14400; dataBuff->configBottle.offlineSeconds = 14400;
dataBuff->configBottle.lora_freq=0;
Config_ForceValid((rd_buff_t *) &dataBuff->configBottle, CAN_DC_BOTTLE_CONFIG); Config_ForceValid((rd_buff_t *) &dataBuff->configBottle, CAN_DC_BOTTLE_CONFIG);
} }

View File

@ -257,7 +257,8 @@ typedef struct
int16_t warnTempr; // 温度报警下限 int16_t warnTempr; // 温度报警下限
int16_t warnTemprH; // 温度报警上限 int16_t warnTemprH; // 温度报警上限
}; };
uint8_t reserved[34]; uint8_t reserved[32];
uint16_t lora_freq;
uint32_t offlineSeconds; // 离线判别时间(中继器本地用) uint32_t offlineSeconds; // 离线判别时间(中继器本地用)
uint16_t crc; uint16_t crc;
} config_bottle_t; } config_bottle_t;

View File

@ -803,17 +803,17 @@ void Form_Task(void *p_arg)
seconds = rf_get_seconds(); seconds = rf_get_seconds();
//apex //apex
// // LORA: 最新接收时间在3秒以内 // // LORA: 最新接收时间在3秒以内
// val = (RF_initStatus && RF_seconds > 0 && RF_seconds + 3 > seconds); val = (RF_initStatus && RF_seconds > 0 && RF_seconds + 3 > seconds);
// if(last_lora != val) if(last_lora != val)
// { {
// data.oper = DWIN_OPER_WRITE; data.oper = DWIN_OPER_WRITE;
// data.Addr = 0x5002; data.Addr = 0x5002;
// data.count = 2; data.count = 2;
// data.word[0] = htons(val); data.word[0] = htons(val);
// DWIN_Refresh(&data); DWIN_Refresh(&data);
// last_lora = val; last_lora = val;
// } }
// GPS标志: 最新定位时间在180秒以内 // GPS标志: 最新定位时间在180秒以内
if(dcBuff.dtuData.posState) if(dcBuff.dtuData.posState)
@ -1256,6 +1256,11 @@ void Form_RestoreParam()
formData.Addr = 0x5020; formData.Addr = 0x5020;
formData.count = 4; formData.count = 4;
formData.dword[0] = htonl(dcBuff.configBottle.offlineSeconds); formData.dword[0] = htonl(dcBuff.configBottle.offlineSeconds);
LoopBuff_PutItem(&Form_DataM, (uint8_t *) &formData);
formData.Addr = 0x5038;
formData.count = 2;
formData.dword[0] = htons(dcBuff.configBottle.lora_freq);
LoopBuff_PutItem(&Form_DataM, (uint8_t *) &formData); LoopBuff_PutItem(&Form_DataM, (uint8_t *) &formData);
// 解锁 // 解锁
@ -1268,7 +1273,7 @@ void Form_RestoreParam()
// 保存参数 // 保存参数
uint8_t Form_SaveParam() uint8_t Form_SaveParam()
{ {
uint16_t emptyPct, fullPct; uint16_t emptyPct, fullPct, lora_freq;
uint32_t warnPress, warnPressH, offlineSeconds; uint32_t warnPress, warnPressH, offlineSeconds;
int32_t warnTempr, warnTemprH; int32_t warnTempr, warnTemprH;
uint8_t save = 0; uint8_t save = 0;
@ -1287,7 +1292,9 @@ uint8_t Form_SaveParam()
return 0; return 0;
if(!Form_ReadUInt32(0x5020, &offlineSeconds)) if(!Form_ReadUInt32(0x5020, &offlineSeconds))
return 0; return 0;
if(!Form_ReadUInt16(0x5038, &lora_freq))
return 0;
if(emptyPct * 100 != dcBuff.configBottle.emptyPct) if(emptyPct * 100 != dcBuff.configBottle.emptyPct)
{ {
dcBuff.configBottle.emptyPct = emptyPct * 100; dcBuff.configBottle.emptyPct = emptyPct * 100;
@ -1323,7 +1330,11 @@ uint8_t Form_SaveParam()
dcBuff.configBottle.offlineSeconds = offlineSeconds; dcBuff.configBottle.offlineSeconds = offlineSeconds;
save = 1; save = 1;
} }
if(lora_freq != dcBuff.configBottle.lora_freq)
{
dcBuff.configBottle.lora_freq = lora_freq;
save = 1;
}
if(save) if(save)
Config_SaveConfig(); Config_SaveConfig();

View File

@ -206,24 +206,29 @@ void Config_Open()
delay_ms(20); delay_ms(20);
SFlash_UnlockBPR(); SFlash_UnlockBPR();
// 设置默认参数(默认参数由另一个程序写入,本程序只读出来用)
// 贴片厂测试软件打开
Config_setBottleDefParam(&dcBuff);
Config_setSensorDefParam(&dcBuff);
Config_setDataDefParam(&dcBuff);
Config_setDisplayDefParam(&dcBuff);
// 读取保存的配置 // 读取保存的配置
// 贴片厂测试软件关闭 // 贴片厂测试软件关闭
Config_LoadConfig(); Config_LoadConfig();
// // 测试代码 // apex// 测试代码
dcBuff.configBottle.PSN[0] = 25; // PSN: 2025092980002 // dcBuff.configBottle.PSN[0] = 25; // PSN: 2025092980002
dcBuff.configBottle.PSN[1] = 9; // dcBuff.configBottle.PSN[1] = 9;
dcBuff.configBottle.PSN[2] = 29; // dcBuff.configBottle.PSN[2] = 29;
dcBuff.configBottle.PSN[3] = 80; // dcBuff.configBottle.PSN[3] = 80;
dcBuff.configBottle.PSN[4] = 0; // dcBuff.configBottle.PSN[4] = 0;
dcBuff.configBottle.PSN[5] = 2; // dcBuff.configBottle.PSN[5] = 2;
if(0== dcBuff.configBottle.offlineSeconds)
{
// 设置默认参数(默认参数由另一个程序写入,本程序只读出来用)
// 贴片厂测试软件打开
Config_setBottleDefParam(&dcBuff);
Config_setSensorDefParam(&dcBuff);
Config_setDataDefParam(&dcBuff);
Config_setDisplayDefParam(&dcBuff);
}
Config_SaveConfig(); Config_SaveConfig();
// dcBuff.configData.intervalTrans = 120; // dcBuff.configData.intervalTrans = 120;

View File

@ -145,13 +145,13 @@ void Modbus_Task(void *p_arg)
if(LoopBuff_GetCount(&Modbus_SendM)) if(LoopBuff_GetCount(&Modbus_SendM))
{ {
// 取数据 // // 取数据
memmove(&Gprs, LoopBuff_GetDataPtr(&Modbus_SendM, Modbus_SendM.info.rdPtr), sizeof(ext_data_t)); memmove(&Gprs, LoopBuff_GetDataPtr(&Modbus_SendM, Modbus_SendM.info.rdPtr), sizeof(ext_data_t));
LoopBuff_RemoveItems(&Modbus_SendM, 1); LoopBuff_RemoveItems(&Modbus_SendM, 1);
i = pack_modbus_tran_data(&Gprs); // i = pack_modbus_tran_data(&Gprs);
// TODO: 发送数据到上位机,不等待返回 // // TODO: 发送数据到上位机,不等待返回
RS485_SendDataByte(Modbus_sendBuff, i); // RS485_SendDataByte(Modbus_sendBuff, i);
} }
} }
} }
@ -162,12 +162,14 @@ void MODBUS_RTU_CMD(modbus_request_t *req)
static uint8_t BUFF2[100]; // 响应包 static uint8_t BUFF2[100]; // 响应包
uint8_t err = 0; // 错误代码 uint8_t err = 0; // 错误代码
uint8_t len = 0; // 发送数据长度 uint8_t len = 0; // 发送数据长度
ext_bottle_t bottleRec;
uint16_t count; // 通信地址 uint16_t count; // 通信地址
// uint16_t reg; // 数据地址 // uint16_t reg; // 数据地址
uint16_t cnt; // 数据个数 uint16_t cnt; // 数据个数
uint16_t crc; uint16_t crc;
ext_box_t boxRec; ext_box_t boxRec;
ext_data_t Gprs;
uint8_t i;
char* psn; char* psn;
// // 检查通信地址 // // 检查通信地址
// 添加记录 // 添加记录
@ -193,12 +195,20 @@ void MODBUS_RTU_CMD(modbus_request_t *req)
{ {
cnt = req->index; // 参数个数 cnt = req->index; // 参数个数
memset(&boxRec, 0, sizeof(ext_box_t)); memset(&boxRec, 0, sizeof(ext_box_t));
memset(&bottleRec, 0, sizeof(ext_bottle_t));
boxRec.PSN[0] = req->PSN[0]; boxRec.PSN[0] = req->PSN[0];
boxRec.PSN[1] = req->PSN[1]; boxRec.PSN[1] = req->PSN[1];
boxRec.PSN[2] = req->PSN[2]; boxRec.PSN[2] = req->PSN[2];
boxRec.PSN[3] = req->PSN[3]; boxRec.PSN[3] = req->PSN[3];
boxRec.PSN[4] = req->PSN[4]; boxRec.PSN[4] = req->PSN[4];
boxRec.PSN[5] = req->PSN[5]; boxRec.PSN[5] = req->PSN[5];
bottleRec.PSN[0] = req->PSN[0];
bottleRec.PSN[1] = req->PSN[1];
bottleRec.PSN[2] = req->PSN[2];
bottleRec.PSN[3] = req->PSN[3];
bottleRec.PSN[4] = req->PSN[4];
bottleRec.PSN[5] = req->PSN[5];
for(int i=0; i<11; i++) for(int i=0; i<11; i++)
boxRec.TGGU[i]=req->BOX[i]; boxRec.TGGU[i]=req->BOX[i];
boxRec.TGGU[11]=0; boxRec.TGGU[11]=0;
@ -206,6 +216,9 @@ void MODBUS_RTU_CMD(modbus_request_t *req)
FRAM_BufferWrite(FRAM_BOX_DATA_BASE + sizeof(ext_box_t) * count, (uint8_t *) &boxRec, sizeof(ext_box_t)); FRAM_BufferWrite(FRAM_BOX_DATA_BASE + sizeof(ext_box_t) * count, (uint8_t *) &boxRec, sizeof(ext_box_t));
// // 更新索引信息 // // 更新索引信息
bottleRec.lastData = -1;
FRAM_BufferWrite(FRAM_BOTTLE_DATA_BASE + sizeof(ext_bottle_t) * count, (uint8_t *) &bottleRec, sizeof(ext_bottle_t));
// strcpy(boxTGGU[count].TGGU, boxRec.TGGU); // strcpy(boxTGGU[count].TGGU, boxRec.TGGU);
// memmove(boxTGGU[count].PSN, boxRec.PSN, 6); // memmove(boxTGGU[count].PSN, boxRec.PSN, 6);
// boxTGGU[count].recNo = count; // boxTGGU[count].recNo = count;
@ -216,11 +229,11 @@ void MODBUS_RTU_CMD(modbus_request_t *req)
if(req->index==req->all) if(req->index==req->all)
{ {
boxCnt.count=req->all; boxCnt.count=req->all;
bottleCnt.count = req->all;
FRAM_SaveInfo(FRAM_BOX_INFO_BASE, (uint8_t *) &boxCnt, sizeof(ext_count_t)); FRAM_SaveInfo(FRAM_BOX_INFO_BASE, (uint8_t *) &boxCnt, sizeof(ext_count_t));
FRAM_SaveInfo(FRAM_BOTTLE_INFO_BASE, (uint8_t *) &bottleCnt, sizeof(ext_count_t));
} }
}
memset(BUFF2, 0, sizeof(BUFF2)); memset(BUFF2, 0, sizeof(BUFF2));
BUFF2[len++]='L'; BUFF2[len++]='L';
BUFF2[len++]='S'; BUFF2[len++]='S';
@ -237,6 +250,23 @@ void MODBUS_RTU_CMD(modbus_request_t *req)
memmove(BUFF2 + len, &crc, 2); memmove(BUFF2 + len, &crc, 2);
len += 2; len += 2;
RS485_SendDataByte(BUFF2, len); RS485_SendDataByte(BUFF2, len);
}
else if(req->cmd == 0x05)
{
if(LoopBuff_GetCount(&Modbus_SendM))
{
// 取数据
memmove(&Gprs, LoopBuff_GetDataPtr(&Modbus_SendM, Modbus_SendM.info.rdPtr), sizeof(ext_data_t));
LoopBuff_RemoveItems(&Modbus_SendM, 1);
i = pack_modbus_tran_data(&Gprs);
// TODO: 发送数据到上位机,不等待返回
RS485_SendDataByte(Modbus_sendBuff, i);
}
}
} }

View File

@ -23,8 +23,8 @@ extern IWDG_HandleTypeDef hiwdg;
const uint8_t RF_APP = RF_APP_AJH; const uint8_t RF_APP = RF_APP_AJH;
const uint8_t RF_PROTOCOL_VER = RF_PROTOCOL_VER_1; const uint8_t RF_PROTOCOL_VER = RF_PROTOCOL_VER_1;
const uint8_t RF_UP_CHANNEL = 28; // 上行信道: 498M uint8_t RF_UP_CHANNEL = 9; // 上行信道: 498M
const uint8_t RF_DOWN_CHANNEL = 29; // 下行信道: 499M uint8_t RF_DOWN_CHANNEL = 8; // 下行信道: 499M
const uint8_t RF_BROADCAST_PSN[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; const uint8_t RF_BROADCAST_PSN[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
const uint8_t RF_EMPTYPSN[6] = {0, 0, 0, 0, 0, 0}; const uint8_t RF_EMPTYPSN[6] = {0, 0, 0, 0, 0, 0};
uint8_t RF_GatewayPSN[6] = {0, 0, 0, 0, 0, 0}; uint8_t RF_GatewayPSN[6] = {0, 0, 0, 0, 0, 0};
@ -336,37 +336,43 @@ uint8_t rf_uart_send(rf_frame_t *frame)
else else
dst = rf_get_addr(frame->destPSN); dst = rf_get_addr(frame->destPSN);
#if 0 #if 0
phy_header[0] = dst >> 8; // phy_header[0] = dst >> 8;
phy_header[1] = dst & 0xFF; // phy_header[1] = dst & 0xFF;
// 信道 // // 信道
if(frame->dir == RF_DIR_UP) // if(frame->dir == RF_DIR_UP)
phy_header[2] = RF_UP_CHANNEL; // phy_header[2] = RF_UP_CHANNEL;
else // else
phy_header[2] = RF_DOWN_CHANNEL; // phy_header[2] = RF_DOWN_CHANNEL;
// while(!RF_READY()); //// while(!RF_READY());
// delay_ms((rand() % 10) * (70 + rand() % 20)); // 多个中继器,避免冲突 //// delay_ms((rand() % 10) * (70 + rand() % 20)); // 多个中继器,避免冲突
delay_ms((rand() % 3) * (10 + rand() % 10)); // 单个中继器,提高效率 // delay_ms((rand() % 3) * (10 + rand() % 10)); // 单个中继器,提高效率
//
printf("\nRF send to Module (%02X %02X %02X, %d bytes):\n", // printf("\nRF send to Module (%02X %02X %02X, %d bytes):\n",
phy_header[0], phy_header[1], phy_header[2], frame->len); // phy_header[0], phy_header[1], phy_header[2], frame->len);
for(i = 0; i < frame->len; i++) // for(i = 0; i < frame->len; i++)
printf(" %02X", ((uint8_t *) frame)[i]); // printf(" %02X", ((uint8_t *) frame)[i]);
printf("\n"); // printf("\n");
//
// 定点发送 // // 定点发送
// 因为射频模块接收数据不能中断超过3个字节的时间故不允许打断。 // // 因为射频模块接收数据不能中断超过3个字节的时间故不允许打断。
__disable_irq(); // __disable_irq();
UART_Transmit(&huart8, phy_header, 3); // UART_Transmit(&huart8, phy_header, 3);
UART_Transmit(&huart8, (uint8_t *) frame, frame->len); // UART_Transmit(&huart8, (uint8_t *) frame, frame->len);
__enable_irq(); // __enable_irq();
#else #else
// 信道 // 信道
if(frame->dir == RF_DIR_UP) // if(frame->dir == RF_DIR_UP)
phy_header[0] = RF_UP_CHANNEL; // {
else // phy_header[0] = (dcBuff.configBottle.lora_freq%3)*2+7;//RF_UP_CHANNEL;
phy_header[0] = RF_DOWN_CHANNEL; // phy_header[1] = (dcBuff.configBottle.lora_freq%3)*2+6;//RF_DOWN_CHANNEL;
// }
// else
// {}
phy_header[0] = (dcBuff.configBottle.lora_freq%3)*2+7;//RF_DOWN_CHANNEL;
phy_header[1] = (dcBuff.configBottle.lora_freq%3)*2+6;//RF_UP_CHANNEL;
// while(!RF_READY()); // while(!RF_READY());
@ -382,7 +388,7 @@ uint8_t rf_uart_send(rf_frame_t *frame)
// 定点发送 // 定点发送
// 因为射频模块接收数据不能中断超过3个字节的时间故不允许打断。 // 因为射频模块接收数据不能中断超过3个字节的时间故不允许打断。
__disable_irq(); __disable_irq();
UART_Transmit(&huart8, phy_header, 1); UART_Transmit(&huart8, phy_header, 2);
UART_Transmit(&huart8, (uint8_t *) frame, frame->len); UART_Transmit(&huart8, (uint8_t *) frame, frame->len);
__enable_irq(); __enable_irq();
#endif #endif
@ -649,7 +655,7 @@ void RF_PowerOn()
cfg[1] = addr >> 8; cfg[1] = addr >> 8;
cfg[2] = addr & 0xFF; cfg[2] = addr & 0xFF;
cfg[3] = 6 | (7 << 3) | (0 << 6); // 115200bps cfg[3] = 6 | (7 << 3) | (0 << 6); // 115200bps
cfg[4] = RF_UP_CHANNEL; cfg[4] = (dcBuff.configBottle.lora_freq%3)*2+6;//RF_UP_CHANNEL;
cfg[5] = 1 | (1 << 2) | (9 << 3) | (0 << 7); // freqcast=1, bw=500kHz, unicast=0 cfg[5] = 1 | (1 << 2) | (9 << 3) | (0 << 7); // freqcast=1, bw=500kHz, unicast=0
printf("\nInitialize RF Module: %02X %02X %02X %02X %02X %02X\n", printf("\nInitialize RF Module: %02X %02X %02X %02X %02X %02X\n",
@ -735,11 +741,12 @@ void RF_GatewayTask(void *p_arg)
if(RTC_offsetSeconds == 0) if(RTC_offsetSeconds == 0)
{ {
printf("\nRTC_offsetSeconds = 0, ignored ...\n"); printf("\nRTC_offsetSeconds = 0, ignored ...\n");
continue; //apex continue;
} }
// 处理帧 // 处理帧
idx = Ext_Lookup_Bottle_PSN(RF_ProcFrame.srcPSN, bottleCnt.count);
if(idx == -1) continue;
// 是否需要回应ack // 是否需要回应ack
if(RF_ProcFrame.mac_ack_req) if(RF_ProcFrame.mac_ack_req)
{ {
@ -777,7 +784,7 @@ void RF_GatewayTask(void *p_arg)
incCount = 0; incCount = 0;
newPSN = 1; newPSN = 1;
idx = Ext_Lookup_Bottle_PSN(RF_ProcFrame.srcPSN, bottleCnt.count); // idx = Ext_Lookup_Bottle_PSN(RF_ProcFrame.srcPSN, bottleCnt.count);
if(idx >= 0) if(idx >= 0)
{ {
newPSN = 0; // 已有PSN newPSN = 0; // 已有PSN
@ -805,7 +812,7 @@ void RF_GatewayTask(void *p_arg)
if(idx >= 0) if(idx >= 0)
{ {
// 修改储罐档案 // 修改储罐档案
if(incCount || newPSN) if(incCount || newPSN)
{ {
memset(&bottleRec, 0, sizeof(ext_bottle_t)); memset(&bottleRec, 0, sizeof(ext_bottle_t));

View File

@ -300,7 +300,8 @@ void UA0_Response(char *s)
if(UA0_RxBuf_offset == 0 && UA0_RxBuf_Length > 1) if(UA0_RxBuf_offset == 0 && UA0_RxBuf_Length > 1)
{ {
sendCh = UA0_RxBuf[0]; sendCh = UA0_RxBuf[0];
UA0_RxBuf_offset = 1; // 不发送第1发个字节 Lora_Param.ch= UA0_RxBuf[1];
UA0_RxBuf_offset = 2; // 不发送第1发个字节
} }
} }