⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 VB SerialPort,SQL2
💻 VB
字号:
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim StrConn As String
        StrConn = "Server=localhost;Database=Northwind;Integrated Security=SSPI"
        Dim CnnWind As SqlConnection = New SqlConnection(StrConn)
        Dim sdrReader As SqlDataReader
        Try
            CnnWind.Open()
            Dim strSQL As String
            strSQL = "select Orders.*,ProductID,UnitPrice,Quantity,Discount from Orders Inner Join [Order Details] on " + _
            "Orders.OrderID=[Order Details].OrderID where CustomerID='ALFKI'"
            Dim cmdReader As SqlCommand = New SqlCommand(strSQL, CnnWind)
            sdrReader = cmdReader.ExecuteReader(CommandBehavior.CloseConnection)
            With sdrReader
                If .HasRows Then
                    While .Read
                        Dim s_intOrderID As SqlInt32 = .GetInt32(0)
                        Dim s_strCustomerID As SqlString = .GetString(1)
                        Dim s_datOrderDate As SqlDateTime = .GetDateTime(3)
                        Dim s_curUnitPrice As SqlMoney = .GetSqlMoney(15)
                        Dim s_sngdiscount As SqlSingle = .GetSqlSingle(17)
                    End While
                End If
            End With
            sdrReader.Close()
        Catch ex As Exception
            MsgBox(ex.Message + ex.StackTrace)
        Finally

        End Try
    End Sub
    '程序启动地址
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim App_Path As String
        App_Path = Application.StartupPath
        MsgBox(App_Path)
    End Sub

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        SerialPort1.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Open()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ReadPort()
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        Timer1.Enabled = CheckBox1.Checked
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ReadPort()
    End Sub
    Sub ReadPort()
        Dim SendText(12) As Byte
        Dim i As Int16
        Dim temp(20) As Byte
        Dim Number As Int16
        Try
            Number = SerialPort1.BytesToRead
            RichTextBox1.Text = RichTextBox1.Text + Str(Number) + Chr(13) + Chr(10)
            SerialPort1.Read(temp, 1, SerialPort1.BytesToRead)
            For i = 1 To Number
                RichTextBox1.Text = RichTextBox1.Text + Str(temp(i))
            Next
            RichTextBox1.Text = RichTextBox1.Text + Chr(13) + Chr(10)
            SendText(0) = &H81
            SendText(1) = &H81
            SendText(2) = &H52
            SendText(3) = 0
            SendText(4) = 0
            SendText(5) = 0
            i = 83
            SendText(7) = CByte(i \ 256)
            SendText(6) = CByte(i Mod 256)
            SerialPort1.Write(SendText, 0, 8)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        SerialPort1.DtrEnable = CheckBox2.Checked
    End Sub

    Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        SerialPort1.RtsEnable = CheckBox2.Checked
    End Sub

    Private Sub ComboBox_Port_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox_Port.SelectedIndexChanged
        SerialPort1.PortName = ComboBox_Port.Text
    End Sub

    Private Sub CheckBox2_CheckedChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged

    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -