• 关于医疗器械的算法、协议开发(五)


    监控芯片接收内容:

    #include "ProtocolManager.h"
    #include "ProtocolHeartBeat.h"
    #include "ProtocolCommunicate.h"
    #include "ProtocolAlarm.h"
    #include "ProtocolWirelessRawData.h"
    #include "ProtocolSpo2.h"
    
    ProtocolManager * ProtocolManager::_instance = NULL;
    
    ProtocolManager::ProtocolManager()
        :_circleBuf(2048)
    {
        _allProtocolObjs.clear();
    #if USING_CMD_LIST
        _cmds.clear();
    #endif
        _proDrv = NULL;
    }
    
    ProtocolManager::~ProtocolManager()
    {
    
    }
    
    int ProtocolManager::registerProtocol(ProtocolObj *obj)
    {
        int ret = 0;
    
        if (!obj)
        {
            return 1;
        }
        list<ProtocolObj *>::iterator it = _allProtocolObjs.begin();
        for (;it != _allProtocolObjs.end(); it++)
        {
            if (*it == obj)
            {
                ret = 1;
                return ret;
            }
        }
    
        obj->init();
        _allProtocolObjs.push_back(obj);
        return ret;
    }
    
    int ProtocolManager::unregisterProtocol(ProtocolObj *obj)
    {
        int ret = 1;
        if (!obj)
        {
            return ret;
        }
        list<ProtocolObj *>::iterator it = _allProtocolObjs.begin();
        for (;it != _allProtocolObjs.end(); it++)
        {
            if (*it == obj)
            {
                ret = 0;
                _allProtocolObjs.erase(it);
                obj->deinit();
                return ret;
            }
        }
        return ret;
    }
    
    int ProtocolManager::loopRun()
    {
        if (_proDrv)
            _proDrv->loopRun();
        list<ProtocolObj *>::iterator it = _allProtocolObjs.begin();
        for (;it != _allProtocolObjs.end(); it++)
        {
            ProtocolObj *obj = *it;
            if (obj)
            {
                obj->loopRun();
            }
        }
    
        handleProtocol();
    #if USING_CMD_LIST
        handleSendCmds();
    #endif
        return 0;
    }
    
    void ProtocolManager::init()
    {
        ProtocolHeartBeat *pHeartBeat = new ProtocolHeartBeat(this);
        registerProtocol(pHeartBeat);
    
        ProtocolAlarm *pAlarm = new ProtocolAlarm(this);
        registerProtocol(pAlarm);
    
        ProtocolWirelessRawData *pWireless = new ProtocolWirelessRawData(this);
        registerProtocol(pWireless);
    
        ProtocolSpo2 *pSpo2 = new ProtocolSpo2(this);
        registerProtocol(pSpo2);
    
        ProtocolCommunicate *pCommunicate = new ProtocolCommunicate();
        registerProDrv(pCommunicate);
    }
    
    void ProtocolManager::registerProDrv(ProtocolDrv *drv)
    {
        if (drv)
        {
            drv->init();
            _proDrv = drv;
        }
    }
    
    int ProtocolManager::checkData(uint8_t *buf, int len)
    {
        int sum = 0;
        for (int i = 0; i < len; i++)
        {
            sum += buf[i];
        }
    
        return sum;
    }
    
    #if 0
    void ProtocolManager::handleProtocol()
    {
        int loopCnt = 0;
        while (_circleBuf.length() >= 7)
        {
            loopCnt++;
            if (loopCnt >= 50)
            {
                break;//防止出现死循环
            }
            uint8_t startCh = _circleBuf[0];
            if (startCh == 0xAA)
            {
                uint8_t h1 = _circleBuf[1];
                uint8_t h2 = _circleBuf[2];
                uint8_t h3 = _circleBuf[3];
                uint8_t h4 = _circleBuf[4];
                uint16_t len = (h3 << 8) | h4;
                //这里存在一个风险,如果数据长度即 _circleBuf[3]_circleBuf[4]数据错位那么就会导致我们buff存满永远处理不了数据
                //例如我们循环队列长度是2048 但是len解析出来是4096 那么就会出现错误
                //所以我们这里给buffer一个阀值
                //如果这包协议中len的长度超出我们的这个值就将数据推出buffer
                if (len > 500)
                {
                    _circleBuf.popData();
                    continue;
                }
                if (_circleBuf.length() < len + 7)
                {
                    break;
                }
                else
                {
                    uint8_t *buf = new uint8_t[len + 7];
                    for (int i = 0; i < len + 7; i++)
                    {
                        buf[i] = _circleBuf.popData();
                    }
                    int checkRcv = (buf[len + 5] << 8) | buf[len + 6];
                    int checkRes = checkData(&buf[5], len);
                    if (checkRcv != checkRes)
                    {
                        log_info("Rcv an invalid packet\n");
                    }
                    else
                    {
                        uint16_t cmd = (buf[1] << 8) | buf[2];
                        list<ProtocolObj *>::iterator it = _allProtocolObjs.begin();
                        for (; it != _allProtocolObjs.end(); it++)
                        {
                            ProtocolObj *obj = *it;
                            if (obj)
                            {
                                if (obj->getId() == cmd)
                                {
                                    obj->handleRcvData(&buf[5], len);
                                }
                            }
                        }
                    }
                    delete [] buf;
                }
            }
            else
            {
                //无效的头部信息
                _circleBuf.popData();
            }
        }
    }
    
    #endif
    
    
    void ProtocolManager::handleProtocol()
    {
        int loopCnt = 0;
        while (_circleBuf.length() >= 7)
        {
            loopCnt++;
            if (loopCnt >= 50)
            {
                break;//防止出现死循环
            }
            uint8_t startCh1 = _circleBuf[0];
            uint8_t startCh2 = _circleBuf[1];
            if (startCh1 == 0xAA && startCh2 == 0xFF)
            {
                uint8_t h3 = _circleBuf[3];
                uint8_t h4 = _circleBuf[4];
                uint16_t len = (h3 << 8) | h4;
                //这里存在一个风险,如果数据长度即 _circleBuf[3]_circleBuf[4]数据错位那么就会导致我们buff存满永远处理不了数据
                //例如我们循环队列长度是2048 但是len解析出来是4096 那么就会出现错误
                //所以我们这里给buffer一个阀值
                //如果这包协议中len的长度超出我们的这个值就将数据推出buffer
                if (len > 500)
                {
                    _circleBuf.popData();
                    continue;
                }
                if (_circleBuf.length() < len + 7)
                {
                    break;
                }
                else
                {
                    uint8_t *buf = new uint8_t[len + 7];
                    for (int i = 0; i < len + 7; i++)
                    {
                        //buf[i] = _circleBuf.popData();
                        buf[i] = _circleBuf[i];
                    }
                    int checkRcv = (buf[len + 5] << 8) | buf[len + 6];
                    int checkRes = checkData(&buf[5], len);
                    if (checkRcv != checkRes)
                    {
                        _circleBuf.popData();
                        log_info("Rcv an invalid packet\n");
                    }
                    else
                    {
                        for (int i = 0; i < len + 7; i++)
                        {
                            buf[i] = _circleBuf.popData();
                        }
                        uint8_t cmd = buf[2];
                        list<ProtocolObj *>::iterator it = _allProtocolObjs.begin();
                        for (; it != _allProtocolObjs.end(); it++)
                        {
                            ProtocolObj *obj = *it;
                            if (obj)
                            {
                                if (obj->getId() == cmd)
                                {
                                    obj->handleRcvData(&buf[5], len);
                                }
                            }
                        }
                    }
                    delete [] buf;
                }
            }
            else
            {
                //无效的头部信息
                _circleBuf.popData();
            }
        }
    }
    
    int ProtocolManager::sendData(uint8_t *buf, uint16_t len, uint8_t cmd)
    {
        if (buf)
        {
            uint16_t sum = 0;
            uint16_t sendLen = 1 + 2 + 2 + len + 2; //查看协议文档
            uint8_t *sendBuf = new uint8_t[sendLen];
            sendBuf[0] = 0xAA;
            sendBuf[1] = 0xFF;
            sendBuf[2] = cmd;
    
            sendBuf[3] = (len & 0xFF00)>>8;
            sendBuf[4] = (len & 0xFF);
    
            for (int i = 0; i < len; i++)
            {
                sendBuf[5+ i] = buf[i];
                sum += buf[i];
            }
    
            sendBuf[5 + len] = (sum & 0xFF00)>>8;
            sendBuf[5 + len + 1] = (sum & 0xFF);
    #if 0
            if (_proDrv && _proDrv->isReady())
                _proDrv->sendData(sendBuf, sendLen);
    
            delete [] sendBuf;
    #endif
    
    #if USING_CMD_LIST
            SendCmd *cmd = new SendCmd;
            cmd->cmd = sendBuf;
            cmd->len = sendLen;
            _cmds.push_back(cmd);
    #else
            if (_proDrv && _proDrv->isReady())
                _proDrv->sendData(sendBuf, sendLen);
    
            delete [] sendBuf;
    #endif
            return len;
        }
        return 0;
    }
    
    #if USING_CMD_LIST
    void ProtocolManager::handleSendCmds()
    {
        while (!_cmds.empty())
        {
            list<SendCmd *>::iterator it = _cmds.begin();
            _cmds.erase(it);
            SendCmd *cmd = *it;
            if (_proDrv)
                _proDrv->sendData(cmd->cmd, cmd->len);
            delete cmd->cmd;
            delete cmd;
        }
    }
    #endif
    
    int ProtocolManager::rcvData(uint8_t *buf, uint16_t len)
    {
        int ret = 0;
        if (buf)
        {
            for (int i = 0; i < len; i++)
            {
                _circleBuf.pushData(buf[i]);
            }
            ret = len;
        }
        return ret;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353

    协议接收数据

    void ProtocolManager::handleProtocol()
    {
        int loopCnt = 0;
        while (_circleBuf.length() >= 7)
        {
            loopCnt++;
            if (loopCnt >= 50)
            {
                break;//防止出现死循环
            }
            uint8_t startCh1 = _circleBuf[0];
            uint8_t startCh2 = _circleBuf[1];
            if (startCh1 == 0xAA && startCh2 == 0xFF)
            {
                uint8_t h3 = _circleBuf[3];
                uint8_t h4 = _circleBuf[4];
                uint16_t len = (h3 << 8) | h4;
                //这里存在一个风险,如果数据长度即 _circleBuf[3]_circleBuf[4]数据错位那么就会导致我们buff存满永远处理不了数据
                //例如我们循环队列长度是2048 但是len解析出来是4096 那么就会出现错误
                //所以我们这里给buffer一个阀值
                //如果这包协议中len的长度超出我们的这个值就将数据推出buffer
                if (len > 500)
                {
                    _circleBuf.popData();
                    continue;
                }
                if (_circleBuf.length() < len + 7)
                {
                    break;
                }
                else
                {
                    uint8_t *buf = new uint8_t[len + 7];
                    for (int i = 0; i < len + 7; i++)
                    {
                        //buf[i] = _circleBuf.popData();
                        buf[i] = _circleBuf[i];
                    }
                    int checkRcv = (buf[len + 5] << 8) | buf[len + 6];
                    int checkRes = checkData(&buf[5], len);
                    if (checkRcv != checkRes)
                    {
                        _circleBuf.popData();
                        log_info("Rcv an invalid packet\n");
                    }
                    else
                    {
                        for (int i = 0; i < len + 7; i++)
                        {
                            buf[i] = _circleBuf.popData();
                        }
                        uint8_t cmd = buf[2];
                        list<ProtocolObj *>::iterator it = _allProtocolObjs.begin();
                        for (; it != _allProtocolObjs.end(); it++)
                        {
                            ProtocolObj *obj = *it;
                            if (obj)
                            {
                                if (obj->getId() == cmd)
                                {
                                    obj->handleRcvData(&buf[5], len);
                                }
                            }
                        }
                    }
                    delete [] buf;
                }
            }
            else
            {
                //无效的头部信息
                _circleBuf.popData();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    协议发送数据

    int ProtocolManager::sendData(uint8_t *buf, uint16_t len, uint8_t cmd)
    {
        if (buf)
        {
            uint16_t sum = 0;
            uint16_t sendLen = 1 + 2 + 2 + len + 2; //查看协议文档
            uint8_t *sendBuf = new uint8_t[sendLen];
            sendBuf[0] = 0xAA;
            sendBuf[1] = 0xFF;
            sendBuf[2] = cmd;
    
            sendBuf[3] = (len & 0xFF00)>>8;
            sendBuf[4] = (len & 0xFF);
    
            for (int i = 0; i < len; i++)
            {
                sendBuf[5+ i] = buf[i];
                sum += buf[i];
            }
    
            sendBuf[5 + len] = (sum & 0xFF00)>>8;
            sendBuf[5 + len + 1] = (sum & 0xFF);
    #if 0
            if (_proDrv && _proDrv->isReady())
                _proDrv->sendData(sendBuf, sendLen);
    
            delete [] sendBuf;
    #endif
    
    #if USING_CMD_LIST
            SendCmd *cmd = new SendCmd;
            cmd->cmd = sendBuf;
            cmd->len = sendLen;
            _cmds.push_back(cmd);
    #else
            if (_proDrv && _proDrv->isReady())
                _proDrv->sendData(sendBuf, sendLen);
    
            delete [] sendBuf;
    #endif
            return len;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    应用层的串口发送接口:

    int ProtocolCommunicate::init()
    {
        MX_USART1_UART_Init();
        ready();
        ProtocolCommunicate::_instance = this;
        HAL_UART_Receive_IT(&huart1, _comRcvBuf, 1);
        return 0;
    }
    
    /*************************************************************
      *Brief:发送数据,中断中不能调用此函数,否则发送数据会错乱
      *Arguments:
      *Returns:
      *Author:xiaoyetongxue
      *Date:2021
    ****************************************************************/
    int ProtocolCommunicate::sendData(uint8_t *buf, uint16_t len)
    {
        if (buf)
        {
            HAL_UART_Transmit(&huart1, buf, len, 100);
            //for (int i = 0; i < len; i++)
            //{
               //HAL_UART_Transmit(&huart1, &buf[i], 1, 100);
               //HAL_Delay(1);
               //波特率是115200 每次发送一个字节需要时间是69us
               //cpu频率是24M每条指令大概是4ns 69*1000/4 = 170000
               //这里等待每个字节发送完毕,延迟大概80us,如果使用HAL_Delay(1);
               //1ms的时间对于系统来说太长了
    //           for (int j =0 ;j < 20000; j++)
    //           {
    
    //           }
            //}
            return len;
        }
        return -1;
    }
    
    ***心电发送到主控芯片***
    int ProtocolHeartBeat::deinit()
    {
        return 0;
    }
    
    int ProtocolHeartBeat::loopRun()
    {
        sendHeartBeat();
        if (_timeOut)
        {
            //副cpu已经死机了要开始重启
        }
        return 0;
    }
    
    #if 1
    void ProtocolHeartBeat::sendHeartBeat()
    {
        static int cnt = 0;
        int tick = HAL_GetTick();
        if (tick - cnt > 1000)
        {
            cnt = tick;
            uint8_t buf[1] = { 0x00 };
            sendData(buf, 0, PROTOCOL_ID_HEART_BEAT);
        }
    }
    #endif
    
    
    ***血氧应用***
    int SpO2::rcvData(uint8_t *buf, int len)
    {
        return SysProtocol->sendData(buf, len, PROTOCOL_ID_SPO2_RAW_DATA);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
  • 相关阅读:
    Java实现模拟键盘和鼠标操作
    Developer Machines FireSolution调色板实现
    Docker 进阶指南(下)- 使用Docker Compose编排多个容器
    SSM+图书馆电子文件资源管理 毕业设计-附源码191614
    exit(0),exit(1),exit(EXIT_SUCCESS),exit(EXIT_FAILURE)
    记一次:jenkins自动化部署服务
    【python数据建模】Pandas库
    (已知中后序、先中序遍历)恢复二叉树
    zookeeper概述
    程序里对象很深很大,可以用这个设计模式缓解一下
  • 原文地址:https://blog.csdn.net/AN15778423590/article/details/125630036