本文介绍了使用VB连续接收COM端口数据到excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想接收COM端口到Excel工作表的数据。它应该是一个连续运行的过程。我编写了一个代码,但是当它运行时,会创建多个excel工作簿。我想在单个工作簿中接收数据。请帮助纠正代码或共享可以成功完成任务的代码 谢谢, 我的代码是 '我们将在代码中使用的导入系统 进口系统 Imports System.ComponentModel Imports System.Threading Imports System.IO.Ports Imports Excel = Microsoft.Office .Interop.Excel 'frmMain是我们表格的名称.... '这里开始我们的主要表格代码...... .. Public class frmMain Dim myPort As Array 委托Sub SetTextCallback(ByVal [text] As String) '页面加载代码从这里开始.... Private Sub frmMain_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)处理MyBase.Load myPort = IO.Ports.SerialPort.GetPortNames() cmbBaud.Items.Add(9600) cmbBaud.Items.Add(19200) cmbBaud.Items.Add(38400) cmbBaud.Items.Add(57600) cmbBaud。 Items.Add(115200) For i = 0 To UBound(myPort) cmbPort.Items.Add(myPort(i)) 下一页 cmbPort.Text = cmbPort.Items.Item(0) cmbBaud.Text = cmbBaud.Items.Item(0) btnDisconnect .Enabled = False End Sub '页面加载代码在此结束.... '连接按钮代码从这里开始.... Private Sub btnConnect_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnConnect.Click SerialPort1.PortName = cmbPort.Text SerialPort1.BaudRate = cmbBaud.Text SerialPort1.Parity = IO.Ports.Parity.None SerialPort1.StopBits = IO.Ports.StopBits.One SerialPort1.DataBits = 8 SerialPort1.Open() btnConnect.Enabled = False btnDisconnect.Enabled = True 结束Sub '连接按钮代码在此结束.... '断开按钮代码从这里开始。 ... Private Sub btnDisconnect_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnDisconnect.Click SerialPort1.Close() btnConnect.Enabled = True btnDisconnect.Enabled = False End Sub '断开按钮代码在此结束.... '发送按钮代码从这里开始.... Private Sub btnSend_Click(ByVal sender As System .Object,ByVal e As System.EventArgs)处理btnSend.Click SerialPort1.Write(txtTransmit.Text) 结束子 '发送按钮代码在这里结束.... '串行端口接收代码从这里开始.... Private Sub SerialPort1_DataReceived(ByVal sender As Object,ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)处理SerialPort1.DataReceived ReceivedText(SerialPort1.ReadExisting()) End Sub '串口接收代码在此结束.... '串行端口接收代码(调用)从这里开始.... Private Sub ReceivedText(ByVal [text] As String) Dim excelApp As Excel.Application Dim excelWB As Excel.Workbook Dim excelWS As Excel.Worksheet Dim ass作为字符串 excelApp = CreateObject(Excel.Application) excelWB = excelApp.Workbooks.Add excelWS = excelWB.Worksheets(1) excelApp.Visible = True 如果Me.rtbReceived 。 InvokeRequired然后 Dim x As New SetTextCallback(AddressOf ReceivedText) Me.Invoke(x,New Object(){(text)}) Else Me.rtbReceived.Text&= [text] ass = Me.rtbReceived.Text 结束如果 for rowCounter = 1 To 100 excelWS.Cells(rowCounter,1).Value = ass Next rowCounter End Sub '串行端口接收代码(调用)在此结束.... 'Com端口更改警告代码从这里开始.... Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object,ByVal e As System.EventArgs)处理cmbPort.SelectedIndexChanged 如果SerialPort1.IsOpen =错误然后 SerialPort1.PortName = cmbPort.Text 否则 MsgBox(仅在端口关闭时有效,vbCritical) 结束如果 结束次级 'Com端口更改警告代码在此结束.... '波特率更改警告代码从这里开始.... Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object,ByVal e As System.EventArgs)处理cmbBaud .SelectedIndexChanged 如果SerialPort1.IsOpen = False那么 SerialPort1.BaudRate = cmbBaud.Text 否则 MsgBox (仅在端口关闭时有效,vbCritical) 结束如果 结束子 '波特率更改警告代码在此结束.. .. 结束班 '整码在此结束........i want to receive data of COM port to Excel worksheet. it should be a continuous running process. i have written a code but when it runs, creates multiple excel workbooks . i want to receive data in single workbook. please help to rectify the code or share a code that can perform the task successfullyThanks,my code is'Import Systems which we are gonna use in our codeImports SystemImports System.ComponentModelImports System.ThreadingImports System.IO.PortsImports Excel = Microsoft.Office.Interop.Excel'frmMain is the name of our form ....'Here starts our main form code .....Public Class frmMain Dim myPort As Array Delegate Sub SetTextCallback(ByVal [text] As String) 'Page Load Code Starts Here.... Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load myPort = IO.Ports.SerialPort.GetPortNames() cmbBaud.Items.Add(9600) cmbBaud.Items.Add(19200) cmbBaud.Items.Add(38400) cmbBaud.Items.Add(57600) cmbBaud.Items.Add(115200) For i = 0 To UBound(myPort) cmbPort.Items.Add(myPort(i)) Next cmbPort.Text = cmbPort.Items.Item(0) cmbBaud.Text = cmbBaud.Items.Item(0) btnDisconnect.Enabled = False End Sub 'Page Load Code Ends Here .... 'Connect Button Code Starts Here .... Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click SerialPort1.PortName = cmbPort.Text SerialPort1.BaudRate = cmbBaud.Text SerialPort1.Parity = IO.Ports.Parity.None SerialPort1.StopBits = IO.Ports.StopBits.One SerialPort1.DataBits = 8 SerialPort1.Open() btnConnect.Enabled = False btnDisconnect.Enabled = True End Sub 'Connect Button Code Ends Here .... 'Disconnect Button Code Starts Here .... Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click SerialPort1.Close() btnConnect.Enabled = True btnDisconnect.Enabled = False End Sub 'Disconnect Button Code Ends Here .... 'Send Button Code Starts Here .... Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click SerialPort1.Write(txtTransmit.Text) End Sub 'Send Button Code Ends Here .... 'Serial Port Receiving Code Starts Here .... Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived ReceivedText(SerialPort1.ReadExisting()) End Sub 'Serial Port Receiving Code Ends Here .... 'Serial Port Receiving Code(Invoke) Starts Here .... Private Sub ReceivedText(ByVal [text] As String) Dim excelApp As Excel.Application Dim excelWB As Excel.Workbook Dim excelWS As Excel.Worksheet Dim ass As String excelApp = CreateObject("Excel.Application") excelWB = excelApp.Workbooks.Add excelWS = excelWB.Worksheets(1) excelApp.Visible = True If Me.rtbReceived.InvokeRequired Then Dim x As New SetTextCallback(AddressOf ReceivedText) Me.Invoke(x, New Object() {(text)}) Else Me.rtbReceived.Text &= [text] ass = Me.rtbReceived.Text End If For rowCounter = 1 To 100 excelWS.Cells(rowCounter, 1).Value = ass Next rowCounter End Sub 'Serial Port Receiving Code(Invoke) Ends Here .... 'Com Port Change Warning Code Starts Here .... Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged If SerialPort1.IsOpen = False Then SerialPort1.PortName = cmbPort.Text Else MsgBox("Valid only if port is Closed", vbCritical) End If End Sub 'Com Port Change Warning Code Ends Here .... 'Baud Rate Change Warning Code Starts Here .... Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged If SerialPort1.IsOpen = False Then SerialPort1.BaudRate = cmbBaud.Text Else MsgBox("Valid only if port is Closed", vbCritical) End If End Sub 'Baud Rate Change Warning Code Ends Here ....End Class'Whole Code Ends Here ........推荐答案来吧,看看你自己的逻辑。每次调用ReceivedText时你在做什么?你正在创造一个全新的工作簿!这告诉你什么?Come on, look at your own logic. What are you doing every time ReceivedText is called?? YOU'RE CREATING A WHOLE NEW WORKBOOK! What does that tell you?? 这篇关于使用VB连续接收COM端口数据到excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 15:24