![]() |
發(fā)布時間: 2019-1-10 15:28
正文摘要:客戶提出要實現(xiàn)天平測量結(jié)果自動采集到電腦的電子表格中,去市場上了解了一下,發(fā)現(xiàn)帶有串口的天平是有,但是配套的軟件安裝過于專業(yè),對于技術(shù)小白的客戶而言太過復(fù)雜,于是嘗試自己做一個天平數(shù)據(jù)采集器. ... |
謝謝樓主分享![]() |
[代碼段1:串口中斷函數(shù)] // UNUSED(huart); //收到先累加 Uart1ReciveBuffer[Uart1ReciveBufferIndex] = Uart1IntBuffer[0]; Uart1ReciveBufferIndex++; //判斷收到的內(nèi)容中是否有回車換行 char Cmd[2] = { "\r\n" }; if (strstr((char *)Uart1ReciveBuffer, Cmd)) { //判斷第13位是回車,第14位是換行 if(Uart1ReciveBuffer[12] == 0x0d || Uart1ReciveBuffer[13] == 0x0a) { //判斷格式是否非法 if(Uart1ReciveBuffer[0] != 0x2d) { int _j = 2; for (int _i=0; _i < 8; _i++) { if (Uart1ReciveBuffer[_i + 1] != 0x20) { //1-9 if (Uart1ReciveBuffer[_i + 1] >= 0x31 && Uart1ReciveBuffer[_i + 1] <= 0x39) { HidKeyboardSend[_j] = Uart1ReciveBuffer[_i + 1] - 19; } //0 if (Uart1ReciveBuffer[_i + 1] == 0x30) { HidKeyboardSend[_j] = 0x27; } //. if (Uart1ReciveBuffer[_i + 1] == 0x2e) { HidKeyboardSend[_j] = 0x37; } // _j++; } } //收到量具返回后將狀態(tài)設(shè)置成2 CurrentNotify = 2; } else { //清空緩存區(qū) memset(Uart1ReciveBuffer, 0, sizeof(Uart1ReciveBuffer)); //計數(shù)清零 Uart1ReciveBufferIndex = 0; } } else { //清空緩存區(qū) memset(Uart1ReciveBuffer, 0, sizeof(Uart1ReciveBuffer)); //計數(shù)清零 Uart1ReciveBufferIndex = 0; } [代碼段2:主循環(huán)函數(shù)] if (CurrentNotify == 2) { //打印收到的內(nèi)容 uint8_t HidKeyboardSendTemp[8] = { 0 }; for (int _i = 0; _i < 6; _i++) { //請空HID緩沖區(qū) memset(HidKeyboardSendTemp, 0, sizeof(HidKeyboardSendTemp)); //發(fā)送一個字節(jié) if(HidKeyboardSend[_i + 2] != 0x00) { // HidKeyboardSendTemp[2] = HidKeyboardSend[_i + 2]; // USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t*)HidKeyboardSendTemp, 8); HAL_Delay(10); USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t*)HidKeyboardUp, 8); HAL_Delay(10); } } } if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == GPIO_PIN_RESET) { //發(fā)送控制符 USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t*)HidKeyboardEnter, 8); HAL_Delay(10); USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t*)HidKeyboardUp, 8); HAL_Delay(10); } else { //發(fā)送控制符 USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t*)HidKeyboardTab, 8); HAL_Delay(10); USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t*)HidKeyboardUp, 8); HAL_Delay(10); } // CurrentNotify = 0; |
Powered by 單片機(jī)教程網(wǎng)