下面是我的串口接收程序的一部分:
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
Open "12.txt" For Append As #1 ‘打开 文本文件
m = (ll + 1) / 2
Print #1, Now"收到"m"个数据"
Close #1
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
X = (ll + 1) / 2 - 1
For ii = 1 To X Step 2 '存入文本中,每行两个数据
V1 = indata(ii) * na
V2 = indata(ii + 1) * nb
V3 = Format(V1, "0.000")
V4 = Format(V2, "0.000")
Open "12.txt" For Append As #1
Print #1, ii &"电压 " &V3 &" v " &(ii + 1) &"电压 " &V4 &" v"
Close #1
Next
'写入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 行
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
既然是数据库,数据肯定比较多,应该借助上位机的海量存储能力和强大运算能力及丰富的软件,最简单的方法就是将单片机采集到的数据经过串口传到上位机,由上位机保存到EXCEl表格中,EXCEL表格就是数据库的一部分你需要做个个串口通讯,将单片机的数据上载到电脑端,电脑端要写一个上位机。可讲通讯过来的数据存入 sql server的数据库中单片机端需要写串口程序,PC 端需要些上位机软件 VB可以写用MSComm1 控件
通讯例子如下
Dim i As Integer
Dim a As Integer
Dim t() As Byte
Private Sub Command1_Click()
If Command1.Caption = "打开串口" Then
'Shape1.Visible = True
Shape1.FillColor = &HFF&
Shape1.BackColor = &HFF&
Shape1.BorderColor = &HFF&
Command2.Enabled = True
Command3.Enabled = True
Command1.Caption = "关闭串口"
'MSComm1.CommPort = 1 '设定Com1口
MSComm1.CommPort = Combo1.Text '设定Com1口
If MSComm1.PortOpen = False Then
'MSComm1.Settings = "4800,N,8,1" '4800波特率,无校验,8位数据位,1位停止位
MSComm1.Settings = Combo2.Text &"," &Combo3.Text &"," &Combo4.Text &"," &Combo5.Text '4800波特率,无校验,8位数据位,1位停止位
MSComm1.InputLen = 1 '读取接收缓冲区的所有字符
MSComm1.InputLen = 5
MSComm1.InBufferSize = 5 '设置接收缓冲区为1024字节
MSComm1.OutBufferSize = 5 '设置发送缓冲区为512字节
MSComm1.SThreshold = 0 '不触发发送事件
MSComm1.RThreshold = 1 '每一个字符到接收缓冲区都触发接收事件
MSComm1.PortOpen = True '打开串口
End If
MSComm1.OutBufferCount = 0 '清空发送缓冲区
MSComm1.InBufferCount = 0 '清空接收缓冲区
Dim temp() As Byte
Else
'Option2.Value = False
'Option1.Value = False
'Shape1.Visible = False
Shape1.BackColor = &H808080
Shape1.BorderColor = &H808080
Shape1.FillColor = &H808080
Command2.Enabled = False
'Command3.Enabled = False
MSComm1.PortOpen = False
Command1.Caption = "打开串口"
End If
End Sub
Private Sub Command2_Click()
'Command2.Enabled = False
'Option2.Value = False
'Option1.Value = False
'Shape2.Visible = False
MSComm1.OutBufferCount = 0 '清空发送缓冲区
MSComm1.InBufferCount = 0 '清空接收缓冲区
't(0) = "255"
' MSComm1.Output = t
MSComm1.Output = Text2.Text
End Sub
Private Sub Command3_Click()
Text1.Text = ""
End Sub
Private Sub Command5_Click() '退出
End
End Sub
Private Sub Command6_Click()
Text2.Text = ""
End Sub
Private Sub Command7_Click()
If Command7.Value Then
'Command7.Visible = False
Command1.Visible = True
Command2.Visible = True
Command3.Visible = True
Command6.Visible = True
Label1.Visible = True
Label2.Visible = True
Label3.Visible = True
Label4.Visible = True
Label5.Visible = True
Label6.Visible = True
Label7.Visible = True
Combo1.Visible = True
Combo2.Visible = True
Combo3.Visible = True
Combo4.Visible = True
Combo5.Visible = True
Text1.Visible = True
Text2.Visible = True
Frame1.Visible = True
Frame2.Visible = False
'Command3.Visible = False
'Option1.Value = False
Shape1.Visible = True
Else
Label1.Visible = False
Label2.Visible = False
Label3.Visible = False
Label4.Visible = False
Label5.Visible = False
Label6.Visible = False
Label7.Visible = False
Combo1.Visible = False
Combo2.Visible = False
Combo3.Visible = False
Combo4.Visible = False
Combo5.Visible = False
Command1.Visible = False
Command2.Visible = False
Command3.Visible = False
Text1.Visible = False
Text2.Visible = False
Frame1.Visible = False
Frame2.Visible = False
End If
End Sub
Private Sub Form_Load()
MSComm1.InBufferCount = 0 '清除发送缓冲区数据
MSComm1.OutBufferCount = 0 '清除接收缓冲区数据
ReDim t(0)
t(0) = "0"
Combo1.AddItem "1"
Combo1.AddItem "2"
Combo1.AddItem "3"
Combo1.AddItem "4"
'Combo1.Text = "1"
Combo2.AddItem "300"
Combo2.AddItem "600"
Combo2.AddItem "1200"
Combo2.AddItem "2400"
Combo2.AddItem "4800"
Combo2.AddItem "9600"
Combo2.AddItem "19200"
Combo3.AddItem "N"
Combo3.AddItem "O"
Combo3.AddItem "E"
Combo4.AddItem "8"
Combo4.AddItem "7"
Combo4.AddItem "6"
Combo5.AddItem "1"
Combo5.AddItem "2"
Combo5.AddItem "3"
End Sub
Private Sub MSComm1_OnComm()
Dim s1 As String
Dim s2 As String
Select Case MSComm1.CommEvent
Case comEventOverrun '数据丢失
Text_SEND.Text = "" '清空发送缓冲区
Text_RECV.Text = "" '清空接收缓冲区
Text_SEND.SetFocus
Exit Sub
Case comEventRxOver '接收缓冲区溢出
Text_SEND.Text = "" '清空发送缓冲区
Text_RECV.Text = "" '清空接收缓冲区
Text_SEND.SetFocus
Exit Sub
Case comEventTxFull '发送缓冲区已满
Text_SEND.Text = "" '清空发送缓冲区
Text_RECV.Text = "" '清空接收缓冲区
Text_SEND.SetFocus
Exit Sub
Case comEvReceive '接收字符数据
Dim Buffer As Variant
MSComm1.InputLen = 1
MSComm1.InputMode = comInputModeBinary '二进制传输
Buffer = MSComm1.Input
'Text1.Text = Buffer(0)
's2 = Buffer(1)
Text1.Text = Text1.Text &(Val(MSComm1.Input)) &" "
Case Else
End Select
End Sub
Private Sub Option1_Click()
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)