iT邦幫忙

0

VB串接程式轉成QT(TAIE)

qt vb
  • 分享至 

  • xImage

大大好,最近因為要整合實驗數據所以被邀求把過去的程式更新成QT但沒怎麼碰過C++ 請問要怎麼著手比較好,程式碼大概如下

1.連線溫度機器

Private Function Open_Temperature()
        
    Dim ValueStr As String
    
    'Connect Port ID
    If MSComm.PortOpen = False Then

        MSComm.CommPort = Port_Id2
        
        MSComm.Settings = "9600,o,8,1"

        '    告訴控制項當使用 Input 時,讀取整個暫存區
        MSComm.InputMode = comInputModeBinary

        MSComm.InputLen = 8
        ' 開啟序列連接埠

        MSComm.PortOpen = True
        DoEvents

    End If

    Sleep (100)
    DoEvents

下指令讀溫度

Private Function Read_Temperature(Read_I As Integer)
        
    Dim ValueStr As String

    If Read_I = 1 Then
        ValueStr = "52 01 00 8A 00 01 DE"
    Else
        ValueStr = "52 02 00 8A 00 01 DF"
    End If
    
    CreatDataFrame (ValueStr) 'Initial PID Head Information
    
    If MSComm.PortOpen = True Then
    MSComm.Output = bytBinary
    DoEvents
    End If
    
    Sleep (100)
    DoEvents
    
    Read_Temperature = Val(GetBack_T) / 10
    DoEvents
  
End Function

回傳溫度

Public Function GetBack_T()

    Dim bytTemp() As Byte
    Dim X As Variant
    Dim sShow As String
    Dim I As Integer
    Dim TValue_1 As String
    Dim TValue_2 As String
    Dim msString As String
    Dim mlReceiveByte As Long, mlReceivePacket As Long, miReceivePacketByte As Integer
Dim mlSendByte As Long, mlSendPacket As Long, miSendPacketByte As Integer

    Dim mbPolling As Boolean

    Dim bytBinary() As Byte
    Dim iCount As Integer, I As Integer

    Dim lPollingTimeout As Long
    Dim Data_Pool() As String
    
    
    Sleep (100)
    DoEvents
    
    While MSComm.PortOpen
        
        If MSComm.InBufferCount Then
            MSComm.InputLen = MSComm.InBufferCount
            mlReceiveByte = mlReceiveByte + MSComm.InputLen
            mlReceiveByte = MSComm.InputLen
            Erase bytTemp
            DoEvents

            bytTemp = MSComm.Input
            
            
            If mbPolling Then
                If (mlReceiveByte Mod 8) = 0 Then
                    If (lTime_2 - lTime_1) > lTime_Max Then
                        lTime_Max = lTime_2 - lTime_1
                        txtTimeMax.Text = lTime_Max
                    End If
                    If (lTime_2 - lTime_1) < lTime_Min Then
                        lTime_Min = lTime_2 - lTime_1
                        txtTimeMin.Text = lTime_Min
                    End If
                    lTime_1 = lTime_2
                    mlSendByte = mlSendByte + iCount + 2 '+2 ==> CRC
    
                    mlSendPacket = mlSendPacket + 1

                    MSComm.Output = bytBinary
                End If
            Else
                sShow = ""
                For I = 0 To UBound(bytTemp)
                    sShow = sShow + Right("0" + Hex(Int(Str(bytTemp(I)))), 2) + " "
                Next I
                
                    Print #1, sShow
                Close #1
                    
                If Mid(sShow, 1, 5) = "07 4D" And Len(sShow) = 24 Then
                
                    TValue_1 = Val(Mid(sShow, 16, 2))
                    
                    TValue_2 = Mid(sShow, 19, 2)
                    
                    If Asc(Mid(TValue_2, 1, 1)) > 64 Then
                        TValue_1 = Val(TValue_1) * 256 + (Val(Asc(Mid(TValue_2, 1, 1))) - 55) * 16
                    Else
                        TValue_1 = Val(TValue_1) * 256 + (Val(Asc(Mid(TValue_2, 1, 1))) - 48) * 16
                    End If
                    
                    
                    If Asc(Mid(TValue_2, 2, 1)) > 64 Then
                        TValue_1 = Val(TValue_1) + (Val(Asc(Mid(TValue_2, 2, 1))) - 55)
                    Else
                        TValue_1 = Val(TValue_1) + (Val(Asc(Mid(TValue_2, 2, 1))) - 48)
                    End If
                
                    GetBack_T = TValue_1
                    
                    DoEvents
                
                Else
                        
                    GetBack_T = "NA"
                
                End If
                
               
                Exit Function
                
            End If
        Else
            Exit Function
        End If
        DoEvents
        
    Wend
    
    Exit Function
  
End Function
看更多先前的討論...收起先前的討論...
froce iT邦大師 1 級 ‧ 2020-02-05 19:20:08 檢舉
QT應該沒有VB的binding,C#好像還有(mono)。

建議可以改用python寫(pyside或是pyQT,你這種程式應該還需要pyserial)
bennnn iT邦新手 5 級 ‧ 2020-02-05 19:46:55 檢舉
之前我有更新到C#過,因為差異性不大只要更改一些寫法就可行

因為新的實驗資料都會從QT出來所以才被要求要改成QT,如果用pyQT會不會很難和其他C++的QT程式相通?
froce iT邦大師 1 級 ‧ 2020-02-06 11:42:39 檢舉
> 因為新的實驗資料都會從QT出來所以才被要求要改成QT

QT只是UI框架。
新的實驗資料在UI下應該還是有可以在console吐資料的功能,你去找那個去做界接。
loukei iT邦新手 5 級 ‧ 2020-02-12 17:39:49 檢舉
如果只是資料處理而不涉及UI邏輯的部份,你想辦法轉成C++的API即可。

QT還是有對C++的一些特性作改進的,比如signal-slot與RTTI機制,物件所有權的管理等等,這些等到你寫UI才會用到。

沒學過VB所以沒辦法幫忙。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答