CMD Send Data:刷写数据传输实现

需积分: 0 1 下载量 95 浏览量 更新于2024-08-03 收藏 1KB TXT 举报
"该代码段展示了C语言中一个用于刷写数据的函数cmdSendData,该函数接收一个字符指针和一个字节计数作为参数,处理数据传输并检查输入的有效性。" 在这个代码示例中,`cmdSendData`函数负责发送数据,并通过返回值报告操作是否成功。首先,它导入了`<stdbool.h>`头文件,这个头文件提供了布尔类型的定义,即`bool`,这对于在C语言中表示逻辑状态非常有用。 函数接受两个参数:`const char* pcData`指向要发送的数据,以及`uint32_t ui32ByteCount`表示数据的字节数。函数首先检查`ui32ByteCount`是否超过了最大允许的单次传输字节数`SBL_CC2650_MAX_BYTES_PER_TRANSFER`。如果超过,函数会设置错误状态并返回`SBL_ERROR`,这是一个表示错误的常量。 接着,函数调用`sendCmd`发送CMD_SEND_DATA命令,将数据和字节数传入。如果`sendCmd`执行失败,`cmdSendData`也会返回相应的错误代码。 在发送命令后,函数调用`getCmdResponse`来接收命令响应。这里,`getCmdResponse`函数预期接收一个布尔指针和一个整数。布尔指针`bSuccess`用于接收命令响应的结果,整数可能是超时限制或其他相关参数。如果`getCmdResponse`返回错误,`cmdSendData`同样返回错误。最后,如果`bSuccess`值为`false`,表示命令未成功,函数返回`SBL_ERROR`;否则,函数返回`SBL_SUCCESS`表示成功。 值得注意的是,代码中的一些常量(如`SBL_SUCCESS`, `SBL_ERROR`, `SBL_CC2650_MAX_BYTES_PER_TRANSFER`, `CMD_SEND_DATA`)并未在此定义,它们可能是在其他地方全局定义的,这需要在实际使用时确保这些常量已正确定义。同时,`sendCmd`和`getCmdResponse`函数的实现也不在当前代码片段中,它们应该是项目中的其他部分或库函数。 这样的设计允许`cmdSendData`函数独立于特定的设备或通信协议,使得代码更具可复用性和可扩展性。在实际应用中,根据具体的硬件平台和通信协议,可能需要对`sendCmd`和`getCmdResponse`函数进行定制。

unsigned char crcMediumCheck16 (unsigned char byte1, unsigned char byte2, unsigned char byte3) { unsigned char synd; synd = (byte1 ^ 0xEC); if (synd & 0x80) synd ^= 0xB7; synd = propagate7[synd] ^ byte2; if (synd & 0x80) synd ^= 0xB7; synd = propagate7[synd] ^ byte3; if (synd & 0x80) synd ^= 0xB7; return synd == 0; } uint16_t max14912_readback; /* cmd2 + data2 + crc2 + cmd1 + data1 + crc1 */ uint16_t Maxim14912_Data_Write(uint16_t data, uint16_t *pfault_data) { uint8_t dat1, dat2; //dat1 is first MAX14912(bit8-15),dat2 is second MAX14912(bit0-7) uint8_t CMD_Data[6]={0x80,0,0,0x80,0,0}; uint8_t data_rx[6]; uint8_t crc_check1, crc_check2; uint8_t ret = 0; uint16_t fault_data; dat1 = (uint8_t)((data >> 8) & 0xff); dat2 = (uint8_t)(data & 0xff); /* data build */ CMD_Data[4] = dat1; CMD_Data[1] = dat2; /* crc build */ CMD_Data[2] = crcMediumEncode16(CMD_Data[0], CMD_Data[1]); CMD_Data[5] = crcMediumEncode16(CMD_Data[3], CMD_Data[4]); /* spi send&recev */ Dio_Spi_Cs_Enable(DO_CS_SELECT); Dio_Spi_Transfer_Data(CMD_Data, data_rx, 6); Dio_Spi_Cs_Release(); /* crc check */ crc_check1 = crcMediumCheck16(data_rx[0], data_rx[1], data_rx[2]); crc_check2 = crcMediumCheck16(data_rx[3], data_rx[4], data_rx[5]); if((crc_check1 == 0) && (crc_check2 == 0)) { fault_data = ((uint16_t)data_rx[0]) | ((uint16_t)data_rx[3] << 8); *pfault_data = fault_data; max14912_readback = ((uint16_t)data_rx[1]) | ((uint16_t)data_rx[4] << 8); /* 2021.09.10 */ } else //crc错误 { max14912_readback = 0; /* 2021.09.10 */ ret = 1; } return ret; }

2023-06-06 上传

void SerialApp_ProcessMSGCmd( afIncomingMSGPacket_t *pkt ){ uint8 stat; uint8 seqnb; uint8 delay; switch ( pkt->clusterId ) { // A message with a serial data block to be transmitted on the serial port. case SERIALAPP_CLUSTERID1: // Store the address for sending and retrying. osal_memcpy(&SerialApp_RxAddr, &(pkt->srcAddr), sizeof( afAddrType_t )); seqnb = pkt->cmd.Data[0]; // Keep message if not a repeat packet if ( (seqnb > SerialApp_RxSeq) || // Normal ((seqnb < 0x80 ) && ( SerialApp_RxSeq > 0x80)) ) // Wrap-around { // Transmit the data on the serial port. if ( HalUARTWrite( SERIAL_APP_PORT, pkt->cmd.Data+1, (pkt->cmd.DataLength-1) ) ) { // Save for next incoming message SerialApp_RxSeq = seqnb; stat = OTA_SUCCESS; } else { stat = OTA_SER_BUSY; } } else { stat = OTA_DUP_MSG; } // Select approproiate OTA flow-control delay. delay = (stat == OTA_SER_BUSY) ? SERIALAPP_NAK_DELAY : SERIALAPP_ACK_DELAY; // Build & send OTA response message. SerialApp_RspBuf[0] = stat; SerialApp_RspBuf[1] = seqnb; SerialApp_RspBuf[2] = LO_UINT16( delay ); SerialApp_RspBuf[3] = HI_UINT16( delay ); osal_set_event( SerialApp_TaskID, SERIALAPP_RESP_EVT ); osal_stop_timerEx(SerialApp_TaskID, SERIALAPP_RESP_EVT); break; // A response to a received serial data block. case SERIALAPP_CLUSTERID2: if ((pkt->cmd.Data[1] == SerialApp_TxSeq) && ((pkt->cmd.Data[0] == OTA_SUCCESS) || (pkt->cmd.Data[0] == OTA_DUP_MSG))) { SerialApp_TxLen = 0; osal_stop_timerEx(SerialApp_TaskID, SERIALAPP_SEND_EVT); } else { // Re-start timeout according to delay sent from other device. delay = BUILD_UINT16( pkt->cmd.Data[2], pkt->cmd.Data[3] ); osal_start_timerEx( SerialApp_TaskID, SERIALAPP_SEND_EVT, delay ); } break; case SERIALAPP_CONNECTREQ_CLUSTER: SerialApp_ConnectReqProcess((uint8*)pkt->cmd.Data); case SERIALAPP_CONNECTRSP_CLUSTER: SerialApp_DeviceConnectRsp((uint8*)pkt->cmd.Data); default: break; }}每行代码注释

2023-06-06 上传
2023-05-27 上传

function link_package(cmd, data) { let isCmd = cmd if (cmd == 1) { isCmd = 128 } console.log("isCmd:" + isCmd + " data:" + data) let totalData = []; var length = data.length; var data_len_20 = length / defaultByteLength; data_len_20 = Math.trunc(data_len_20); var data_len_0 = length % defaultByteLength; var i = 0; if (data_len_20 > 0) { for (; i < data_len_20; i++) { let perData = []; let tmpSeq = 0 if (cmd == 0) { tmpSeq = app.getLinkedSeq() } var cmdId = isCmd + tmpSeq % 128; console.log("cmdId1:" + cmdId + " seq1:" + tmpSeq); perData = perData.concat(cmdId) let da = data.slice(defaultByteLength * i, defaultByteLength * (i + 1)); perData = perData.concat(da) totalData = totalData.concat(header) totalData = totalData.concat(mergLinkHeadAndCrc(perData)); } } if (data_len_0 > 0) { var ed = data.slice( defaultByteLength * i, defaultByteLength * i + data_len_0 ); let perData = []; let tmpSeq = 0 if (cmd == 0) { tmpSeq = app.getLinkedSeq() } var cmdId = isCmd + tmpSeq % 128; console.log("cmdId2:" + cmdId + " seq2:" + tmpSeq); perData = perData.concat(cmdId) let emptyData = Array(defaultByteLength - data_len_0).fill(0xFF); perData = perData.concat(ed) perData = perData.concat(emptyData) totalData = totalData.concat(header) totalData = totalData.concat(mergLinkHeadAndCrc(perData)); } return totalData; } //组装最后的报文 function mergLinkHeadAndCrc(sendData) { let data = sendData.concat() let totalData = [] totalData = totalData.concat(data) var hexStrWithBank = utils.byteArrayToHex(totalData); var crcCalcu = parseCrc(hexStrWithBank); var crcByte = utils.HexStrToByte(crcCalcu); console_log.log( "data bank:" + utils.ByteArrayToHexBankStr(totalData) + "--carc:" + utils.byteArrayToHex(crcByte) ); var afterCrcBytes = []; afterCrcBytes = afterCrcBytes.concat(totalData); afterCrcBytes = afterCrcBytes.concat(crcByte); return afterCrcBytes; }请用class封装,并且将详细的代码告诉我

2023-07-09 上传