標(biāo)題: 用STM32F103做天平數(shù)據(jù)采集器 [打印本頁]

作者: 熊貓仙    時(shí)間: 2019-1-10 15:28
標(biāo)題: 用STM32F103做天平數(shù)據(jù)采集器
    客戶提出要實(shí)現(xiàn)天平測(cè)量結(jié)果自動(dòng)采集到電腦的電子表格中,去市場(chǎng)上了解了一下,發(fā)現(xiàn)帶有串口的天平是有,但是配套的軟件安裝過于專業(yè),對(duì)于技術(shù)小白的客戶而言太過復(fù)雜,于是嘗試自己做一個(gè)天平數(shù)據(jù)采集器.
    采集器的基本設(shè)計(jì)思路是這樣,使用F103這款芯片,利用其USB口模擬成USB鍵盤,實(shí)現(xiàn)在PC機(jī)上的免驅(qū),然后通過其UART串口加MAX323電平轉(zhuǎn)換實(shí)現(xiàn)與天平RS232的通訊,天平則選擇一款可以通過串口指令實(shí)現(xiàn)測(cè)量結(jié)果打印的產(chǎn)品,除此以外,還要加裝按鍵和蜂鳴器,萬事俱備,開工!  










作者: 熊貓仙    時(shí)間: 2019-1-10 15:28
[代碼段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));
                //計(jì)數(shù)清零
                Uart1ReciveBufferIndex = 0;
        }
                       
}
else
{
        //清空緩存區(qū)
        memset(Uart1ReciveBuffer, 0, sizeof(Uart1ReciveBuffer));
        //計(jì)數(shù)清零
        Uart1ReciveBufferIndex = 0;
}
       


[代碼段2:主循環(huán)函數(shù)]
if (CurrentNotify == 2)
{
        //打印收到的內(nèi)容
        uint8_t HidKeyboardSendTemp[8] = { 0 };
        for (int _i = 0; _i < 6; _i++)
        {
                //請(qǐng)空HID緩沖區(qū)
                memset(HidKeyboardSendTemp, 0, sizeof(HidKeyboardSendTemp));
                //發(fā)送一個(gè)字節(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;
作者: lindeijun1    時(shí)間: 2020-3-25 09:29
謝謝樓主分享




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1