簡體   English   中英

從VBA訪問串行端口的最佳方法是什么?

[英]What is the best way to access a serial port from VBA?

從VBA訪問串行端口的最佳方法是什么?

我需要一些銷售代表能夠從PowerPoint中的操作按鈕通過串行端口發送簡單的字符串。 我通常不使用VBA,尤其是對於這樣的東西。 通常,我會將其轉換為某種應用程序,但實際上我並不認為這個想法很糟糕。 對於他們來說,這將是一個方便的工具,讓他們可以在投影儀上演示該設備,並與其他銷售人員和非技術人員進行交談。 同樣,該銷售人員對VBA或PowerPoint演示文稿進行少量修改也不會有問題,但是在重新編譯.NET應用程序方面不會做得很好。

我知道我們可以通過從演示文稿運行的批處理文件來做到這一點,但這並不能令我非常高興。 我認為我們可能可以訪問一個COM對象並從那里運行,但是我也不是完全了解VBA中使用的最新和最出色的庫,並且對如何輕松打開它有一個簡短的入門知識也很不錯,發送並關閉連接。

由於這將需要在多人的計算機上運行,​​因此,如果可以輕松地將其傳輸到其他計算機,那就太好了。 我應該可以說它必須在Office 2007和Windows XP上運行。 與其他任何東西的兼容性都是不錯的選擇。

我應該如何處理呢? 有什么好的提示或技巧嗎? 圖書館的建議?

Win32 API將串行端口作為文件處理。 您可以通過在VBA中調用這些API函數來直接訪問串行端口。 我必須為舊的.NET應用程序執行此操作,但VBA沒什么不同。

多年來,我一直在參考這個參考,而不是在本網站上將其哈希化。 如何在VBA中執行串行端口通信

Sub Stinky()
Dim COM_Byte As Byte
Dim Received_Lines As Long
Dim Input_Buffer As String
Dim Output_Buffer As String
Dim Chars2Send As Long
Dim CharsRemaining As Long
Dim lfsr As Long
    Open "COM7:9600,N,8,1" For Random As #1 Len = 1
    Input_Buffer = ""
    CharsRemaining = 0
    Do
    Get #1, , COM_Byte
    If COM_Byte Then
        If COM_Byte = 13 Then           ' look for CR line termination
            Debug.Print Input_Buffer, Now   ' print it
            Input_Buffer = ""               ' and clear input buffer
        '   generate some output (9 characters)
            lfsr = &H3FFFFFFF - 2 ^ (Received_Lines And 15)
            Output_Buffer = "?@@@@@@@@"
            Chars2Send = 9
            CharsRemaining = 9
            For j = 0 To 2
                Mid(Output_Buffer, 2 + j, 1) = Chr(Asc(Mid(Output_Buffer, 2 + j, 1)) + (31 And Int(lfsr / 32 ^ (2 - j))))
            Next j
            Debug.Print Output_Buffer
        '   show what I generated
            Received_Lines = Received_Lines + 1 ' keep track of received line count
        Else
            Input_Buffer = Input_Buffer & Chr(COM_Byte) ' assemble output buffer
        '   process any characters to send
            If CharsRemaining Then
                CharsRemaining = CharsRemaining - 1
                COM_Byte = Asc(Mid(Output_Buffer, Chars2Send - CharsRemaining, 1))
                Put #1, , COM_Byte
            End If
        End If
    End If
    DoEvents
    Loop
    Close
End Sub

這對我有用。 我不確定OPEN是否真的設置了波特率,因為我第一次使用TeraTerm。 我的COM端口是與BASYS3原型套件的USB連接。 它以9600噴出字符,記錄了以CR結尾的36個字符。 我可以隨機發送9個字符的命令。 在上面的代碼中,每當收到新行時,我都會生成這些命令字符串。 我選擇發送哪個字符的方法有些笨拙:也許更好的方法是擁有一個字符指針和多個字符,並且當這些字符相等時將它們都設置為零。

這是VBA代碼的簡要模塊,可以在PC串行端口上發送和接收消息。 這不是很優雅,但是很簡單,並且可以在現代版本的Excel和Windows上運行。

您可以獨自擴展功能,以及存儲或解析消息。 這僅顯示了處理串行端口的低級內容。

前5行聲明了毫秒級的“睡眠”庫函數(基於Excel版本)。

SerialPort()子例程概述了以下步驟:打開端口,傳輸一些數據,接收一些數據,再次嘗試接收一些數據(以表明它確實不會違反“文件結尾”錯誤)並關閉港口。


#If VBA7 Then ' Excel 2010 or later
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal Milliseconds As LongPtr)
#Else ' Excel 2007 or earlier
    Public Declare Sub Sleep Lib "kernel32" (ByVal Milliseconds As Long)
#End If

Public Sub SerialPort()
    ' open a COM port, transmit a message, gather results, close the port.

    ' open the COM port as file #1
    Debug.Print "Open COM port 4"
    Open "COM4:115200,N,8,1" For Binary Access Read Write As #1

    transmit$ = Chr(2) + "Hello, World." + Chr(13)
    receiveDummy$ = "~~~"

    ' transmit a message
    Put #1, , transmit$
    Debug.Print "Message sent."

    ' wait a bit for a response
    Sleep 100

    ' check for received message
    Debug.Print "Look for incoming message."
    On Error Resume Next
    Do While True
        receive$ = receiveDummy$  'dummy value
        Input #1, receive$
        If receive$ = receiveDummy$ Then Exit Do  'the string didn't change, so move on
        Debug.Print receive$
    Loop
    On Error GoTo 0

    ' do it again to show that the empty input queue doesn't stop the flow
    Debug.Print "Look again for incoming message (should not stop on error)."
    On Error Resume Next
    Do While True
        receive$ = receiveDummy$  'dummy value
        Input #1, receive$
        If receive$ = receiveDummy$ Then Exit Do  'the string didn't change, so move on
        Debug.Print receive$
    Loop
    On Error GoTo 0

    ' close the serial port
    Debug.Print "Close COM port."
    Close #1

    Debug.Print "Done."
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM