VB.NET SerialPort....
-
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 -
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, AaronThis 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)
TrytxtResults.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 :)
-
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)
TrytxtResults.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 :)
Thanks for your help.... but it didn't work :( I ended up with
#Region "SMS"
Public Sub openModem()
Try
Modem.Open()
Catch ex As ExceptionEnd 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
-
Thanks for your help.... but it didn't work :( I ended up with
#Region "SMS"
Public Sub openModem()
Try
Modem.Open()
Catch ex As ExceptionEnd 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
-
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
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 -
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