博士
最后登录1970-1-1
在线时间 小时
注册时间2014-10-31
|
发表于 2016-3-10 15:03:17
|
显示全部楼层
本帖最后由 11kxmx 于 2016-3-10 15:30 编辑
上位机编程用VC或VB,最基本的是你要先能用程序打开EXCEL表格或ACESS表格,向其写入数据,我用VB弄过
,串口收到一定数量的数据后保存在EXCEL表格"Book3.xls"中,主要代码如下:
Option Explicit
Dim Temp(0) As Byte
Dim ABC As Byte
Dim strBuff As String
Dim xlsApp As Excel.Application
Dim xlsBook As Excel.Workbook
Dim xlsSheet As Excel.Worksheet
Dim row
Dim range
Select Case MSComm1.CommEvent '接收事件发生
Case 2
inbuff = MSComm1.Input '读入到缓冲区
ll = UBound(inbuff)
Label10.Caption = Label10.Caption + UBound(inbuff) + 1
ReDim indata(1 To (ll + 1))
For ii = 0 To UBound(inbuff)
strHex = strHex & Right("0" & Hex(inbuff(ii)), 2) & " " '如果只有一个字符,则前补0, 如F显示0F,最后补空格
'方便显示观察如: 00 0F FE
TextReceive = strHex
Next ii
For ii = 1 To Len(strHex) Step 6
indata((ii + 5) / 6) = Val("&H" & Mid(strHex, ii, 2)) * 4 + Val("&H" & Mid(strHex, ii + 3, 2))
Next ii
n1 = Val(Text1.Text) '电源电压
n2 = Val(Text2.Text) '第一路电压衰减倍数
n3 = Val(Text3.Text) '第二路电压衰减倍数
na = n1 * n2 / 1024
nb = n1 * n3 / 1024
'写入EXCEL表格中
Set xlsApp = New Excel.Application
Set xlsBook = xlsApp.Workbooks.Open(App.Path & "\Book3.xls")
Set xlsSheet = xlsBook.Worksheets(1)
range = 2 '第二列为起始列,但每次写入时,range并不一定是2,而是在原有基础上另起1列
'第一列,写入奇数数据
Do Until xlsSheet.Cells(2, range) = ""
range = range + 1
Loop
xlsSheet.Cells(1, range) = Date '第1行
xlsSheet.Cells(2, range) = Time '第2行
xlsSheet.Cells(3, range) = "回路1电压(V)" '第3 行
X = (ll + 1) / 2 - 1
For ii = 1 To X Step 2 '从第4行开始存数据
V1 = indata(ii) * na
V3 = Format(V1, "0.000")
xlsSheet.Cells((ii + 1) / 2 + 3, range) = V3 '第4行
Next ii
'另起一列,写入偶数数据
Do Until xlsSheet.Cells(2, range) = ""
range = range + 1
Loop
xlsSheet.Cells(2, range) = Time '第2行
xlsSheet.Cells(3, range) = "回路2电压(V)" '第3行
For ii = 2 To X + 1 Step 2 '从第4行开始存数据
V2 = indata(ii) * nb
V4 = Format(V2, "0.000")
xlsSheet.Cells(ii / 2 + 3, range) = V4
Next ii
xlsBook.Save
xlsBook.Application.Quit
strHex = "" '处理完成后清空字符串,等待下一次接收
'TextReceive = ""
End Select
End Sub
保存后是这个样子的
在编译之前要在工程里引用microsoft excel 12.0 object librarty
acess是可以直接打开EXCEL表格的
|
-
|