1、GCCX上位机制作总结本系统的上位机制作主要采用 Microsoft Word Visual Basic 6.0编写。主要实现将采集和传输的数据进行接收、处理和储存的功能。一、基本思路如下所示:开始 从串口接收数据数据实时显示将数据存储到数据库中,并实现查询结束由上图可以知道,本程序的三个要点分别是:1、数据的接收部分,主要指将传到串口的数据进行接收,这是后面步骤的前提与基础。2、数据的处理部分,将接收到的数据进行实时处理与绘制曲线图,并对超过警戒值的数据给予提示。3、数据的储存部分,指对接收处理过的数据按照一定的格式存储到access2007数据库中,以便以后查询,也为分析列车运行规律以及螺
2、丝生产厂家提供真实可贵的资料。总体思路是先分别实现三个部分对应的功能,再将三个部分有机联合起来形成功能完善的上位机系统。二、分别实现三个部分的制作过程及成果第一部分,从串口接收数据。主要用到的是 MSComn控件( )实现串口通信。其测试设计界面如下图所示:其中 MSComn控件的属性设置为:程序如下:测试便可实时显示串口传输的数据。即实现了从串口接收数据。第二部分,数据的实时显示部分。这个部分的测试与制作的思路是采用随机函数产生数据并与时间控件( )配合来模拟所收集的数据,对其进行实时绘图。测试设计界面:程序如下:Option ExplicitDim DataFromComy As Inte
3、ger 从串口读过来的实时值 y坐标Dim DataFromComLasty As Integer 上次的串口值 y坐标Dim TimeCount As IntegerDim i As IntegerPrivate Sub Command1_Click()返回按钮Unload MeEnd SubPrivate Sub Form_Load()PicScale Pic 调整图像框的坐标系zuobiaoxi Pic 画出坐标系PicMidleLine Pic 在图像框中画一条中线End SubPrivate Sub PicScale(picX As PictureBox) 调整图像框的坐标系picX
4、.Scale (0, picX.ScaleHeight)-(picX.ScaleWidth, 0)End SubPrivate Sub PicMidleLine(picX As PictureBox) 在图像框中画一条中线picX.Line (0, 1000)-(picX.ScaleWidth, 1000), vbGreen 画出中线End SubPrivate Sub zuobiaoxi(picX As PictureBox) 表示坐标系Dim i%picX.Line (0, 0)-(picX.ScaleWidth, 0), vbWhite x坐标轴表示picX.Line (0, 0)-(0
5、, -picX.ScaleHeight), vbWhite y坐标轴表示For i = 0 To picX.ScaleWidth Step 1000 X,y轴坐标坐标刻度显示picX.Line (0, i)-(100, i)picX.Line (i, 0)-(i, 100)Next iEnd SubPrivate Sub DrawRealLine(picX As PictureBox, DataFromComy As Integer, DataFromComLasty As Integer, TimeCount As Integer)If TimeCount - 1 0 ThenpicX.Li
6、ne (TimeCount - 100, DataFromComLasty)-(TimeCount, DataFromComy), vbWhiteEnd IfEnd SubPrivate Sub Timer1_Timer()DataFromComLasty = DataFromComyRandomizeDataFromComy = Int(2000 - 0 + 1) * Rnd + 0) 随机数的产生TimeCount = TimeCount + 100If TimeCount - 100 = Pic.Width ThenTimeCount = 0Pic.ClsPicMidleLine Pic
7、zuobiaoxi PicEnd IfDrawRealLine Pic, DataFromComy, DataFromComLasty, TimeCount 画出实时的曲线End Sub测试结果如下:至此,由数据的实时绘图部分的测试完成。第三部分,数据库的操作,数据库采用 access2007数据库。并采用 Visual Basic 的 ADO Data 控件( )进行连接和 SQL语言实现数据库的添加,删除和查阅等基本功能。 (本实验以姓名、性别、年龄的操作为例)测试设计界面:创建数据库 mydb如下:首先是数据库的连接:利用 ADO控件生成字符串连接数据库的步骤为:1)右击 data控件,
8、选择 ADODC属性2)点击生成,选择数据库类型3)填写数据源4)测试连接其次是数据库操作:1) 添加:Dim rs As New ADODB.RecordsetDim cn As New ADODB.ConnectionPrivate Sub OpenConn()Set cn = New ADODB.ConnectionSet rs = New ADODB.Recordsetcn.CursorLocation = adUseClientcn.Open “Provider=Microsoft.ACE.OLEDB.12.0;DataSource=C:UsersAdministratorDeskt
9、opGCCX数据库mydb.accdb;Persist Security Info=False“End SubPrivate Sub CloseConn()If rs.State sckclosed Then cn.CloseSet cn = NothingEnd SubPrivate Sub Command1_Click()Call OpenConnrs.Open “select 姓名,性别,年龄 from 表 1“, cn,adOpenStatic, adLockPessimisticrs.AddNewrs.Fields(“姓名“) = Text1.Textrs.Fields(“性别“)
10、= Text2.Textrs.Fields(“年龄“) = Text3.Textrs.UpdateCall CloseConnMsgBox “添加成功!“, , “温馨提示“Text1.Text = “Text2.Text = “Text3.Text = “End SubPrivate Sub Command2_Click()Unload MeEnd SubPrivate Sub Command3_Click()Unload MeForm3.ShowEnd SubPrivate Sub Command4_Click()Form5.ShowUnload MeEnd Sub结果:2) 删除:Pri
11、vate cn As New ADODB.ConnectionDim rs As New ADODB.RecordsetPrivate Sub OpenConn()Set cn = New ADODB.ConnectionSet rs = New ADODB.Recordsetcn.CursorLocation = adUseClientcn.Open “Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:UsersAdministratorDesktopGCCX数据库mydb.accdb;Persist Security Info=False“En
12、d SubPrivate Sub CloseConn()If rs.State sckclosed Then cn.CloseSet cn = NothingEnd SubPrivate Sub Command1_Click()Call OpenConnrs.Open “select ID from 表 1 Where ID=“ DataSource=C:UsersAdministratorDesktopGCCX数据库mydb.accdb;Persist Security Info=False“End SubPrivate Sub CloseConn()If rs.State sckclose
13、d Then cn.CloseSet cn = NothingEnd SubPrivate Sub Command1_Click()Call OpenConnrs.Open “select ID,姓名,性别,年龄 from 表 1 Where ID=“ Data Source=C:UsersAdministratorDesktopGCCX数据库mydbnew.accdb;Persist Security Info=False“End SubPrivate Sub CloseConn()rs.Closecn.CloseSet rs = NothingSet cn = NothingEnd Sub
14、Private Sub Form_Load()PicScale Pic 调整图像框的坐标系PicMidleLine Piczuobiaoxi PicEnd SubPrivate Sub PicScale(picX As PictureBox)调整图像框的坐标系picX.Scale (0, picX.ScaleHeight)-(picX.ScaleWidth, 0)End SubPrivate Sub PicMidleLine(picX As PictureBox) 在图像框中画一条中线picX.Line (0, 1500)-(picX.ScaleWidth, 1500), vbRed 画出中线
15、End SubPrivate Sub zuobiaoxi(picX As PictureBox) 表示坐标系Dim i%picX.Line (0, 0)-(picX.ScaleWidth, 0) x坐标轴表示picX.Line (0, 0)-(0, -picX.ScaleHeight) y坐标轴表示 For i = 0 To picX.ScaleWidth Step 1000 X,y轴坐标坐标刻度显示picX.Line (0, i)-(100, i)picX.Line (i, 0)-(i, 100)Next iEnd SubPrivate Sub DrawRealLine(picX As Pi
16、ctureBox, DataFromComy As Integer, DataFromComLasty As Integer, TimeCount As Integer)If TimeCount - 1 0 ThenpicX.Line(TimeCount-100,DataFromComLasty)-(TimeCount, DataFromComy), vbGreenEnd IfEnd SubPrivate Sub Timer1_Timer()If k = 1 ThenDataFromComLasty = DataFromComyRandomizeDataFromComy = Int(3000
17、- 0 + 1) * Rnd + 0)随机数的产生If DataFromComy = Pic.Width ThenTimeCount = 0Pic.ClsPicMidleLine Piczuobiaoxi PicEnd IfDrawRealLine Pic, DataFromComy, DataFromComLasty, TimeCount 画出实时的曲线End IfEnd SubPrivate Sub Command1_Click()Command3.Enabled = Falsek = 1End SubPrivate Sub Command2_Click()Command3.Enabled
18、 = Truek = 0End SubPrivate Sub Command3_Click()Form2.ShowUnload MeEnd SubPrivate Sub Command4_Click()Unload MeEnd Sub实验结果:查询功能:Dim rs As New ADODB.RecordsetDim cn As New ADODB.ConnectionPrivate Sub OpenConn()Set cn = New ADODB.ConnectionSet rs = New ADODB.Recordsetcn.CursorLocation = adUseClientcn.O
19、pen“Provider=Microsoft.ACE.OLEDB.12.0;DataSource=C:UsersAdministratorDesktopGCCX数据库mydbnew.accdb;Persist Security Info=False“End SubPrivate Sub CloseConn()rs.Closecn.CloseSet rs = NothingSet cn = NothingEnd SubPrivate Sub Command1_Click()Call OpenConnrs.Open “select 时间,力 from 表 1 Where 时间=“ & Text1.
20、Text, cn, adOpenStatic, adLockPessimisticIf rs.EOF And rs.BOF ThenMsgBox “您所查询的时间没有记录“, , “温馨提示“Text1.Text = “Text1.SetFocusElseText2.Text = rs.Fields(“力“)rs.UpdateEnd IfCall CloseConnEnd SubPrivate Sub Command2_Click()Text1.Text = “Text2.Text = “Text1.SetFocusEnd SubPrivate Sub Command3_Click()Form1.ShowUnload MeEnd Sub结果: