title: How to save Received Messages From Gsm Modem to your Mysql Database?
body: <p>I am trying to save received sms from my modem to MySQL database, and I am using MySQL Workbench for my database.</p>
  
  <p>Here's my functionalities:<br>
  1. Auto detect modem<br>
  2. Connect modem<br>
  3. Send Messages
  <br>4. Read Messages<br>
  5. Handles (SerialPort Data received)</p>
  
  <p>Here's the Code:<br><b>1. Auto Detect Modem</b>
  <code></p>
  
  <pre><code>'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 &amp; (queryObj("AttachedTo") &amp; " - " &amp; queryObj("Description") &amp; "***")
                  End If
              Next
          Catch err As ManagementException
              MessageBox.Show("An error occurred while querying for WMI data: " &amp; 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
  </code></pre>
  
  <p></code></p>
  
  <p><b>2. Connect/Disconnect Modem</b>
  <code></p>
  
  <pre><code>    '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 " &amp; Label1.Text &amp; " 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
  </code></pre>
  
  <p></code></p>
  
  <p><b>3. Send Messages</b>
  <code></p>
  
  <pre><code>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('" &amp; txtNumber.Text &amp; "', '" &amp; txtMessage.Text &amp; "')"
              sqlcmd = New MySqlCommand(query, conn)
              sqlreader = sqlcmd.ExecuteReader
  
              With SerialPort1
                  .Write("at" &amp; vbCrLf)
                  Threading.Thread.Sleep(200)
                  .Write("at+cmgf=1" &amp; vbCrLf)
                  Threading.Thread.Sleep(200)
                  .Write("at+cmgs=" &amp; Chr(34) &amp; txtNumber.Text &amp; Chr(34) &amp; vbCrLf)
                  .Write(txtMessage.Text &amp; Chr(26))
                  Threading.Thread.Sleep(200)
  
              End With
              If rcvdata.ToString.Contains("&gt;") 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
  </code></pre>
  
  <p></code></p>
  
  <p><b>4. Read Messages</b>
  <code></p>
  
  <pre><code>    Private Sub btnReadsms_Click(sender As System.Object, e As System.EventArgs) Handles btnReadsms.Click
          Try
              With SerialPort1
                  .Write("AT" &amp; vbCrLf)
                  Threading.Thread.Sleep(1000)
                  .Write("AT+CMGF=1" &amp; vbCrLf)
                  Threading.Thread.Sleep(1000)
                  .Write("AT+CPMS=""SM""" &amp; vbCrLf)
                  Threading.Thread.Sleep(1000)
                  .Write("AT+CMGL=""ALL""" &amp; 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
  </code></pre>
  
  <p></code></p>
  
  <p><b>5. Handles (SerialPort Data received)</b>
  <code></p>
  
  <pre><code>Private Sub SerialPort1_datareceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
      Dim datain As String = ""
      Dim numbytes As Integer = SerialPort1.BytesToRead
      For i As Integer = 1 To numbytes
          datain &amp;= Chr(SerialPort1.ReadChar)
      Next
      test(datain)
  End Sub
  
  Private Sub test(ByVal indata As String)
      rcvdata &amp;= indata
  End Sub
  </code></pre>
  
  <p></code></p>
  
  <blockquote>
    <p>Hope someone can Help, please... :/</p>
  </blockquote>