Threads - "Conversion from string "" to type 'Double' is not valid."

Gopher2011

Well-known member
Joined
Mar 3, 2011
Messages
111
Location
South England
Programming Experience
10+
Threads - "Conversion from string "" to type 'Double' is not valid."

I have a application that writes and reads from a serial port.
The com-port is opened elsewhere in the early stages of starting / running frm_Main and is not shown.

I have a button1 on my form and when I click it, the module 'RTUValues_Get_IOData' executes perfectly ok, with no errors. No Problems.

VB.NET:
Imports System
Imports System.IO.Ports
Imports System.Threading
Imports System.Threading.Thread

[COLOR="blue"]Public Class frm_Main[/COLOR]
   [...inits and vars not shown...]

   [COLOR="blue"]Private Sub Button1_Click[/COLOR](ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Debug.WriteLine("    ...Get the Eprom Data - Works fine called from here")
      RTUValues_Get_IOData()
   [COLOR="blue"]End Sub[/COLOR]

   [COLOR="blue"]Private Sub Button2_Click[/COLOR](ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
      Debug.WriteLine("    ...Get the Eprom Data - Causes an Error")
      bgwComms.RunWorkerAsync()
   [COLOR="blue"]End Sub[/COLOR]

   [COLOR="blue"]Private Sub bgwComms_DoWork[/COLOR](ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwComms.DoWork
      RTUValues_Get_IOData()
   [COLOR="blue"]End Sub[/COLOR]

[COLOR="blue"]End Class[/COLOR]

The module code that is called is shown below.

VB.NET:
[COLOR="blue"]Module Get_Data[/COLOR]
   [...inits and vars not shown...]

   [COLOR="blue"]Public Sub RTUValues_Get_IOData[/COLOR]()
        If RTU_Write(150, "val") <> 1 Then Debug.WriteLine("    Failed to send")
        If RTU_Read(150, vbCr) <> "a" Then Debug.WriteLine("    Failed to relpy")
        [COLOR="red"]Temp_Val = RTU_Read(150, ", ")[/COLOR] : frm_Main.txtDataReceived.AppendText(" : ")
   [COLOR="blue"]End Sub[/COLOR]
[COLOR="blue"]End Module[/COLOR]

However when I call it via a thread [button2], I get the following exception error and it hangs, at the part shown above in RED

"Conversion from string "" to type 'Double' is not valid."

Does anyone have any advice? Thanks in advance. :(
 
At some point the RTU_Read function returns an empty string, which as the error message says can not be converted to a Double (number). I don't know what is the problem, if the function returns a String, or that you declared the variable as Double.
 
Ok, that makes sense. However the problem to my novice mind is the thread calling the module. Is the thread able to access all my form objects ok? Could the thread be out of step with the port read or write? Does the main program pause when a thread is called?

As I mentioned the code works fine from a button, but when called from a tread via a button, it hangs.
 
frm_Main.txtDataReceived.AppendText(" : ")
This part I quoted here you can not do from a secondary thread, the reason it doesn't throw an exception (when you don't have conversion problems) is that you use the default form instance and it returns a new instance for each thread. For a BackgroundWorker you can simply use the progress/complete event handlers to interact with UI thread, otherwise Control.Invoke is required to make calls to UI thread.
This is not related to the problem of converting an empty string to a numeric value though.
 
Back
Top