VB.net编写上位机软件时候的数据处理小工具+数据库操作

博主最近在编写上位机软件,毕竟博主本质还是做嵌入式的,以下总结了一些常用数据数理函数,非常非常基础,权当是对自己工作的总结。语言是.net 。

一. 串口通讯(RS232)

  1. 引入Imports System.IO.Ports
    在这里插入图片描述
    2.打开串口

    Dim RS232 As SerialPort           '定义SerialPort
    
    Dim mBaudRate As Integer          '五个相关参数
    Dim mParity As IO.Ports.Parity
    Dim mDataBit As Integer
    Dim mStopBit As IO.Ports.StopBits
    Dim mPortName As String
    
    mPortName = My.Settings.set_com
    mBaudRate = 9600
    mParity = Parity.None
    mDataBit = 8
    mStopBit = StopBits.One
    
    RS232 = New IO.Ports.SerialPort(mPortName, mBaudRate, mParity, mDataBit, mStopBit)
    
    	If Not RS232.IsOpen Then
            Try
                RS232.Open()
                flag_rs232 = True 
                Timer1.Enabled = True
    
            Catch ex As Exception
    
            End Try
        End If
    

    3.关闭串口

    If (flag_rs232 = True) Then
    
        Try
            RS232.Close()
            flag_rs232 = False
            Timer1.Enabled = False
        Catch ex As Exception
            Application.Exit()
        End Try
    End If
    

    4.串口发送

    Sub send_direction(type As String, address As String)
    
    	Dim outByte() As Byte    '发的是byte数组
    	'指令(该函数返回一个byte数组)
    	outByte = direction_Hex(type, address)
    
    	RS232.DiscardInBuffer() '清缓存
    	RS232.Write(outByte, 0, outByte.Length)
    
    End Sub
    

    碎碎念:

    Public Function direction_Hex
        Dim outByte(5) As Byte
        outByte(0) = &H3 
        outByte(1) = &H3   
        outByte(2) = &H0
        outByte(3) = &H64
        outByte(4) = &H0
        outByte(5) = &H1
    
    	Return outByte
    End Function
    

    4.串口接收

    Function readbtye(type As String, address As String)
    	Dim inDataLen As Integer = 0
    
    	If flag_rs232 = True Then
    
        	inDataLen = RS232.BytesToRead()     '从RS232缓冲区获取可读取的字节数
        	Dim inBytes(inDataLen - 1) As Byte  '回传的字节数组
        	RS232.Read(inBytes, 0, inDataLen)   '读取数据
        	Return (inDataLen)
    	Else
        	Return 0
    	End If
    
    End Function
    

二、 延时函数

	Public Sub TimeDelay(ByVal DT As Integer)

    Dim st As Long = Now.Ticks
    Dim st1 As Long = Now.Ticks
    Dim st2 As Integer = 0
    Do
        st2 = CInt((st1 - st) / 10000)
        Select Case st2 - DT
            Case Is < 0
                Application.DoEvents()
                st1 = Now.Ticks
            Case Else
                '达到条件下,延时结束
                Exit Do
        End Select
    Loop
End Sub

三、 各种数据类型转化

  1. 十六进制字符串转十进制数字

    Dim a As String
    a = Val("&H0A")
    Debug.Print(a)      ’输出10
    
  2. 十六进制byte 转 十进制/十六进制数字/字符串

      Dim b(5) As Byte
      Dim a As String
      b(0) = Val("&H0A")
      a = [String].Format("{0:D2}", b(0))   ’输出10
      a = [String].Format("{0:X2}", b(0))   ’输出0A
    
  3. 十六进制字符串转 十六进制byte

       Dim direction_string As String = "101010"   
       Dim outByte(direction_string.Length / 2) As Byte
         For i As Integer = 1 To direction_string.Length Step 2
             outByte((i - 1) / 2) = Val("&H" + Mid(direction_string, i, 2))
         Next
         
     	'即输入101010,输出byte(3)为(0x10,0x10,0x10)
    

四、 数据库操作

  1.   Public Function insertInto([date] As String, time As String, number As String, section As String, tem As String, shidu As String, lefttime As String)
      
      	‘hwhs.mdb是存放于你debug和release下的文件
     	Dim oleConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=hwhs.mdb")
     	Dim sql As String
     	
     	’插入语句,日期,时间,设备号,段号,温度,湿度,剩余时间为表格的title
     	sql = "insert into DATA(日期,时间,设备号,段号,温度,湿度,剩余时间) values( @日期, @时间,@设备号,@段号,@温度, @湿度,@剩余时间) "
     	Dim oleCommand As New OleDbCommand(sql, oleConnection)
     	oleCommand.Parameters.AddWithValue("@日期", [date])
     	oleCommand.Parameters.AddWithValue("@时间", time)
     	oleCommand.Parameters.AddWithValue("@设备号", number)
     	oleCommand.Parameters.AddWithValue("@段号", section)
     	oleCommand.Parameters.AddWithValue("@温度", tem)
     	oleCommand.Parameters.AddWithValue("@湿度", shidu)
     	oleCommand.Parameters.AddWithValue("@剩余时间", lefttime)
    
     	oleConnection.Open()
     	oleCommand.ExecuteNonQuery()
     	oleConnection.Close()
     	Return Nothing
     End Function
    

    access里日期时间要调成date 的格数据格式
    注:程序里获取当前时间: Dim time As Date = DateTime.Parse(DateTime.Now.ToString())在这里插入图片描述

  2. Public Sub delete(number As String, dateS As String, dateE As String)
    Dim order As String
    Dim oleConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=hwhs.mdb")
    order = "delete from DATA where 设备号=@number and 日期 between @DateS and @DateE"
    Dim oleCommand As New OleDbCommand(order, oleConnection)
    oleCommand.Parameters.AddWithValue("@number", number)
    oleCommand.Parameters.AddWithValue("@DateS", dateS)
    oleCommand.Parameters.AddWithValue("@DateE", dateE)
    Dim adapter As New OleDbDataAdapter(oleCommand)
    Dim table As New DataTable()
    oleConnection.Open()
    oleCommand.ExecuteNonQuery()
    oleConnection.Close()
    
    End Sub
    
  3. Public Function search(number As String, dateS As String, dateE As String)
    Dim order As String
    Dim oleConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=hwhs.mdb")
    
    order = "select 日期,时间,设备号,段号,温度,湿度,剩余时间 from DATA where 设备号 = '01' and 日期 between @DateS and @DateE order by 日期 asc"
    oleConnection.Open()
    Dim oleCommand As New OleDbCommand(order, oleConnection)
    oleCommand.Parameters.AddWithValue("@DateS", dateS)
    oleCommand.Parameters.AddWithValue("@DateE", dateE)
    
    Dim adapter As New OleDbDataAdapter(oleCommand)
    Dim table As New DataTable()
    adapter.Fill(table)
    
    Return table
    End Function
    
    
    ‘使用该方法
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    DataGridView1.DataSource = search(ComboBox1.Text, DateTimePicker1.Value, DateTimePicker2.Value)
    
    ’自定义格式化日期
    Dim style As New DataGridViewCellStyle
    Dim style1 As New DataGridViewCellStyle
    style.Format = "HH:mm:ss"
    style1.Format = "yyyy/MM/dd"
    
    DataGridView1.Columns(0).DefaultCellStyle = style1
    DataGridView1.Columns(1).DefaultCellStyle = style
    
    End Sub
    

五、 小操作

  1. 鼠标右键事件:

     Private Sub DataGridView1_MouseUp(sender As Object, e As MouseEventArgs) Handles DataGridView1.MouseUp
     	If e.Button = Windows.Forms.MouseButtons.Right Then
         	ContextMenuStrip1.Show(Me, PointToClient(Cursor.Position))
     	End If
     End Sub
    
  2. 弹出的小菜单:
    ToolStripMenu

  3. datagridview相关

       DataGridView11.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    
         DataGridView11.Rows.Add()
    
         DataGridView11.Rows(Set_count).Cells(0).Value = section
         DataGridView11.Rows(Set_count).Cells(1).Value = data_setting(1)
         DataGridView11.Rows(Set_count).Cells(2).Value = data_setting(2)
         DataGridView11.Rows(Set_count).Cells(3).Value = data_setting(0)
    
    
         Set_count = Set_count + 1
    
         If Set_count >= 50 Then
             DataGridView11.Rows.Clear()
             Set_count = 0
         End If
    
         DataGridView11.FirstDisplayedScrollingRowIndex = DataGridView11.RowCount - 1
    
  4. 关于页面打开与关闭

     Me.Hide()  '当前页面
     Dim dl As FormLanguage
     dl = New FormLanguage()
     dl.ShowDialog()  '注意顺序
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值