2012年4月13日 星期五

RS-232 ﹣用Excel API 做串列介面數據傳輸及接收 (五)

RS-232 ﹣用Excel API 做串列介面數據傳輸及接收 ()

利用 Excel API 做串列介面 (Serial Poort – COM1) 數據傳輸及接收,數據流從一個 RS232(串列)埠直接到 Excel 電子表格,使用 KERNEL32 庫,而不是 MSCOMM.OCX。並使用 Writefile Readfile 來達到傳輸及接收數據,配置串口 DCB (Device Control Block) 結構定義了串口通信設備的控制設置。

 
Excel VBA 編程:
  • 檔案→開新檔案,打開新檔案 (或按 Ctrl+N)
  • 工具→巨集→Visual Basic編輯器,進入 VBA 編輯器 (或按 Alt+F11)
  • 選擇功能表命令 插入→點擊模組 (Insert Module)
  • 編寫VBA程式。
  • 執行VBA模組 (或按 F5)
  • 檔案→關閉並回到 Microsoft Excel (或按 Alt+Q)
  • 儲存檔案。

RS232 Send Data
RS232 Received Data
鍵入文字 "This is test com1" 然後 Send
Received 文字 "This is test com1"

測試成功! 

相關網頁:
◎ RS-232 ﹣串列數據通訊的介面標準 (一)
◎ RS-232 ﹣超級終端機串列介面測試 (二)
◎ RS-232 ﹣超級終端機串列介面 Loopback 測試 (三)
◎ RS-232 ﹣用 Excel 做串列介面數據傳輸及接收 (四)
◎ RS-232 ﹣用 Excel API做串列介面數據傳輸及接收 (五)

相關網頁:
 DIY - 電子:EIA RS-232 串行通信協議標準 (九十二) :
DIY - 電子:MAX232 串行通信協議標準 IC (九十三) :
 DIY - 電子:C18 PIC18F4550 USART 函數庫程式 (九十四) :
DIY - 電子:PIC18F4550 USART 電路功能介紹 (九十五) :
 DIY - 電子:PIC18F4550 USART 自動波特率電路介紹 (九十六) :
DIY - 電子:PIC18F4550 USART 異步模式 (九十七) :
◆ DIY - 電子:PIC18F4550 + MAX232 USART 電路 (九十八) :
◆ DIY - 電子:PIC18F4550 + MAX232 USART 電路程式 (九十九) :
DIY - 電子:PIC18F4550 測試程式 Hyper Terminal (一百) :

相關網頁:
◎ DIY - 智能小車:TL-WR703N 無線路由器與 16F877A 控制版串口連接電路 (二十)
◎ DIY - 智能小車:ser2net 詳細介紹 (二十一)
◎ DIY - 智能小車:智能小車 ser2net 設定 (二十二)
◎ DIY - 智能小車:16F877A 控制版串口連接程式 (二十三)
◎ DIY - 智能小車:TL-WR703N 路由器和 RS232 串行板組裝 (二十九)
◎ DIY - 智能小車:PIC 16F877A 處理器板 RS232 接收程式碼 (三十三)

相關網頁:
◎ ESCORT 3146A 5 1/2位數字雙顯示桌上型電錶數據傳輸
◎ Hameg HM507 CombiScope (模數組合示波器) – RS232

8 則留言:

  1. 請問有
    Excel API 做串列介面 (Serial Poort – COM1) 數據傳輸及接收
    的程式碼?
    可以傳給我嗎
    謝謝

    回覆刪除
  2. 可以參考 http://dev.emcelettronica.com/serial-port-communication-excel-vba,Test過程式碼是 OK,如有問題可再連絡。

    回覆刪除
  3. 您好! 謝謝您熱心地分享和指點! 我也學習您, 已能如上面方式雙向傳訊息, 但想要用 Excel 2010 的 worksheet能接收每筆放入 cell裡面. 類似 datalog方式, 能否請您再指點我的 VBA bug, 謝謝您!

    Private Sub CommandButton3_Click()

    'Com Port 參數
    Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4
    Dim lngStatus As Long
    Dim strData As String
    'Dim strData

    ' Open COM port
    lngStatus = CommOpen(intPortID, "COM" & CStr(intPortID), _
    "baud=9600 parity=N data=8 stop=1")

    '試算表 參數
    Dim exlApp As Excel.Application
    Dim exlWork As Excel.Workbook
    Dim sheet As Excel.Worksheet
    Set exlApp = New Excel.Application
    exlApp.Visible = False

    'Com Port 轉入 MutilTex參考
    TextBox2.ScrollBars = 1
    TextBox2.AutoSize = False
    TextBox2.WordWrap = True
    TextBox2.MultiLine = True

    '指定讀取 Com1
    intPortID = 1

    'Com1 狀態
    lngStatus = CommRead(intPortID, strData, 10)

    '字串轉入 MutilText
    'TextBox2.Text = strData

    '一次開檔寫入資料
    Set exlWork = exlApp.Workbooks.Open("C:\Users\Joe\Desktop\RS232.xlsm")
    Set sheet = exlWork.Sheets("RS232")
    exlWork.Sheets("RS232").Select
    'sheet.Visible = xlSheetVisible
    'sheet.Activate

    '以下仍在 debug部分
    '測試監控 Com1 Port數據, 寫入 worksheet 的 cell
    For iRow = 1 To 100
    For iCol = 1 To 3
    'Cells(iRow, iCol) = iRow & iCol
    TextBox2.Text = strData
    Cells(iRow, iCol).Text = strData
    'PIC 1632的 32MX795F512L, 每秒鐘送出時間一次
    'Application.Wait (Now + TimeValue("0:00:01"))
    Next
    Next
    '測試 cell能否寫入
    'For i = 1 To 10
    'sheet.Cells(sheet.UsedRange.Rows.Count + 1, 2).Value = i

    '判斷有無資料
    'If lngStatus = Null Then
    'Set sheet = Nothing
    'exlWork.Save
    'exlWork.Close
    'Set exlWork = Nothing
    'exlApp.Quit
    'Set exlApp = Nothing
    'End If

    'Next i
    End Sub

    回覆刪除
  4. 有一年多沒碰 Excel VBA!請問上面程式的問題在那裡?看上面程式是按鍵然後去讀取 RS-232 數據放入 Cell 內。如果程式太長,請用 E-mail 郵箱 bugworkshop@gmail.com。

    回覆刪除
  5. 請問一下,上列網址已經失效,能不能請版主再分享一次程式,謝謝。

    回覆刪除
  6. When I use an RS232C device connected to my computer via the Keysight VEE program (or via another software), an error appears: "Bus error occurred?”.
    Via IO libraries, an error appears when I try to scan using *IDN? command. The description says: "The device did not respond to an *IDN? query".
    Would you mind telling how can I make a successful connection?. Please, what can I do to resolve this problem?

    Sincerely,

    回覆刪除
  7. Hi Jacasse, Please make sure the computer can receive/send the information from/to Keysight equipment. You can use the Hyper Terminal program to check at first. You can see more detail at https://bugworkshop.blogspot.com/2011/07/diy-picpic18f4550-hyper-terminal.html

    回覆刪除
  8. Thank you very much for responding. Keysight connection expert interface, it's not possible to detect equipment. When I posted this issue in the IN-Labview forum, the administrator confirms that the apparatus is made of china, and it'snot registered to be known. Owing to this fact, there is any possibility to control this apparatus (IVYTEC-LCR-106X) via VB-excel.
    Sincerly,

    回覆刪除