如何将串口接收到的数据保存到EXCEL文档。
请高手指点,如何将串口接收到的数据保存到EXCEL文档。 搜搜 串口监控器 能自动保存数据 但不是excel格式 用VB DDE通讯! 1.启动excel从:工具-->宏-->Visual Basic 编辑器,打开excel VBA.2.在ThisWorkbook上右单击鼠标选择插入--用户窗体
3.单击一下插入的窗体,单击菜单上的--工具--附加控件--选择Microsoft Communications Control, version 6.0
4.在窗体上添加mscomm1,添加commandButton1
5.单串口机子,短接rs232的2脚和3脚,双串口机子用232线连接两个串口,注意2、3线交叉,我这里以单串口短接举例。
6.复制以下代码到你的窗体里:
VB code
'VBA代码
Private Sub CommandButton1_Click()
MSComm1.Output = "BEG1END"
End Sub
Private Sub MSComm1_OnComm()
Dim t1 As Long, com_String As String
Static i As Integer
t1 = Timer
Select Case MSComm1.CommEvent
Case comEvReceive '收到 RThreshold定义的字符数1字节
MSComm1.RThreshold = 0
Do
DoEvents
Loop While Timer - t1 < 0.1 '延时时间自己调整
com_String = MSComm1.Input
MSComm1.RThreshold = 1
i = i + 1: If i > 255 Then i = 1
Application.Cells(3, i).Value = com_String
End Select
'ActiveWorkbook.SaveAs Filename:="C:\d1.xls"
End Sub
Private Sub iniMscomm()
'On Error Resume Next
'=====-----初始化通信串口-----=====
MSComm1.CommPort = 1 '使用 COM1
MSComm1.Settings = "9600,N,8,1" '9600 波特,无奇偶校验,8 位数据,一个停止位
MSComm1.PortOpen = True '打开端口
MSComm1.RThreshold = 1 '缓冲区有1个字节就产生OnComm事件
MSComm1.InputLen = 0 '为 0 时,使用 Input 将使 MSComm 控件读取接收缓冲区中全部的内容。
MSComm1.InputMode = comInputModeText'Input以二进制形式取回用comInputModeBinary,以文本形式取回是(缺省项)
MSComm1.RTSEnable = True
MSComm1.InBufferCount = 0 '清空缓冲区
End Sub
Private Sub UserForm_Initialize()
iniMscomm
End Sub
7.双击thisWorkBook,复制以下代码到窗体里:
VB code
'VBA代码
Private Sub Workbook_Open()
UserForm1.Show 0
'UserForm1.Hide
End Sub
8.保存文件并关闭excel,然后再打开你的文件,然后单击你窗体上的按钮看看。
9.这个程序是我为你特意做的,已经做过测试,sys2003 office2003。
10.如果excel中有数据输入,再去测试你的设置,注意com口号与波特率的设置。 楼上正解,用VBA MARK mark 【3楼】 真是热心人! 好资料。。。 mark mark excel 串口 因有【3楼】 ggg12345这位先生贡献的文章,建议给条裤子了 还有一个办法就是用任何编程,只需将收到的数据用csv文件的格式,生成一个文本文件,然后就可以用excel打开了。
这个方法适合于任何语言,只要能生成文本文件就可以了。
希望对楼主有帮助。 大神 mark了 3楼正解 3楼说说怎么解决KB的那个补丁问题,很多人是没法用串口的那个控件的 mark mark 3楼大侠太好了 改天我试试怎么用 12楼的最简单 mark excel 串口 MARK mark mark mark 不错 上位机处理的 这个得mark一下 谢谢楼主的提问,谢谢3楼大侠的解答! good,牛人 mark 如果存成txt再打包可以的话, Perl简单些,不熟悉的人3天也能做出来.
Spreadsheet::WriteExcel 和 Spreadsheet::ParseExcel mark 串口 VBA 我很需要这个例子
谢谢
学习了 ggg12345 发表于 2011-5-15 03:16 static/image/common/back.gif
1.启动excel从:工具-->宏-->Visual Basic 编辑器,打开excel VBA.
2.在ThisWorkbook上右单击鼠标选择插入- ...
好人啊~~~正需要,谢谢了~~~{:victory:} {:handshake:} {:lol:} 果断收藏啊
好东西 mark
页:
[1]