Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. VB.NET SerialPort....

VB.NET SerialPort....

Scheduled Pinned Locked Moved Visual Basic
csharphelpquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mr J 0
    wrote on last edited by
    #1

    Hi... I'm working on a project for a site I'm doing which gets SMS' from a GSM Modem, so far it's going okay... But I've hit a slight problem. I've been though pretty much every page on Google and still no luck. #Region "SMS" Public Sub openModem() Try Modem.Open() Catch ex As Exception End Try Thread.Sleep(7000) ModemWrite(" ", False) ''Seems to wake up modem MsgBox(ModemWrite("AT+COPS?", True)) End Sub Public Function ModemWrite(ByVal WriteString As String, ByVal ExpectReturn As Boolean) As String Dim ReturnString As String = "" Modem.DiscardOutBuffer() Modem.DiscardInBuffer() Try Modem.Write(WriteString) Application.DoEvents() Catch ex As Exception End Try Thread.Sleep(1000) If ExpectReturn = True Then If Modem.BytesToRead = True Then ReturnString = Modem.ReadExisting End If End If Return ReturnString End Function #End Region I can talk to the modem though HyperTerminal, and the code above seems to be sending the commands.... But I don't seem to get any thing back other than a space. I've tried using the event handler... and still nothing... If anyone has any ideas, I'd love to hear them. Thanks, Aaron

    R 1 Reply Last reply
    0
    • M Mr J 0

      Hi... I'm working on a project for a site I'm doing which gets SMS' from a GSM Modem, so far it's going okay... But I've hit a slight problem. I've been though pretty much every page on Google and still no luck. #Region "SMS" Public Sub openModem() Try Modem.Open() Catch ex As Exception End Try Thread.Sleep(7000) ModemWrite(" ", False) ''Seems to wake up modem MsgBox(ModemWrite("AT+COPS?", True)) End Sub Public Function ModemWrite(ByVal WriteString As String, ByVal ExpectReturn As Boolean) As String Dim ReturnString As String = "" Modem.DiscardOutBuffer() Modem.DiscardInBuffer() Try Modem.Write(WriteString) Application.DoEvents() Catch ex As Exception End Try Thread.Sleep(1000) If ExpectReturn = True Then If Modem.BytesToRead = True Then ReturnString = Modem.ReadExisting End If End If Return ReturnString End Function #End Region I can talk to the modem though HyperTerminal, and the code above seems to be sending the commands.... But I don't seem to get any thing back other than a space. I've tried using the event handler... and still nothing... If anyone has any ideas, I'd love to hear them. Thanks, Aaron

      R Offline
      R Offline
      rudemusik
      wrote on last edited by
      #2

      This is what I did to recieve data to my app.

      Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
      '' Threading.Thread.Sleep(500)
      Try

              txtResults.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
      
          Catch ex As Exception
              MsgBox(ex.ToString)
          End Try
      
      End Sub
      

      Public Delegate Sub myDelegate()

      Public Sub updateTextBox()
      With txtResults
      .Font = New Font("Arial", 10.0, FontStyle.Bold)

             SerialPort1.ReadExisting())
              .AppendText(SerialPort1.ReadExisting())
      
              .ScrollToCaret()
      
      
          End With
          'THIS WILL ONLY WORK WHEN CONNECTED  SerialPort1.Close()
      End Sub
      

      Hope this helps! rudy :)

      M 1 Reply Last reply
      0
      • R rudemusik

        This is what I did to recieve data to my app.

        Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        '' Threading.Thread.Sleep(500)
        Try

                txtResults.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
        
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        
        End Sub
        

        Public Delegate Sub myDelegate()

        Public Sub updateTextBox()
        With txtResults
        .Font = New Font("Arial", 10.0, FontStyle.Bold)

               SerialPort1.ReadExisting())
                .AppendText(SerialPort1.ReadExisting())
        
                .ScrollToCaret()
        
        
            End With
            'THIS WILL ONLY WORK WHEN CONNECTED  SerialPort1.Close()
        End Sub
        

        Hope this helps! rudy :)

        M Offline
        M Offline
        Mr J 0
        wrote on last edited by
        #3

        Thanks for your help.... but it didn't work :( I ended up with

        #Region "SMS"
        Public Sub openModem()
        Try
        Modem.Open()
        Catch ex As Exception

            End Try
        
            Thread.Sleep(7000)
        
            ModemWrite(" ", False)
            MsgBox(ModemWrite("AT+COPS?", True))
        End Sub
        Public Function ModemWrite(ByVal WriteString As String, ByVal ExpectReturn As Boolean) As String
            Dim ReturnString As String = ""
            Modem.DiscardOutBuffer()
            Modem.DiscardInBuffer()
        
        
            Try
                Modem.Write(WriteString)
                Application.DoEvents()
            Catch ex As Exception
        
            End Try
            Thread.Sleep(1000)
            If ExpectReturn = True Then
                While ReturnStringTXT.Text = ""
                    Application.DoEvents()
                End While
                While Not ReturnStringTXT.Text = ""
                    ReturnString = ReturnStringTXT.Text
                End While
        
            End If
        
            Return ReturnString
        End Function
        Private Sub ModemDataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Modem.DataReceived
            Try
                ReturnStringTXT.Invoke(New smsDelegate(AddressOf updateTextBox), New Object() {})
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        
        End Sub
        Public Delegate Sub smsDelegate()
        Public Sub updateTextBox()
            ReturnStringTXT.Text = Modem.ReadExisting()
        End Sub
        

        #End Region

        Popped up with an exception... :( Am I doing something wrong? Thanks, Aaron

        R 1 Reply Last reply
        0
        • M Mr J 0

          Thanks for your help.... but it didn't work :( I ended up with

          #Region "SMS"
          Public Sub openModem()
          Try
          Modem.Open()
          Catch ex As Exception

              End Try
          
              Thread.Sleep(7000)
          
              ModemWrite(" ", False)
              MsgBox(ModemWrite("AT+COPS?", True))
          End Sub
          Public Function ModemWrite(ByVal WriteString As String, ByVal ExpectReturn As Boolean) As String
              Dim ReturnString As String = ""
              Modem.DiscardOutBuffer()
              Modem.DiscardInBuffer()
          
          
              Try
                  Modem.Write(WriteString)
                  Application.DoEvents()
              Catch ex As Exception
          
              End Try
              Thread.Sleep(1000)
              If ExpectReturn = True Then
                  While ReturnStringTXT.Text = ""
                      Application.DoEvents()
                  End While
                  While Not ReturnStringTXT.Text = ""
                      ReturnString = ReturnStringTXT.Text
                  End While
          
              End If
          
              Return ReturnString
          End Function
          Private Sub ModemDataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Modem.DataReceived
              Try
                  ReturnStringTXT.Invoke(New smsDelegate(AddressOf updateTextBox), New Object() {})
              Catch ex As Exception
                  MsgBox(ex.ToString)
              End Try
          
          End Sub
          Public Delegate Sub smsDelegate()
          Public Sub updateTextBox()
              ReturnStringTXT.Text = Modem.ReadExisting()
          End Sub
          

          #End Region

          Popped up with an exception... :( Am I doing something wrong? Thanks, Aaron

          R Offline
          R Offline
          rudemusik
          wrote on last edited by
          #4

          What's the exception? I'm ussing the serial port control, you may have to modify it. This came from a program that I used to replace Hyper terminal. If you want, I can supply the whole code. It might give you an idea what to do. let me know. Rudy

          M 1 Reply Last reply
          0
          • R rudemusik

            What's the exception? I'm ussing the serial port control, you may have to modify it. This came from a program that I used to replace Hyper terminal. If you want, I can supply the whole code. It might give you an idea what to do. let me know. Rudy

            M Offline
            M Offline
            Mr J 0
            wrote on last edited by
            #5

            Here's the exception: System.InvalidOperationException: Invoke or BeginInvoke cannot be called on a control until the window handle has been created. at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Objects[] args, Boolean synchronous) at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args) at Application.GUI.ModemDataReceived(Objects sender, SerialDataReceivedExentArgs e) in C:\Application\Server Any ideas? Thanks, Aaron

            M 1 Reply Last reply
            0
            • M Mr J 0

              Here's the exception: System.InvalidOperationException: Invoke or BeginInvoke cannot be called on a control until the window handle has been created. at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Objects[] args, Boolean synchronous) at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args) at Application.GUI.ModemDataReceived(Objects sender, SerialDataReceivedExentArgs e) in C:\Application\Server Any ideas? Thanks, Aaron

              M Offline
              M Offline
              Mr J 0
              wrote on last edited by
              #6

              Woop.... Fixed :) All it was, was not finishing the lines with vbCrLf.... 9 hours wasted :( Thanks anyway, Aaron

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups