vb 停止按钮_如何使用VB.Net开发简单的温度巡检仪PC端软件

学习http://VB.NET实践所编,力有不殆,如有错漏,欢迎请各位专家指教

9835894be7e9c3cc5b14682482995d66.png
https://www.zhihu.com/video/1207104009678594048

1.开发环境

  • 使用微软公司官方的Visual Studio 社区版进行http://VB.Net的开发,
  • 官网下载链接: https://visualstudio.microsoft.com/zh-hans/vs/?rr=https%3A%2F%2Fwww.sogou.com%2Flink%3Furl%3DhedJjaC291MwzWuxQEdpNeCoskMohj34iaWuGUdgGbet5pUc3zy0dg..

22d024efd7c4502e355bd7ef4ba93f10.png

(图片来自官网截图)

  • 建立项目的步骤:
    • 1.创建新的http://VB.Net窗体应用

9947efbc70e8e31cb33f5c817d2d15fc.png
  • 项目命名

8231e475c66e3db9609ac3028e394ca2.png
  • 项目布局

9bf8f4e0aaf966847daee51ee3424847.png

2.窗体设计

  • Form1 窗体设计
    • Menu菜单栏
    • label+ComboBox 组成的通信参数
    • 用于打开和关闭通信的按钮
    • Panel区域用于放置Form2和Form3构成显示实时数据和图线变化的窗体
    • 需要另外添加 SerialPort用于串口通信,Timer用于定时完成通讯动作

4d936ac77ebf49a2cdf290bdd5f2d9f9.png
  • Form2窗体
    • 使用Text控件显示通道名称,通信时可自行定义名称
    • Label修改外观后用于显示数据
    • Form2将嵌入Form1的Panel区域,使用菜单栏切换Panel的显示内容(Form2或Form3)

b75a272193bcde78d1bdca7012048652.png
  • Form3窗体
    • 使用Chart控件在Form3中显示各系列数据的变化,检测数据的变化趋势

1605ec5c8dee5ca51d33318245459927.png
  • 其他功能设计
    • 程序运行时将打开(或新建)log.dat文件,将当前日期写入(方便后续查看数据记录)
    • SerialPort 与串口设备通讯均采用16进制,可在通道数中更改和自定义发送的命令(上传的代码目前只适用于16通道全采集,未作所用情形的适配)
    • 串口接受数据后将16进制转换为整数数组,按照ModBus通讯协议,对数据部分进行计算后逐行记录到log.dat中,并显示到Form2中和Form3的线图中
    • 历史数据查询和数据导出功能(上传代码中未实现)

3.具体代码

  • 本程序代码均集成在Form1的代码中,无需对Form2和Form3编码
  • 本项目文件已经上传GitHub:
Imports System
Imports System.IO.Ports
Public Class Form1
    Private Sub 连接ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles 连接ToolStripMenuItem1.Click
    End Sub
    '菜单栏打开实时数据标签,Panel中显示Form2窗体
    Private Sub ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem1.Click
        Form3.Visible = False
        Form2.TopLevel = False
        Form2.FormBorderStyle = FormBorderStyle.None
        Form2.WindowState = FormWindowState.Maximized
        Form2.Parent = Panel1
        Form2.Show()
    End Sub
    Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
    End Sub
    '菜单栏打开图线标签,Panel中显示Form3窗体
    Private Sub ToolStripMenuItem2_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem2.Click
        Form2.Visible = False
        Form2.Close()
        Form3.TopLevel = False
        Form3.FormBorderStyle = FormBorderStyle.None
        Form3.WindowState = FormWindowState.Maximized
        Form3.Parent = Panel1
        Form3.Show()
    End Sub
    Private Sub ToolStripMenuItem3_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem3.Click
    End Sub
    '定义几个全局变量,方便其他Sub或者Function调用
    Public Com2Send As Byte()
    Public Recieve_String() As Integer
    '用于打开串口通信的按钮
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Static count As Integer = 1
        If (Button1.Text = "Start" Or Button1.Text = "未连接") Then
            Try
                Serial_con()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            If (SerialPort1.IsOpen) Then
                Button1.Text = "Stop"
                Button1.BackColor = Color.Green
                Button1.ForeColor = Color.Red
                'timer设置
                Com2Send = Comforsend(ComSelect())
                Dim i As Integer
                If count = 1 Then
                    For i = 0 To Val(ComboBox7.Text)
                        Form3.Chart1.Series.Add("CH" & i)
                    Next
                End If
                Timer1.Enabled = True
            Else
                Button1.Text = "未连接"
                Button1.BackColor = Color.Red
            End If
        ElseIf (Button1.Text = "Stop") Then
            Timer1.Enabled = False
            Button1.Text = "Start"
            Button1.BackColor = SystemColors.Control
            Button1.ForeColor = Color.Black
            Try
                SerialPort1.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            count += 1
        End If
    End Sub
'窗口载入过程
    Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '获取端口信息,并添加进下拉菜单
        Dim ports As String() = SerialPort.GetPortNames()
        Dim port As String
        For Each port In ports
            ComboBox1.Items.Add(port)
        Next port
        '初始化控制面板
        ComboBox2.SelectedIndex = 0
        ComboBox3.SelectedIndex = 0
        ComboBox4.SelectedIndex = 0
        ComboBox5.SelectedIndex = 0
        ComboBox6.SelectedIndex = 0
        ComboBox7.SelectedIndex = 0
        ComboBox8.SelectedIndex = 1
        ComboBox1.SelectedIndex = 0
        '加载数值显示画面
        Form2.TopLevel = False
        Form2.FormBorderStyle = FormBorderStyle.None
        Form2.WindowState = FormWindowState.Maximized
        Form2.Parent = Panel1
        Form2.Show()
        '建立一个log文件存储数据
        FileOpen(1, "log.dat", OpenMode.Append, OpenAccess.ReadWrite)
        Print(1, Format(Now(), "yyyy/MM/dd-H:mm:ss,") & vbCrLf)
        FileClose(1)
        Form3.Chart1.Series.RemoveAt(0)
        Form3.Chart1.ChartAreas(0).AxisX.Title = "时间 / min"
        'Form3.Chart1.ChartAreas(0).AxisY.Title = " 数据"
    End Sub
    '串口参数
    Private Sub Serial_con()
        SerialPort1.PortName = ComboBox1.Text '串口名称
        SerialPort1.BaudRate = Val(ComboBox2.Text) '波特率
        SerialPort1.DataBits = Val(ComboBox3.Text) '数据位
        SerialPort1.StopBits = Val(ComboBox5.Text) '停止位
        SerialPort1.Parity = Val(ComboBox4.Text) '校验位
        SerialPort1.Open() '打开串口
    End Sub
'退出程序按钮
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        End
    End Sub
'定时器,每隔一定时间进行通讯操作
    Public Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Timer1.Interval = Val(ComboBox8.Text) * 1000
        PortSend(Com2Send)
        Recideal()
    End Sub
'数据写入文件的过程
Public Sub Log_file(ByVal LogString As String)'需调用其他的数据
        FileOpen(1, OpenMode.Append, OpenAccess.ReadWrite)
        Print(1, LogString)
        FileClose(1)
    End Sub
'串口接收数据
    Public Sub Recideal()
        Dim n As Integer
        Dim i As Integer
        Dim rc() As Byte
        Dim RecStr As String = ""
        n = SerialPort1.BytesToRead
        ReDim rc(n)
        ReDim Recieve_String(n)
        Try
            If n > 0 Then
                For i = 0 To (n - 1)
rc(i) = SerialPort1.ReadByte()'逐字节接收数据
Recieve_String(i) = Val(rc(i))'每字节16进制转换为整数
                    RecStr += rc(i) & ","
                Next
'数据写入文件
				FileOp(RecStr)
'Form2显示数据
                LCDShow()
'画图
                LineDraw()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message & "接收数据错误")
        End Try
    End Sub
    '选择发送命令
    Public Function ComSelect() As String
        Dim ComIndex As Integer
        ComIndex = Val(ComboBox7.Text)
        Select Case ComIndex
            Case 16
                ComSelect = "01 03 00 00 00 10 44 06"
            Case 15
                ComSelect = "01 03 00 00 00 0F 05 CE"
            Case 14
                ComSelect = "01 03 00 00 00 0E C4 0E"
            Case Else
                ComSelect = InputBox("请输入16进制通讯命令:")
        End Select
    End Function
    '将待发送数据转换为16进制
    Public Function Comforsend(ByVal CommandString As String) As Byte()
        Dim TestArray() As String = Split(CommandString) '分割通信数据
        Dim hexBytes() As Byte '定义数组
        ReDim hexBytes(TestArray.Length - 1) '重定义
        Dim i As Integer
        For i = 0 To TestArray.Length - 1
            hexBytes(i) = Val("&h" & TestArray(i))
        Next
        Comforsend = hexBytes
    End Function
    '发送数据
    Public Sub PortSend(ByVal Senddata As Byte())
        Try
            SerialPort1.Write(Senddata, 0, Senddata.Length)
        Catch ex As Exception
            MessageBox.Show(ex.Message & "发送数据错误")
        End Try
    End Sub
    Public Sub FileOp(ByVal fo As String)
        Try
            FileOpen(1, "log.dat", OpenMode.Append, OpenAccess.Write)
            Print(1, fo & vbCrLf)
            FileClose(1)
        Catch ex As Exception
            MessageBox.Show(ex.Message & "读写文件错误")
        End Try
    End Sub
    Public Sub LCDShow()
        Try
            Form2.Label10.Text = Recieve_String(4) + Recieve_String(5) * 0.1 '1
            Form2.Label11.Text = Recieve_String(6) + Recieve_String(7) * 0.1
            Form2.Label12.Text = Recieve_String(8) + Recieve_String(9) * 0.1
            Form2.Label13.Text = Recieve_String(10) + Recieve_String(11) * 0.1
            Form2.Label14.Text = Recieve_String(12) + Recieve_String(13) * 0.1 '5
            Form2.Label15.Text = Recieve_String(14) + Recieve_String(15) * 0.1
            Form2.Label16.Text = Recieve_String(16) + Recieve_String(17) * 0.1
            Form2.Label17.Text = Recieve_String(18) + Recieve_String(19) * 0.1
            Form2.Label18.Text = Recieve_String(20) + Recieve_String(21) * 0.1
            Form2.Label19.Text = Recieve_String(22) + Recieve_String(23) * 0.1 '10
            Form2.Label20.Text = Recieve_String(24) + Recieve_String(25) * 0.1
            Form2.Label21.Text = Recieve_String(26) + Recieve_String(27) * 0.1
            Form2.Label22.Text = Recieve_String(28) + Recieve_String(29) * 0.1
            Form2.Label23.Text = Recieve_String(30) + Recieve_String(31) * 0.1
            Form2.Label24.Text = Recieve_String(32) + Recieve_String(33) * 0.1 '15
            Form2.Label25.Text = Recieve_String(34) + Recieve_String(35) * 0.1 '16
        Catch ex As Exception
            MessageBox.Show(ex.Message & "LCD显示错误")
        End Try
    End Sub
    Public Sub LineDraw()
        Try
            Static Count As Integer = 1
            Dim i As Integer
            Dim yvalue As Double
            For i = 0 To Val(ComboBox7.Text)
                'Form3.Chart1.Series(i).Points.Add()
                yvalue = Recieve_String(i + 3) + Recieve_String(i + 4) * 0.1
                Form3.Chart1.Series(i).Points.AddXY(Format((Count - 1) * Val(ComboBox8.Text) / 60, "0.00"), yvalue)
                Form3.Chart1.Series(i).ChartType = DataVisualization.Charting.SeriesChartType.Line
            Next
            Count += 1
        Catch ex As Exception
            MessageBox.Show(ex.Message & "LineDraw")
        End Try

    End Sub
End Class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值