大大好,最近因為要整合實驗數據所以被邀求把過去的程式更新成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