如何将收到的消息从Gsm调制解调器保存到您的Mysql数据库?

时间:2015-10-22 00:32:45

标签: vb.net mysql-workbench

我正在尝试将收到的短信从我的调制解调器保存到MySQL数据库,我正在使用MySQL Workbench作为我的数据库。

这是我的功能:
1.自动检测调制解调器
2.连接调制解调器
3.发送消息
4。阅读留言
5.句柄(收到SerialPort数据)

以下是代码:
1。自动检测调制解调器

'Some Imports
Imports System.Management
Imports System.Threading
Imports System.Text.RegularExpressions
Imports MySql.Data.MySqlClient

Public Class Form1
    Dim result() As String
    Dim query As String
    Dim rcvdata As String = ""

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim ports() As String
        ports = Split(ModemsConnected(), "***")
        For i As Integer = 0 To ports.Length - 2
            ComboBox1.Items.Add(ports(i))
        Next
    End Sub

    Public Function ModemsConnected() As String
        Dim modems As String = ""
        Try
            Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_POTSModem")

            For Each queryObj As ManagementObject In searcher.Get()
                If queryObj("Status") = "OK" Then
                    modems = modems & (queryObj("AttachedTo") & " - " & queryObj("Description") & "***")
                End If
            Next
        Catch err As ManagementException
            MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            Return ""
        End Try
        Return modems
    End Function

'Show Detected or Available modem
    Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
        Label1.Text = Trim(Mid(ComboBox1.Text, 1, 5))
    End Sub

'Some Imports Imports System.Management Imports System.Threading Imports System.Text.RegularExpressions Imports MySql.Data.MySqlClient Public Class Form1 Dim result() As String Dim query As String Dim rcvdata As String = "" Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Dim ports() As String ports = Split(ModemsConnected(), "***") For i As Integer = 0 To ports.Length - 2 ComboBox1.Items.Add(ports(i)) Next End Sub Public Function ModemsConnected() As String Dim modems As String = "" Try Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_POTSModem") For Each queryObj As ManagementObject In searcher.Get() If queryObj("Status") = "OK" Then modems = modems & (queryObj("AttachedTo") & " - " & queryObj("Description") & "***") End If Next Catch err As ManagementException MessageBox.Show("An error occurred while querying for WMI data: " & err.Message) Return "" End Try Return modems End Function 'Show Detected or Available modem Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged Label1.Text = Trim(Mid(ComboBox1.Text, 1, 5)) End Sub

2。连接/断开调制解调器

    'button connect
            Private Sub btnConnect_Click(sender As System.Object, e As System.EventArgs) Handles btnConnect.Click
                Try
                    With SerialPort1
                        .PortName = Label1.Text
                        .BaudRate = 9600
                        .Parity = IO.Ports.Parity.None
                        .DataBits = 8
                        .StopBits = IO.Ports.StopBits.One
                        .Handshake = IO.Ports.Handshake.None
                        .RtsEnable = True
                        .ReceivedBytesThreshold = 1
                        .NewLine = vbCr
                        .ReadTimeout = 1000
                        .Open()
                    End With
                    If SerialPort1.IsOpen Then
                        Label3.Visible = True
                        btnDisconnect.Visible = True
                        Label3.Text = "Connected - Port " & Label1.Text & " is used"
                    Else
                        Label3.Text = "Got some Error, Check your connection with your Modem."
                    End If
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Text Messaging")
                End Try
            End Sub

'button Disconnect
    Private Sub btnDisconnect_Click(sender As System.Object, e As System.EventArgs) Handles btnDisconnect.Click
        Try
            If SerialPort1.IsOpen Then
                With SerialPort1
                    .Close()
                    .Dispose()
                    Label1.Visible = False
                    Label3.Visible = False
                    btnDisconnect.Visible = False
                    ListView1.Items.Clear()
                End With
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

3。发送消息 'button connect Private Sub btnConnect_Click(sender As System.Object, e As System.EventArgs) Handles btnConnect.Click Try With SerialPort1 .PortName = Label1.Text .BaudRate = 9600 .Parity = IO.Ports.Parity.None .DataBits = 8 .StopBits = IO.Ports.StopBits.One .Handshake = IO.Ports.Handshake.None .RtsEnable = True .ReceivedBytesThreshold = 1 .NewLine = vbCr .ReadTimeout = 1000 .Open() End With If SerialPort1.IsOpen Then Label3.Visible = True btnDisconnect.Visible = True Label3.Text = "Connected - Port " & Label1.Text & " is used" Else Label3.Text = "Got some Error, Check your connection with your Modem." End If Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Text Messaging") End Try End Sub 'button Disconnect Private Sub btnDisconnect_Click(sender As System.Object, e As System.EventArgs) Handles btnDisconnect.Click Try If SerialPort1.IsOpen Then With SerialPort1 .Close() .Dispose() Label1.Visible = False Label3.Visible = False btnDisconnect.Visible = False ListView1.Items.Clear() End With End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub

4。阅读消息

Private Sub btnSend_Click(sender As System.Object, e As System.EventArgs) Handles btnSend.Click
        Dim query As String
        Try
            Connection()
            query = "INSERT INTO thesis.tblsentitems (PhoneNumber, message) VALUES('" & txtNumber.Text & "', '" & txtMessage.Text & "')"
            sqlcmd = New MySqlCommand(query, conn)
            sqlreader = sqlcmd.ExecuteReader

            With SerialPort1
                .Write("at" & vbCrLf)
                Threading.Thread.Sleep(200)
                .Write("at+cmgf=1" & vbCrLf)
                Threading.Thread.Sleep(200)
                .Write("at+cmgs=" & Chr(34) & txtNumber.Text & Chr(34) & vbCrLf)
                .Write(txtMessage.Text & Chr(26))
                Threading.Thread.Sleep(200)

            End With
            If rcvdata.ToString.Contains(">") Then
                MsgBox("Message Succesfully Sent")
                conn.Close()
            Else
                MsgBox("Got some error!")
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Text Messaging")
        End Try
    End Sub

Private Sub btnSend_Click(sender As System.Object, e As System.EventArgs) Handles btnSend.Click Dim query As String Try Connection() query = "INSERT INTO thesis.tblsentitems (PhoneNumber, message) VALUES('" & txtNumber.Text & "', '" & txtMessage.Text & "')" sqlcmd = New MySqlCommand(query, conn) sqlreader = sqlcmd.ExecuteReader With SerialPort1 .Write("at" & vbCrLf) Threading.Thread.Sleep(200) .Write("at+cmgf=1" & vbCrLf) Threading.Thread.Sleep(200) .Write("at+cmgs=" & Chr(34) & txtNumber.Text & Chr(34) & vbCrLf) .Write(txtMessage.Text & Chr(26)) Threading.Thread.Sleep(200) End With If rcvdata.ToString.Contains(">") Then MsgBox("Message Succesfully Sent") conn.Close() Else MsgBox("Got some error!") End If Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Text Messaging") End Try End Sub

5。句柄(收到SerialPort数据)

    Private Sub btnReadsms_Click(sender As System.Object, e As System.EventArgs) Handles btnReadsms.Click
        Try
            With SerialPort1
                .Write("AT" & vbCrLf)
                Threading.Thread.Sleep(1000)
                .Write("AT+CMGF=1" & vbCrLf)
                Threading.Thread.Sleep(1000)
                .Write("AT+CPMS=""SM""" & vbCrLf)
                Threading.Thread.Sleep(1000)
                .Write("AT+CMGL=""ALL""" & vbCrLf)
                Threading.Thread.Sleep(1000)
                'MsgBox(rcvdata.ToString)
                readmsg()
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

'Save Received message to ListView1 
    Private Sub readmsg()
        Try
            Dim lineoftext As String
            Dim i As Integer
            Dim arytextfile() As String
            lineoftext = rcvdata.ToString
            arytextfile = Split(lineoftext, "+CMGL", , CompareMethod.Text)
            For i = 1 To UBound(arytextfile)
                Dim input As String = arytextfile(i)
                Dim pattern As String = "(:)|(,"")|("","")"
                result = Regex.Split(input, pattern)
                Dim concat() As String
                With ListView1.Items.Add("null")
                    'for index
                    .SubItems.AddRange(New String() {result(2).ToString})
                    'for status
                    .SubItems.AddRange(New String() {result(4).ToString})
                    'for number
                    Dim my_string, position As String
                    my_string = result(6)
                    position = my_string.Length - 2
                    my_string = my_string.Remove(position, 2)
                    .SubItems.Add(my_string)
                    'for date and time
                    concat = New String() {result(8)}
                    .SubItems.AddRange(concat)
                    'for message
                    Dim lineoftexts As String
                    Dim arytextfiles() As String
                    lineoftexts = arytextfile(i)
                    arytextfiles = Split(lineoftexts, "+32", , CompareMethod.Text)
                    .SubItems.Add(arytextfiles(1))
                End With
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Private Sub btnReadsms_Click(sender As System.Object, e As System.EventArgs) Handles btnReadsms.Click Try With SerialPort1 .Write("AT" & vbCrLf) Threading.Thread.Sleep(1000) .Write("AT+CMGF=1" & vbCrLf) Threading.Thread.Sleep(1000) .Write("AT+CPMS=""SM""" & vbCrLf) Threading.Thread.Sleep(1000) .Write("AT+CMGL=""ALL""" & vbCrLf) Threading.Thread.Sleep(1000) 'MsgBox(rcvdata.ToString) readmsg() End With Catch ex As Exception MsgBox(ex.Message) End Try End Sub 'Save Received message to ListView1 Private Sub readmsg() Try Dim lineoftext As String Dim i As Integer Dim arytextfile() As String lineoftext = rcvdata.ToString arytextfile = Split(lineoftext, "+CMGL", , CompareMethod.Text) For i = 1 To UBound(arytextfile) Dim input As String = arytextfile(i) Dim pattern As String = "(:)|(,"")|("","")" result = Regex.Split(input, pattern) Dim concat() As String With ListView1.Items.Add("null") 'for index .SubItems.AddRange(New String() {result(2).ToString}) 'for status .SubItems.AddRange(New String() {result(4).ToString}) 'for number Dim my_string, position As String my_string = result(6) position = my_string.Length - 2 my_string = my_string.Remove(position, 2) .SubItems.Add(my_string) 'for date and time concat = New String() {result(8)} .SubItems.AddRange(concat) 'for message Dim lineoftexts As String Dim arytextfiles() As String lineoftexts = arytextfile(i) arytextfiles = Split(lineoftexts, "+32", , CompareMethod.Text) .SubItems.Add(arytextfiles(1)) End With Next Catch ex As Exception MsgBox(ex.Message) End Try End Sub

  

希望有人可以帮助,请......:/

0 个答案:

没有答案