Link to home
Start Free TrialLog in
Avatar of judico
judico

asked on

Cannot Concatenate 14 Incoming Characters into One Compact 14-Character String

The problem consists in the following. We are trying to communicate with an external device via a serial (rs232) port. At that 14 separate characters constituting a pachet of data are being received from the device one character at a time. In order to use this packet of 14-character data further in the program the separate characters sent individually have to be concatenated in one single compact 14-character string. So far, however, we have not been able to find a way to have the 14 incoming characters from the device in one intact string, so that we can use it later

in the program.

Here is an example of what we want to do:

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
Public Class Form1

    Inherits System.Windows.Forms.Form
    Dim ThreadedTimer As System.Threading.Timer
    Dim GlobalString as String

<Windows Form Designer generated code>

Public Sub OpenPort()
        RS232Comms1.CloseComms()
        RS232Comms1.OpenComms()
        RS232Comms1.WriteBlock(&H12)
End Sub


Public Sub rS232Comms1_DataRxEvent(ByVal sender As System.Object, ByVal e As

RS232.RxEventArgs) Handles RS232Comms1.DataRxEvent

       Dim buffer() As Byte = e.data
       Dim newString As String =    
System.Text.Encoding.ASCII.GetString(buffer)
       If newString = "." Then newString = ","

       RS232Comms1.SetDTR()

      GlobalString &= newString
       TextBox1.Text = GlobalString

       End Sub


Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
        RS232Comms1.CloseComms()
        Me.Refresh()
        OpenPort()
    End Sub
End Class
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .

The whole problem stems from the fact that every time a call to the device
is made the component rx handler executes 14 times by itself without a
For-Next loop and at every one of these executions the new value of the
newString erases the previous value of the newString. The value of
GlobalString which is supposed to be in the end the concatenated string
containing all 14 characters also is erased at every execution of the rx
handler. As a result, one sees on the screen (TextBox1.Text = GlobalString)
a brief appearance and disappearance of the incoming characters so that
finally GlobalString contains only the last of these 14 characters.

Where could be the error? How can we get an intact GlobalString containing
the 14 characters after each call to the device?

Best regards,

Judi
Avatar of bramsquad
bramsquad
Flag of United States of America image

this almost seems to simple, but replace

TextBox1.Text = GlobalString

with

TextBox1.Text += GlobalString

~b
Avatar of Joe_Griffith
Joe_Griffith

It's a little hard to tell exactly what the scope of various variables is.  You obviously need somewhere to store the string that isn't being reinitialized.  One place would be the text box itself.  

Try changing this line:

TextBox1.Text = GlobalString

to

TextBox1.Text &= GlobalString
This line looks odd to me: Dim GlobalString as String

I didn't even know you could do that but apparently you can.  I would try changing it to: Private Global String As String and leave the rest of the code unchanged.
or, you could initialize the string in your function and make it static

Static GlobalString  As String

~b
Another thing you could try is to define "GlobalString" as a Static inside your function like this:

Static GlobalString As String

Of course it won't be global anymore.
Avatar of judico

ASKER

Unfortunately, none of this works:

" this almost seems to simple, but replace
TextBox1.Text = GlobalString
with
TextBox1.Text += GlobalString"

Neither &=, nor += help in obtaining a concatenated 14-character string. For some reason, without any loop in sight, the subroutine of the rx handle executes 14 times (seen from the debugger) and every time it executes it erases the previous values of the variables. There is no way these previous values to be stored anywhere. One way we tried is to use data stuctures. Didn't do any good. The only way an intact 14-character concatenated string could be stored was to write the incoming data in a text file and then retreive it from there. However, in this way the call to the device could not be executed repeatedly -- the data from the device could be obtained only once and then the program hangs. It is unbelievable that such an elementary thing as concatenating characters in a string should be a problem.

" It's a little hard to tell exactly what the scope of various variables is.  You obviously need somewhere to store the string that isn't being reinitialized.  One place would be the text box itself.  
Try changing this line:
TextBox1.Text = GlobalString
to
TextBox1.Text &= GlobalString"

This si exactly the problem. However, TextBox1.Text &= GlobalString" doesn't help because on the screen what appears to be a string can be obtained anyway, say, by TextBox1.AppendText(GlobalString) but this screen image in fact is not a concatenated string which you can work with later.

" This line looks odd to me: Dim GlobalString as String
I didn't even know you could do that but apparently you can.  I would try changing it to: Private Global String As String and leave the rest of the code unchanged."

Doesn't seem to accept a declaration such as: Private Global String As String.

" or, you could initialize the string in your function and make it static
Static GlobalString  As String"

With Dim GlobalString as String at least the program gives the appearance that it works -- you can see on the screen the characters placed one next to the other (but the actual 14-character string not being formed). With Static GlobalString as String the program hangs and nothing appers on the screen.





this is really wierd.

lets try the registry

try these lines

'this will get it in the registry, if it doesnt exist (ie first time) it will return ""
temp = GetSetting("your_program_name", "your_form_name", "GlobalString", "")
savesetting("your_program_name", "your_form_name", "GlobalString", temp & GlobalString)

your 14 character string should be in the variable 'temp' upon last execution of the loop

~b
Avatar of judico

ASKER

"'this will get it in the registry, if it doesnt exist (ie first time) it will return ""
temp = GetSetting("your_program_name", "your_form_name", "GlobalString", "")
savesetting("your_program_name", "your_form_name", "GlobalString", temp & GlobalString)"

If you do that, TextBox1.Text = temp.Substring(4, 5) gives a result only once and then the program hangs (I am extracting a Substring to verify that temp is a genuine 14-character string).
im out of ideas :(
Avatar of judico

ASKER

OK ... thanks anyway ...
You say that the event handler gets called 14x and you are expecting 14 chars.  So it sounds like your event handler gets called once for each character.  How many bytes are in the buffer array each call?  Can you put a breakpoint there and query buffer()?
Just had an idea...try the StringBuilder.

You may need to add an Imports System.Text line at the top of your program OUTSIDE the class declaration.

Under Globalstring , declare:

Public sb As New Stringbuilder()


Public Sub rS232Comms1_DataRxEvent(ByVal sender As System.Object, ByVal e As RS232.RxEventArgs) Handles RS232Comms1.DataRxEvent

       Dim buffer() As Byte = e.data
       Dim newString As String =    System.Text.Encoding.ASCII.GetString(buffer)
       If newString = "." Then newString = ","

       RS232Comms1.SetDTR()

       sb.Add(newString)
       TextBox1.Text = sb.ToString()

End Sub
Avatar of judico

ASKER

" You say that the event handler gets called 14x and you are expecting 14 chars.  So it sounds like your event handler gets called once for each character.  How many bytes are in the buffer array each call?  Can you put a breakpoint there and query buffer()?"

That's correct, somehow the event handler gets called once for each character although there is no loop in sight. As far as the bytes, I guess, the buffer contains one byte at each call. It should be checked, though. Where is the breakpoint to be placed and how exactly is this query buffer() to be written?
If you are using Visual Studio, click in the left margin next to the line where your buffer is declared.

Then run the app in Debug mode.

When it stops, look at your Auto or Locals window in the lower left corner of your screen.  Hit F8 to execute that line and watch what comes into your variable "buffer()".
Avatar of judico

ASKER

Upon the first execution, the value of the buffer is {Nothing}. Upon hitting F11 (hitting F8 causes no activity) the value is {length=1} for every call.
F11/F8...depends on how you have VS configured.

Did you try the StringBuilder?
Avatar of judico

ASKER

" Just had an idea...try the StringBuilder.
You may need to add an Imports System.Text line at the top of your program OUTSIDE the class declaration.
Under Globalstring , declare:
Public sb As New Stringbuilder()
Public Sub rS232Comms1_DataRxEvent(ByVal sender As System.Object, ByVal e As RS232.RxEventArgs) Handles RS232Comms1.DataRxEvent
       Dim buffer() As Byte = e.data
       Dim newString As String =    System.Text.Encoding.ASCII.GetString(buffer)
       If newString = "." Then newString = ","
       RS232Comms1.SetDTR()
       sb.Add(newString)
       TextBox1.Text = sb.ToString()
End Sub"

"Did you try the StringBuilder?"

I get the following error message: 'add' is not a member of 'System.Text.StringBuilder'.
Avatar of judico

ASKER

I like the idea with the registry:

'this will get it in the registry, if it doesnt exist (ie first time) it will return ""
temp = GetSetting("your_program_name", "your_form_name", "GlobalString", "")
savesetting("your_program_name", "your_form_name", "GlobalString", temp & GlobalString)

What remains to be understood now is why on a subsequrent call to the device the program hangs.
Avatar of judico

ASKER

In fact, it seems more is to be understood about the example with the registry. How is the data placed there in the first place? When I erase the data there above lines don't fill the register back with new data.
My mistake.  It should be the "Append" method...

Just had an idea...try the StringBuilder.
You may need to add an Imports System.Text line at the top of your program OUTSIDE the class declaration.
Under Globalstring , declare:
Public sb As New Stringbuilder()
Public Sub rS232Comms1_DataRxEvent(ByVal sender As System.Object, ByVal e As RS232.RxEventArgs) Handles RS232Comms1.DataRxEvent
       Dim buffer() As Byte = e.data
       Dim newString As String =    System.Text.Encoding.ASCII.GetString(buffer)
       If newString = "." Then newString = ","
       RS232Comms1.SetDTR()
       sb.Append(newString)
       TextBox1.Text = sb.ToString()
End Sub"

Try that...

Avatar of judico

ASKER

Nope ... I tried that yesterday and again now after your message ... TextBox1.Text = sb.ToString() doesn't show any activity while ListBox1.Items.Add(sb.ToString()) shows a blank box but the sliding bar shortens with every incoming set of data.
Hmm...I wonder if this has something to do with newString going out of scope each time this routine is called.  I am grasping at straws here, but try this:

Make newString a private class-level variable (like the StringBuilder object sb).

Then in your code try this:

Public sb As New Stringbuilder()
Private newString As String


Public Sub rS232Comms1_DataRxEvent(ByVal sender As System.Object, ByVal e As RS232.RxEventArgs) Handles RS232Comms1.DataRxEvent
       Dim buffer() As Byte = e.data
       newString =  System.Text.Encoding.ASCII.GetString(buffer)
       If newString = "." Then newString = ","
       RS232Comms1.SetDTR()
       sb.Append(newString)
       TextBox1.Text = sb.ToString()
End Sub

Avatar of judico

ASKER

Doesn't help either ... I can't believe that this should be such a problem ...
Me niether.  I'm stumped.
Can you post exactly what is in e.data each call?  If it is just an ASCII byte code, couldn't you just do this:

newString = Chr(e.data)
Avatar of judico

ASKER

The 14-character string is:

" OH  13.27mOhm"

The numbers change but the letters are always the same. Eaxh one of these ASCII characters comes in individually, erasing the previous one, after a call to the device is executed.

Putting newString = Chr(e.data) gives: Value of type '1-dimensional array of Byte' cannot be converted to 'Integer'.

Dim newString As String = Chr(e.data) gives: Value of type '1-dimensional array of Byte' cannot be converted to 'Integer'.


Avatar of judico

ASKER

As I mentioned before, the only way to create the above 14-character string as an intact string is to have it saved to a text file and then retrieve it from that text file. However, if you do that you can only have it after the first call to the device. The device hangs if you try to call it a second time.
You had mentioned that e.Data only contains 1 char each call.  So would this work:

newString = Chr(Ctype(e.data(0), Integer))
Avatar of judico

ASKER

" You had mentioned that e.Data only contains 1 char each call.  So would this work:
newString = Chr(Ctype(e.data(0), Integer))"

Same thing ... One character at a time ... each incoming character erasing the previous one ...
Ok...this one is driving me nuts.

Here's some test code that I just wrote that basically does the same thing, which works fine.  Got a form with a button & a textbox on it.

    Private newstring As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim chrs() As String = {"0", "1", "2", "3", "4", "5"}
        For ii As Integer = 0 To chrs.Length - 1

            Call addchar(Convert.ToByte(Asc(chrs(ii))))
            System.Threading.Thread.Sleep(500)
        Next
    End Sub

    Private Sub addchar(ByVal e As Byte)
        newstring = Chr(CType(e, Integer))
        TextBox1.Text &= newstring
        Me.Refresh()

    End Sub

The only difference I see is that your routine is being called as the result of an event.  My code is from a loop.  
Avatar of judico

ASKER

Yes, your example works well. This is what I'd expect to happen in the example I have. But it doesn't. Let's write your example in this way (with ListBox1.Items.Add instead of with TextBox1.Text and with GlobalString &= newstring ):

Private newstring As String
Dim GlobalString As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim chrs() As String = {"0", "1", "2", "3", "4", "5"}
        For ii As Integer = 0 To chrs.Length - 1
            Call addchar(Convert.ToByte(Asc(chrs(ii))))
            System.Threading.Thread.Sleep(500)
        Next
    End Sub

    Private Sub addchar(ByVal e As Byte)
        newstring = Chr(CType(e, Integer))
        GlobalString &= newstring
        ListBox1.Items.Add(GlobalString)
        Me.Refresh()
    End Sub

You can see in your example, after pressing the button, building of your 6-character string GlobalString in the ListBox1. This, however, doesn't happen in my  example for some reason. In my example you can only see each individual, unconcatenated character appearing in the ListBox1 if you replace  ListBox1.Items.Add(GlobalString) with  ListBox1.Items.Add(newString). This is the most you can do in my example.
"This, however, doesn't happen in my  example for some reason. In my example you can only see each individual, unconcatenated character appearing in the ListBox1 if you replace  ListBox1.Items.Add(GlobalString) with  ListBox1.Items.Add(newString). This is the most you can do in my example."

I am not sure I understand what you are saying here.  Are you saying that if you put newstring in your listbox.items.add you only see the individual characters...lke this:


0
1
2
3
4
5


When what you want is this:

0
01
012
0123
01234
012345

right?
Avatar of judico

ASKER

That's correct ...
Then that makes sense.  The listbox doesn't concatinate anything.  It adds whatever you pass it to the list.

So what you want is to put GlobalString in your add method for the LIstbox  ANd you should see what you desire.

I tried it on my system and is works fine.
Avatar of judico

ASKER

Can you show it in code?
Here's the code from my example that works fine.  Try this in a new project to see if you get the behavior you want:

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Timer1 As System.Timers.Timer
    Friend WithEvents UltraDataSource1 As Infragistics.Win.UltraWinDataSource.UltraDataSource
    Friend WithEvents ExcelExport As Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter
    Friend WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(272, 136)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(312, 80)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = "TextBox1"
        '
        'ListBox1
        '
        Me.ListBox1.ItemHeight = 16
        Me.ListBox1.Location = New System.Drawing.Point(496, 144)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(208, 84)
        Me.ListBox1.TabIndex = 2
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
        Me.ClientSize = New System.Drawing.Size(816, 480)
        Me.Controls.Add(Me.ListBox1)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Button1)
        Me.KeyPreview = True
        Me.Name = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private newstring, GlobalString As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim chrs() As String = {"0", "1", "2", "3", "4", "5"}
        For ii As Integer = 0 To chrs.Length - 1

            Call addchar(Convert.ToByte(Asc(chrs(ii))))
            System.Threading.Thread.Sleep(500)
        Next

    End Sub


    Private Sub addchar(ByVal e As Byte)
        newstring = Chr(CType(e, Integer))
        GlobalString &= newstring
        ListBox1.Items.Add(GlobalString)
        Me.Refresh()
    End Sub


End Class
Avatar of judico

ASKER

As I said, in your example  ListBox1.Items.Add(GlobalString) works fine. Not in mine, though ... for some reason ...
Ahh crap...delete the following lines from the code above:

    Friend WithEvents Timer1 As System.Timers.Timer
    Friend WithEvents UltraDataSource1 As Infragistics.Win.UltraWinDataSource.UltraDataSource
    Friend WithEvents ExcelExport As Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter
    Friend WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Avatar of judico

ASKER

Yes, this is exactly what i'm saying. In your example, the way you have written it in your last posting, it works. The same thing doesn't work in my example. That was the problem to begin with ...
Avatar of judico

ASKER

Your example shows what is expected to happen. It does in your case. In mine it doesn't for some weird reason ...
So your saying that on your PC, my code does not work???  That's wierd.
Avatar of judico

ASKER

Probably, the whole problem is how you generate the characters. The way characters are generated in you case and in my case differ ...
Avatar of judico

ASKER

On my PC your code does work. What doesn't work on my PC is my code.
Can  you take the class I posted above and put it into a new project and run it?  Then tell me if it works on your PC or not.
Can you post your entire listing for your form?
Avatar of judico

ASKER

Here it is:

Imports System.Text
Imports System.Threading

Public Class Form1

    Inherits System.Windows.Forms.Form
    Dim ThreadedTimer As System.Threading.Timer
   
   
    Dim GlobalString As String
    Public sb As New StringBuilder
    Private newString As String


#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        Timer1.Enabled = True

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents RS232Comms1 As RS232.RS232Comms
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.RS232Comms1 = New RS232.RS232Comms
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(0, 24)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.ReadOnly = True
        Me.TextBox1.Size = New System.Drawing.Size(224, 192)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = ""
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(16, 224)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(176, 32)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Button1"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(272, 248)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(16, 16)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Button2"
        '
        'RS232Comms1
        '
        Me.RS232Comms1.BaudRate = RS232.RS232Comms.eBAUDRATE.CBR_600
        Me.RS232Comms1.DataBits = RS232.RS232Comms.eDATABITS.DATA7
        Me.RS232Comms1.StopBit = RS232.RS232Comms.eSTOPBIT.TWOSTOPBITS
        '
        'Timer1
        '
        Me.Timer1.Enabled = True
        Me.Timer1.Interval = 2000
        '
        'ListBox1
        '
        Me.ListBox1.Location = New System.Drawing.Point(280, 32)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(184, 186)
        Me.ListBox1.TabIndex = 3
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(504, 266)
        Me.Controls.Add(Me.ListBox1)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

   

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenPort()
    End Sub





    Public Sub OpenPort()
        RS232Comms1.CloseComms()
        RS232Comms1.OpenComms()
        RS232Comms1.WriteBlock(&H12)
    End Sub





Public Sub rS232Comms1_DataRxEvent(ByVal sender As System.Object, ByVal e As RS232.RxEventArgs) Handles RS232Comms1.DataRxEvent

        Debug.WriteLine("hnhn <------")

        'Dim buffer() As Byte = e.data ' <-------------
        'Dim newString As String = System.Text.Encoding.ASCII.GetString(buffer) ' <------------
        'Dim newString As String = Chr(e.data)
        newString = Chr(CType(e.data(0), Integer))

        If newString = "." Then newString = ","

        RS232Comms1.SetDTR()

        GlobalString &= newString
           
        ListBox1.Items.Add(GlobalString)
     
End Sub





    '                              QUIT
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        RS232Comms1.CloseComms()
        TextBox1.Clear()
        TextBox1.AppendText("Good Bye:   ")
    End Sub




    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
       
        RS232Comms1.CloseComms()
        Me.Refresh()
        OpenPort()

    End Sub
End Class
Avatar of judico

ASKER

Yes, it does ... Your class works fine on my PC. Same as before ...
Avatar of judico

ASKER

It's really unbelievable that the same principle implemented in your class which works on my PC doen't work with my code. I guess, the reason stems from the difference in which the individual characters are generated. In your class the individual characters are generated within a subroutine in a For-Next loop. In my code there is no For-Next loop and the individual characters are obtained from an external device which somehow takes care to send them one by one. During this process (unlike the way it goes in your example) something happens and every incoming character deletes the previous.
I understand what you are saying, but logically, that doesn't make sense.  Once that char gets into your sub, regardless of where it came from, you should be able to capture it & do what you want with it.

What is the purpose of the timer?  I wonder if closing & reopening the comm port is doing something screwy with the events?
Avatar of judico

ASKER

Of course ... the way my code behaved defies all logic ... it must be something inherent in the language, I guess ...

It very well may be that closing and opening of the port does something strange to the events but I do need to open and close the port. Opening and closing the port is not supposed to affect the way variables behave. It appears that VB.NET is not the most convenient language for RS232 communications.
What reference do I need to add to use the RS232 class?
Avatar of judico

ASKER

You need to add a rs232.dll module to your Tools ...
OK...Check out this code at http://www.freevbcode.com/ShowCode.Asp?ID=4666

It shows how to use the Comm port to read data.  I noticed that the method they use is different from yours, namely, they open the port, then read the chars off the port one @ a time until there are no more chars.  Then they close the port.

Avatar of judico

ASKER

I know this example ... doesn't work ... The only proper communication so far to get any data from the rs232 port whatsoeveris through installind the said rs232.dll in the Toolbox. None of the examples I could find on the net did any good in this respect ... However, you know already what problem appears with the rs232.dll, despite the fact that the individual characters are properly obtained from the device ...
Avatar of judico

ASKER

See, I would like to read the characters one at a time ... turns out it is impossible ... Once you open the port the device does this job for you -- it sends the characters one by one as if from a loop, although there is no loop in sight in the code ...
Avatar of judico

ASKER

This communication is done so easy in QBASIC with only four lines of code:

Open “COM1: 2400, N, 7, 2, RS, CS, DS, CD” FOR RANDOM AS #2
a$ = “D”
PRINT #2, a$
in$ = INPUT$(14, #2)

The whole string " OH  13.47kOhm" is obtained at once in the string in$ at each call to the device. Who would ever expect that VB.NET will incur such problems?

I am stumped.  The way I have interfaced to COM ports in the past (haven't done it yet with .NET) is to open the port, loop pulling chars out of the buffer and then close the port when no more chars are in the buffer, OR I have the data I need.

I'm all out of ideas.
1) dont open and close the port, leave it open
2) why is this public ?

Dim GlobalString As String

private GlobalString as string

just to make sure its not being accessed elsewhere ...

3)is this ALL of your code ? it is obviously missing some code please put that code up as well I bet that the string is getting stepped on elsewhere.
Avatar of judico

ASKER

This is the entire code:


Imports System.Text
Imports System.Threading
Public Class Form1

    Inherits System.Windows.Forms.Form
    Dim ThreadedTimer As System.Threading.Timer
    Dim temp As String
    Private GlobalString As String
    Public sb As New StringBuilder
    Private newString As String

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
       
        Timer1.Enabled = True

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents RS232Comms1 As RS232.RS232Comms
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Button2 = New System.Windows.Forms.Button
        Me.RS232Comms1 = New RS232.RS232Comms
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(16, 32)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.ReadOnly = True
        Me.TextBox1.Size = New System.Drawing.Size(224, 184)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = ""
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(272, 248)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(16, 16)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Button2"
        '
        'RS232Comms1
        '
        Me.RS232Comms1.BaudRate = RS232.RS232Comms.eBAUDRATE.CBR_600
        Me.RS232Comms1.DataBits = RS232.RS232Comms.eDATABITS.DATA7
        Me.RS232Comms1.StopBit = RS232.RS232Comms.eSTOPBIT.TWOSTOPBITS
        '
        'Timer1
        '
        Me.Timer1.Enabled = True
        Me.Timer1.Interval = 2000
        '
        'ListBox1
        '
        Me.ListBox1.Location = New System.Drawing.Point(280, 32)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(184, 186)
        Me.ListBox1.TabIndex = 3
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(504, 266)
        Me.Controls.Add(Me.ListBox1)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Sub OpenPort()
        RS232Comms1.OpenComms()
        RS232Comms1.WriteBlock(&H12)
    End Sub


    Public Sub rS232Comms1_DataRxEvent(ByVal sender As System.Object, ByVal e As RS232.RxEventArgs) Handles RS232Comms1.DataRxEvent

        Debug.WriteLine("hnhn <------")

        Dim ii As Integer
        For ii = 1 To 13
            newString = Chr(CType(e.data(0), Integer))
            If newString = "." Then newString = ","
            RS232Comms1.SetDTR()
        Next

        GlobalString &= newString

        TextBox1.Text = newString
        ListBox1.Items.Add(newString)
    End Sub


    '                              QUIT
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        RS232Comms1.CloseComms()
        TextBox1.Clear()
        TextBox1.AppendText("Good Bye:   ")
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        RS232Comms1.CloseComms()
        Me.Refresh()
        OpenPort()
    End Sub
End Class


As you can see, at your suggestion, I don't close the port and leave it open. Also, I've changed 'Dim GlobalString As String' to 'private GlobalString as String'. These changes, however, don't seem to contribute much to the solution of the problem
1) why are you looping from 1 to 13 without checking the size of your data

2) TextBox1.Text = newString ?

        For ii = 1 To 13
            newString = Chr(CType(e.data(0), Integer))
            If newString = "." Then newString = ","
            RS232Comms1.SetDTR()
        Next

this will set 1 character 13 times ...

for i = 0 to e.data.length -1
    newstring &=  Chr(CType(e.data(i), Integer))
next

GlobalString &= newstring
Avatar of judico

ASKER

I'm sorry, the loop

 For ii = 1 To 13
            newString = Chr(CType(e.data(0), Integer))
            If newString = "." Then newString = ","
            RS232Comms1.SetDTR()
Next

doesn't exist in the code I use and discuss here. The loop was suggested by the author of the rs232comms1.dll which I later removed. Some time passed since I discuseed that code and I just didn't notice the loop when I posted the code above.

The curious thing, as I've explained in several postings above, is that the program executes a loop without in fact any loop in sight. The code is just:

 newString = Chr(CType(e.data(0), Integer))
 If newString = "." Then newString = ","
 RS232Comms1.SetDTR()

Furthermore, despite the loop executing out of the blue, the characters that are received as a result of the execution of the said strange loop, cannot be concatenated in one compact 14-character string, no matter what method one tries to apply.

If you take a look at the previous posts you will see a model which ptakja suggested whereby symbols are sent one after another from within the program and the concatenation takes place as expected:

0
01
012
0123
01234
012345

Not with the "loop" invoked when the characters are received via the serial port, though. When characters are received through the serial port the outcome is different, namely

0
1
2
3
4
5

and , as seen, no concatenation occurs. The code you suggested

for i = 0 to e.data.length -1
    newstring &=  Chr(CType(e.data(i), Integer))
next

GlobalString &= newstring

also doesn't help in this respect.
put your FULL current code up.

btw: if you like I can write some code using the the code I mentioned earlier for serial access.
Avatar of judico

ASKER

The current code is same as the above one except for the loop. If you want I can post it again. As far as you writing code for serial access, I will be more than happy to try it.
its 1 am here I will do it first thing.
Avatar of judico

ASKER

OK, thanks. Have a Good Night ...
ok this code uses http://www.codeproject.com/dotnet/DotNetComPorts.asp ... all you have to do is download this build it (it is written in C#) and add a reference to your project ... note that I am polling here, in a real application you would want to do that polling in another thread and raise an event to your UI (I can show you how to do that if you need). I would also implement timeouts if you want synchronous behaviors.

Imports SerialPorts
Imports System.Threading
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents Button4 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.Button3 = New System.Windows.Forms.Button
        Me.Button4 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(40, 40)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Open"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(192, 56)
        Me.Button2.Name = "Button2"
        Me.Button2.TabIndex = 1
        Me.Button2.Text = "Close"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(88, 160)
        Me.Button3.Name = "Button3"
        Me.Button3.TabIndex = 2
        Me.Button3.Text = "SendData"
        '
        'Button4
        '
        Me.Button4.Location = New System.Drawing.Point(216, 168)
        Me.Button4.Name = "Button4"
        Me.Button4.TabIndex = 3
        Me.Button4.Text = "Receive"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.Button4)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Port As SerialPort
    Private Func As SerialPorts.WithEvents
    Private Const PollTime As Integer = 100

    Private Sub OpenPort(ByVal _PortIndex As Integer)
        Dim cnfg As New SerialCnfg(_PortIndex)
        cnfg.BaudRate = SerialPorts.LineSpeed.Baud_9600
        Port = New SerialPort(Func)
        Dim ok As Boolean = Port.Open(_PortIndex)
        If Not ok Then
            MessageBox.Show("Unable to open port")
        End If
    End Sub

    Private Sub SendData(ByVal _Message As Byte())
        Port.Send(_Message)
    End Sub

    Private Function ReceiveData(ByVal ExpectedBytes As Integer) As Byte()
        Dim i As Integer
        Dim Ret(ExpectedBytes) As Byte
        Dim tmpBuffer() As Byte
        Dim cnt As Integer = 0
        While (cnt < ExpectedBytes)
            Dim nBytes As Integer = Convert.ToInt32(Port.Recv(tmpBuffer))
            If nBytes > 0 Then
                For i = 0 To tmpBuffer.Length - 1
                    Ret(cnt) = tmpBuffer(i)
                Next
            Else
                Thread.Sleep(Me.PollTime)
            End If
        End While
        Return Ret
    End Function

    Private Sub ClosePort()
        Port.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.OpenPort(1)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim foo As Byte() = {1, 2, 3, 4, 5, 6, 7, 8}
        Me.SendData(foo)
    End Sub


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim foo As Byte()
        Dim bar As String
        Dim i As Integer
        foo = Me.ReceiveData(14)
        For i = 0 To foo.Length - 1
            If bar <> "" Then
                bar &= " "
            End If
            bar &= foo(i).ToString()
        Next
        MessageBox.Show("ASCII VALS " + bar)
    End Sub
End Class
Avatar of judico

ASKER

Thanks a lot for the code. Unfortunately, however, I don't have C# and can't build the code from the link you gave me. Is there any way you can send me the compiled object?
you dont "have" C# ? its in the SDK (its free) csc.exe

drop me an email this name @ this site and ill send you the .dll
Avatar of judico

ASKER

See, I got my Visual Studio .NET package from Staples and I only have Visual Basic .NET and Visual C++ .NET in it. Now that you told me, I found the csc.exe in c:\Windows\Microsoft.NET\Framework\v1.1.4322 but I don't know what to do with it. I tried opening it but I only saw a console opening momentarily. So, I guess the best bet would be to have the .dll sent to me somehow, if you don't mind. I think it's against the policy of this site to post e-mail addresses so would you mind uploading it somewhere so I can download it from there? Thanks in advance.
its cool just email me gregory_young@experts-exchange  remove the _
Avatar of judico

ASKER

I just sent an e-mail to gregoryyoung@experts-exchange.com.
Avatar of judico

ASKER

Thanks a lot. I'm trying to install it as a component in the toolbox but it won't install. Maybe I should use differently but how?
Avatar of judico

ASKER

correction: Maybe I should use it differenly but how?
just add a reference ... right cliick on references and select add , thn browse to the .dll ....
Avatar of judico

ASKER

Yes, adding it as a reference did it ... however, I don't seem to be able to accomplish much. I can open the port. I can see that by the light of the cable blinking but I can't receive data.
did you click the receive button ?
Avatar of judico

ASKER

Yes, I did ... no activity is observed, though ...
what com port do you have it setup with ?
Avatar of judico

ASKER

I have it set up with COM1.
Avatar of judico

ASKER

Obviously, it communicates with the port because I see the light on the cable blinking when depressing the button Open.
well I ran that here with an outboard device, read no problem ... what speed is your device set to ? that is currently running at 9600 ...
Avatar of judico

ASKER

I changed that. My device is running at 600.

I told you before, I tried various ways one can find on the net but the only way I could get data from the device was by using that special rs232comms1.dll written by Jonathan Travers. Unfortunately, for some reason I cannot concatenate the incoming characters into one compact string.
serial is serial ... I doubt that there is some magic with your serial device that causes it to work differently than any other.

what happens if you put a breakpoint into the receive ? and step through it.
heh I see a problem....

    Private Function ReceiveData(ByVal ExpectedBytes As Integer) As Byte()
        Dim i As Integer
        Dim Ret(ExpectedBytes) As Byte
        Dim tmpBuffer() As Byte
        Dim cnt As Integer = 0
        While (cnt < ExpectedBytes)
            Dim nBytes As Integer = Convert.ToInt32(Port.Recv(tmpBuffer))
            If nBytes > 0 Then
                For i = 0 To tmpBuffer.Length - 1
                    Ret(cnt) = tmpBuffer(i)
                    cnt += 1
                Next
            Else
                Thread.Sleep(Me.PollTime)
            End If
        End While
        Return Ret
    End Function

change it to that.
Avatar of judico

ASKER

I can't agree more ... serial is serial ... However, I have a feeling that there is something in VB.NET which I don't understand and that brings in the problem. How is it possible without any loop in the code the data to be received as if from a loop?

I'll try the code you just posted. Give me a minute ...
Avatar of judico

ASKER

Nope, no reaction at all. You can see the light blinking when depressing the button Open but depressing the button Receive yields no reaction whatsoever. The program hangs.
put a breakpoint in it and step through it ... yes it will hang as it is running in the same thread as your UI and its looping until it gets enough data (which is why I specified above that once its working we will put it into its own thread and raise events with full messages in them) Out of curiosity you said this is a radio shack device ... which one is it (is it expensive ?)
Avatar of judico

ASKER

This one device is an older version and RadioShack doesn't carry it any more, unfortunately. It doesn't have a brand name but the catalog number is 22-805.
Avatar of judico

ASKER

This is where it complains:

Dim nBytes As Integer = Convert.ToInt32(Port.Recv(tmpBuffer))

The error message is:

'Object reference not set to an instance of an object.'
are you clicking open first ?
Avatar of judico

ASKER

Yes, I am.
ok whats not set to an instance ... tmpbuffer is defined byref (the function sets it) ... is port null ? because before you said you clicked it and nothing happenned (it locked up) which is what should happen if you are not receiving data.
Avatar of judico

ASKER

I am sorry, I take that back ... I put a breakpoint on Open and now it passes the above step. What I observe now is it appears to loop forever here:

While (cnt < ExpectedBytes)
            Dim nBytes As Integer = Convert.ToInt32(Port.Recv(tmpBuffer))
            'Object reference not set to an instance of an object.
            If nBytes > 0 Then
                For i = 0 To tmpBuffer.Length - 1
                    Ret(cnt) = tmpBuffer(i)
                    cnt += 1
                Next
            Else
                Thread.Sleep(Me.PollTime)
            End If
        End While
yes it is saying "Do I have enough data ?" if yes return "Do I have current data?" if(yes) copy data

if its not receiving data it will just keep looping (this is how serial communications are generally implemented) ... is there anything else in the protocol that needs to be done i.e. setting a  pin to tell it to send data ?
Avatar of judico

ASKER

Yes, the DTR has to be set. I have in my code RS232Comms1.SetDTR().
after a bunch of diagnostics the answer was quite simple:

you are receiving binary not string data from the serial port.

the first character you are receiving is a null (ASCII 0) this value is used to delineate the end of a string in most string methodologies (a notable difference being Pascal style strings) unfortunatly .net uses standard C style strings (which means that when it encounters a null it perceives it as being the end of the string. Therefor the solution to your problem is going to be to either

1) hold the data in a byte array
2) use numbers in your string instead of values
3) use base64 to insure that the data is string safe

I would recommend using #1 ...

basically you will define ...

private buffer(1024) as byte

private currentwritelocation  as integer

then in your routine copy the byte over into the byte array at location currentwritelocation ... then add one to currentwritelocation but remember to % buffersize so you dont get over flow ...
Avatar of judico

ASKER

Can you please show me how to exactly do that?
well you said you needed 14 chars so I made the buffer here 14 ...

note that it resets the buffer location everytime you open port an send the poll ...

Imports System.Text
Imports System.Threading
Public Class Form1

    Inherits System.Windows.Forms.Form
    Dim ThreadedTimer As System.Threading.Timer
    Dim temp As String
    Private buffer(14) As Byte
    Private currentloc = 0

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

        Timer1.Enabled = True

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents RS232Comms1 As RS232.RS232Comms
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Button2 = New System.Windows.Forms.Button
        Me.RS232Comms1 = New RS232.RS232Comms
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(16, 32)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.ReadOnly = True
        Me.TextBox1.Size = New System.Drawing.Size(224, 184)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = ""
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(272, 248)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(16, 16)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Button2"
        '
        'RS232Comms1
        '
        Me.RS232Comms1.BaudRate = RS232.RS232Comms.eBAUDRATE.CBR_600
        Me.RS232Comms1.DataBits = RS232.RS232Comms.eDATABITS.DATA7
        Me.RS232Comms1.StopBit = RS232.RS232Comms.eSTOPBIT.TWOSTOPBITS
        '
        'Timer1
        '
        Me.Timer1.Enabled = True
        Me.Timer1.Interval = 2000
        '
        'ListBox1
        '
        Me.ListBox1.Location = New System.Drawing.Point(280, 32)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(184, 186)
        Me.ListBox1.TabIndex = 3
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(504, 266)
        Me.Controls.Add(Me.ListBox1)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Sub OpenPort()
        RS232Comms1.OpenComms()
        Me.currentloc = 0
        RS232Comms1.WriteBlock(&H12)
    End Sub


    Public Sub rS232Comms1_DataRxEvent(ByVal sender As System.Object, ByVal e As RS232.RxEventArgs) Handles RS232Comms1.DataRxEvent

        Debug.WriteLine("hnhn <------")

        Dim ii As Integer
        If (e.data.Length + Me.currentloc > 14) Then
            Throw New System.Exception("too much data for buffer")
        End If
        For ii = 0 To e.data.Length - 1
            Me.buffer(Me.currentloc + ii) = e.data(0)
        Next
        Me.currentloc += e.data.Length
    End Sub


    '                              QUIT
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        RS232Comms1.CloseComms()
        TextBox1.Clear()
        TextBox1.AppendText("Good Bye:   ")
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        RS232Comms1.CloseComms()
        Me.Refresh()
        OpenPort()
    End Sub
End Class
Avatar of judico

ASKER

And so, in the end, how do you get the concatenated string?
you can't the first character is an illegal string character ...

you could feasably say ...

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        RS232Comms1.CloseComms()
        dim i as integer
        dim s as string = ""
        for i = 1 to buffer.length -1
            s &= chr(buffer(i))
        next
        TextBox1.Clear()
        TextBox1.AppendText("Good Bye:   ")
    End Sub
notice that it skips the first character.
Avatar of judico

ASKER

Sorry, I said "in the end" ... I meant, after getting it in the byte array ... In other words, how do you get the concatenated string here:

Public Sub rS232Comms1_DataRxEvent(ByVal sender As System.Object, ByVal e As RS232.RxEventArgs) Handles RS232Comms1.DataRxEvent

        Debug.WriteLine("hnhn <------")

        Dim ii As Integer
        If (e.data.Length + Me.currentloc > 14) Then
            Throw New System.Exception("too much data for buffer")
        End If
        For ii = 0 To e.data.Length - 1
            Me.buffer(Me.currentloc + ii) = e.data(0)
        Next
        Me.currentloc += e.data.Length

        Dim i As Integer
        Dim s As String = ""
        For i = 1 To buffer.Length - 1
            s &= Chr(buffer(i))
        Next

        TextBox1.Text = s

End Sub

TextBox1 doesn't show anything when I run the program ... Neither does ListBox1.Items.Add(s).
btw

Me.buffer(Me.currentloc + ii) = e.data(0)
needs to be
Me.buffer(Me.currentloc + ii) = e.data(ii)

are there other illegal characters in your data ? I only skipped the first one because we saw earlier that it was a null character.
Avatar of judico

ASKER

I corrected Me.buffer(Me.currentloc + ii) = e.data(ii)
 but still I don't get the concatenated string. I'mchecking it also with debug:

For i = 1 To buffer.Length - 1
            s &= Chr(buffer(i))
            Debug.WriteLine(Chr(buffer(i)))
Next

and I still get nothing. As for other illegal characters, I wonder if there is a character after the last 14-th character of the string and it would also be illegal..
look at what buffer(i) equals as a number also

change this to

For i = 1 To buffer.Length - 1
            s &= Chr(buffer(i))
            Debug.WriteLine("position " + i + " = " + buffer(i))
Next

what numbers come out ?

Avatar of judico

ASKER

Had to change it to Debug.WriteLine("position " & CStr(i) & " = " & buffer(i)). This is what comes out:

...

hnhn <------
position 1 = 0
position 2 = 0
position 3 = 0
position 4 = 0
position 5 = 0
position 6 = 0
position 7 = 0
position 8 = 0
position 9 = 0
position 10 = 0
position 11 = 0
position 12 = 0
position 13 = 0
position 14 = 0
The thread 'Rx Thread' (0xc98) has exited with code 0 (0x0).
The thread 'Tx Thread' (0x53c) has exited with code 0 (0x0).
hnhn <------

etc.
is the loop above setting any data set a breakpoint and step through it ? i.e. does it loop through at all and set any data in your buffer ? it was printing data before ... btw remember that you are receiving the characters 1 at a time (maybe) ... so it may not print anything the first time through ... you really want to get the string when its completed ...

or you could just check that the number is > 0 before adding it ... but they may use other control characters.
Avatar of judico

ASKER

Let me just mention. The following:

For i = 1 To buffer.Length - 1
            s &= Chr(buffer(i))
            Debug.WriteLine("position " & CStr(i) & " = " & CStr(buffer.Length))
Next

gives

hnhn <------
position 1 = 15
position 2 = 15
position 3 = 15
position 4 = 15
position 5 = 15
position 6 = 15
position 7 = 15
position 8 = 15
position 9 = 15
position 10 = 15
position 11 = 15
position 12 = 15
position 13 = 15
position 14 = 15
The thread 'Rx Thread' (0xd0c) has exited with code 0 (0x0).
The thread 'Tx Thread' (0x14c) has exited with code 0 (0x0).
The thread 'Rx Thread' (0xe6c) has exited with code 0 (0x0).
The thread 'Tx Thread' (0xed4) has exited with code 0 (0x0).
hnhn <------
yes the length of your buffer is 15 bytes long
Avatar of judico

ASKER

Indeed, I was getting data in the debugger ... What happened now?
Avatar of judico

ASKER

OK, I found out ... We had, in addition to the above also the line:

 RS232Comms1.SetDTR()

Now, where shall I put this line here ... Let me see ...
change it to be like this ...

For i = 1 To currentloc
            s &= Chr(buffer(i))
            Debug.WriteLine("position " & CStr(i) & " = " & CStr(buffer.Length))
Next

remember that this will run once for every character (very important)

you should see something like ...

nothing
a
ab
abc
abcd
abcde
etc
Avatar of judico

ASKER

Seems I can't find a place for that line. It seems no matter where I put it after several readings it gives me:

'An unhandled exception of type 'System.Exception' occurred in RS232 4.exe

Additional information: too much data for buffer'
email me back your current code.
Avatar of judico

ASKER

It appears that the whole problem now focuses on the line

RS232Comms1.SetDTR()

First couple of times one sees in the ListBox1 the concatenated string but then the program crashes with the error message:

'An unhandled exception of type 'System.Exception' occurred in RS232 4.exe

Additional information: too much data for buffer'
Avatar of judico

ASKER

OK, hold on a sec ...
ASKER CERTIFIED SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of judico

ASKER

Great ... Why is this error message appearing after about 23 call to the multimeter:

An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll

Additional information: Object reference not set to an instance of an object.

On the 19th call some king of a hitch is seen.

Also, first and second strings are always incomplete ...

Great job ... we're almost done ...
Avatar of judico

ASKER

Great ... However, why is this error message appearing after about 23 call to the multimeter:

An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll

Additional information: Object reference not set to an instance of an object.

On the 19th call some kind of a hitch is seen.

Also, first and second strings are always incomplete ...

Great job ... we're almost done ...
Avatar of judico

ASKER

I wish these messages could be edited ...
well get on in  there with your debugger and see whats going on :)

is it possible that e.data is being returned as null ? perhaps it does this for a com error etc ?
Avatar of judico

ASKER

In the debugger I see that it's struggling to form nice packages of the type

hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09.
OH  09.9
OH  09.98
OH  09.98k
OH  09.98kO
OH  09.98kOh
OH  09.98kOhm
OH  09.98kOhm

hnhn <------



but there are numerous instance where it accomplishes the job only partially. See what is happenning at the beginning:



hnhn <------
~
~hnhn <------
~
~~
~~hnhn <------
~
~~
~~h
~~hhnhn <------
~
~~
~~h
~~hm
~~hmhnhn <------
~
~~
~~h
~~hm
~~hm

~~hm
The thread 'Rx Thread' (0xd40) has exited with code 0 (0x0).
The thread 'Tx Thread' (0x13c) has exited with code 0 (0x0).
hnhn <------
~
hnhn <------
O
O~
hnhn <------
O
OH
OHh
hnhn <------
O
OH
OH
OH m
hnhn <------
O
OH
OH
OH  
OH  

hnhn <------
O
OH
OH
OH  
OH  0
OH  0hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09.
OH  09.hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09.
OH  09.9
OH  09.9hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09.
OH  09.9
OH  09.98
OH  09.98hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09.
OH  09.9
OH  09.98
OH  09.98k
OH  09.98khnhn <------
O
OH
OH
OH  
OH  0
The thread 'Rx Thread' (0x2a0) has exited with code 0 (0x0).
The thread 'Tx Thread' (0xa24) has exited with code 0 (0x0).
hnhn <------





and this is towards the end







The thread 'Rx Thread' (0x4e4) has exited with code 0 (0x0).
The thread 'Tx Thread' (0xf24) has exited with code 0 (0x0).
hnhn <------
~
~H
hnhn <------
~
~O
~O
hnhn <------
~
~O
~OH
~OH
hnhn <------
~
~O
~OH
~OH
~OH 0
hnhn <------
~
~O
~OH
~OH
~OH  
~OH  9
hnhn <------
~
~O
~OH
~OH
~OH  
~OH  0
~OH  0.
hnhn <------
~
~O
~OH
~OH
~OH  
~OH  0
~OH  09
~OH  099
hnhn <------
~
~O
~OH
~OH
~OH  
~OH  0
~OH  09
~OH  09.
~OH  09.8
hnhn <------
~
~O
~OH
~OH
~OH  
~OH  0
~OH  09
~OH  09.
~OH  09.9
~OH  09.9k
hnhn <------
~
~O
~OH
~OH
~OH  
~OH  0
~OH  09
~OH  09.
~OH  09.9
~OH  09.98
~OH  09.98O
hnhn <------
~
~O
~OH
~OH
~OH  
~OH  0
~OH  09
~OH  09.
~OH  09.9
~OH  09.98
~OH  09.98k
~OH  09.98kO
~OH  09.98kOm
hnhn <------
~
~O
~OH
~OH
~OH  
~OH  0
~OH  09
~OH  09.
~OH  09.9
~OH  09.98
~OH  09.98k
~OH  09.98kO
~OH  09.98kOh
~OH  09.98kOhm
~OH  09.98kOhm

hnhn <------
~
~O
~OH
~OH
~OH  
~OH  0
~OH  09
~OH  09.
~OH  09.9
~OH  09.98
~OH  09.98k
~OH  09.98kO
~OH  09.98kOh
~OH  09.98kOhm
~OH  09.98kOhm

~OH  09.98kOhm
The thread 'Rx Thread' (0x948) has exited with code 0 (0x0).
The thread 'Tx Thread' (0x758) has exited with code 0 (0x0).
hnhn <------
~
hnhn <------
O
OO
hnhn <------
O
OH
OHH
hnhn <------
O
OH
OH
OH  
hnhn <------
O
OH
OH
OH  
OH  
hnhn <------
O
OH
OH
OH  
OH  0
OH  00
hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  099
hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09.
OH  09..
hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09.
OH  09.9
OH  09.99
hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09.
OH  09.9
OH  09.98
OH  09.988
hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09.
OH  09.9
OH  09.98
OH  09.98k
OH  09.98kk
hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09.
OH  09.9
OH  09.98
OH  09.98k
OH  09.98kO
OH  09.98kOh
OH  09.98kOhh
hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09.
OH  09.9
OH  09.98
OH  09.98k
OH  09.98kO
OH  09.98kOh
OH  09.98kOhm
OH  09.98kOhmm
hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09.
OH  09.9
OH  09.98
OH  09.98k
OH  09.98kO
OH  09.98kOh
OH  09.98kOhm
OH  09.98kOhm

OH  09.98kOhm


The thread 'Rx Thread' (0xb40) has exited with code 0 (0x0).
The thread 'Tx Thread' (0xba0) has exited with code 0 (0x0).
hnhn <------
O
hnhn <------
O
OH
hnhn <------
O
OH
OH
hnhn <------
O
OH
OH
OH  
hnhn <------
O
OH
OH
OH  
OH  0
hnhn <------
O
OH
OH
OH  
OH  0
OH  09
hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09.
hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09.
OH  09.9
hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09.
OH  09.9
OH  09.98
hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09.
OH  09.9
OH  09.98
OH  09.98k
hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09.
OH  09.9
OH  09.98
OH  09.98k
OH  09.98kO
OH  09.98kOh
hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09.
OH  09.9
OH  09.98
OH  09.98k
OH  09.98kO
OH  09.98kOh
OH  09.98kOhm
hnhn <------
O
OH
OH
OH  
OH  0
OH  09
OH  09.
OH  09.9
OH  09.98
OH  09.98k
OH  09.98kO
OH  09.98kOh
OH  09.98kOhm
OH  09.98kOhm

OH  09.98kOhm


The thread 'Rx Thread' (0xfc0) has exited with code 0 (0x0).
The thread 'Tx Thread' (0xe54) has exited with code 0 (0x0).
An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll

Additional information: Object reference not set to an instance of an object.

The program '[3956] RS232 4.exe' has exited with code 0 (0x0).



Maybe some Sleep should be put inbetween somewhere in the code?
Avatar of judico

ASKER

I'm trying with

 System.Threading.Thread.Sleep(100)

but still crashes around the 20th call (as seen from the TexBox1). Also, the intervals between the appearance of the data are uneven for some reason.
i dont think the sleep has much relevance ...

what should the strings look like ? when it gets it only partially what happens when you step through it ? could it be bad reads ?
Avatar of judico

ASKER

A good string is the folllowing:

" OH  09.98kOhm"

That's why I was telling you that it's 14-character string.

Obviously, there are multiple bad reads.
well thats always been a problem with serial ... you dont always get good data across the line (most protocols implement mechanisms such as checksums for testing the quality of data)
Avatar of judico

ASKER

The problematic part is that it crashes somewhere around the 20th point. How can this be overcome? Could throwing in some exceptions help it (although I'd like to avoid exceptions as much as possible)?
well why not look whats causing the exception ?

try
    some code
catch e as exception
    MessageBox.Show(Ex.ToString())
end try
then you can see what line of code its failing on ...
Avatar of judico

ASKER

This is the error message that appears after the crash (I still haven't placed the last exception you suggested)

'An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll

Additional information: Object reference not set to an instance of an object.'
Avatar of judico

ASKER

I wrapped up anything having to do with the post in exceptions and yet the problem persists ... what could be causing it?
Avatar of judico

ASKER

Why should it wait for 20 calls and then crash? Why not sooner?
put the try catch in and maybe I can tell you (it will tell me what line it fails on)
Avatar of judico

ASKER

I put many try-catches ... which one is the important one?
Avatar of judico

ASKER

gregoryyoung, I think you did an exceptional job. Do you think it will be better for me to accept your answer here so that you can get your points? I can then open a new question in which we can discuss the outstanding problems with crashing of the device.

judico
its up to you mate ... honestly, ee points and a dollar will get me a cup of coffee ....

put a try catch around each method ... lets see what the stack trace comes up as ...

example:

public sub foo()
    try
            '
            ' metod code
            '
    catch Ex as Exception
           MessageBox.Show(Ex.ToString()))
    end try
end sub

I'd show you how to do a default error handler (which I'll show you anyways) but this will probably be quicker for your like 5 methods.
Avatar of judico

ASKER

OK. We may continue here for a while, if you don't mind, to keep it all in one place. If it gets too big I'll open a new question.

Now, before playing with the exceptions let me mention a new problem that emerged. So far I've been using a RS232-USB cable and all the successes so far were when using this cable. Out of curiosity I connected the multimeter directly to the RS232 port (which I should have done in the first place but I didn't). And, lo and behold, the program refuses to acquire data. Now, that's unexpected, isn't it. I tried changing the parameters of the COM1 port in the  Control Panel, restarted the computer, you name it. No effect ... When I revert to the RS232-USB cabel everything goes back to normal (but crashing around the 20th point, if that's normal.) Now I'll try the exceptions and will let you know what the outcome is.
Avatar of judico

ASKER

I placed exception handlers around the code in:



Public Sub OpenPort()
...
End Sub



and



Public Sub rS232Comms1_DataRxEvent(ByVal sender As System.Object, ByVal e As RS232.RxEventArgs) Handles RS232Comms1.DataRxEvent
...
 End Sub



and this is the outcome. Initially, first couple of point (as seen in the TextBox1), the pattern is always crummy and looks like this:

hnhn <------
hnhn <------
\
\k
\khnhn <------
\
\k
\kO
\kOhnhn <------
\
\k
\kO
\kOh
\kOhhnhn <------
\
\k
\kO
\kOh
\kOhm
\kOhmhnhn <------
\
\k
\kO
\kOh
\kOhm
\kOhm

\kOhm
The thread 'Rx Thread' (0xfc) has exited with code 0 (0x0).
The thread 'Tx Thread' (0x3d4) has exited with code 0 (0x0).
hnhn <------




Later the pattern stabilizes and typically looks like this:




The thread 'Rx Thread' (0x28c) has exited with code 0 (0x0).
The thread 'Tx Thread' (0x178) has exited with code 0 (0x0).
hnhn <------
O
hnhn <------
O
OH
OH
hnhn <------
O
OH
OH
OH  
hnhn <------
O
OH
OH
OH  
OH  1
hnhn <------
O
OH
OH
OH  
OH  1
OH  10
hnhn <------
O
OH
OH
OH  
OH  1
OH  10
OH  10.
hnhn <------
O
OH
OH
OH  
OH  1
OH  10
OH  10.
OH  10.5
OH  10.53
hnhn <------
O
OH
OH
OH  
OH  1
OH  10
OH  10.
OH  10.5
OH  10.54
OH  10.54k
hnhn <------
O
OH
OH
OH  
OH  1
OH  10
OH  10.
OH  10.5
OH  10.54
OH  10.54k
OH  10.54kO
hnhn <------
O
OH
OH
OH  
OH  1
OH  10
OH  10.
OH  10.5
OH  10.54
OH  10.54k
OH  10.54kO
OH  10.54kOh
OH  10.54kOhm
OH  10.54kOhm

hnhn <------
O
OH
OH
OH  
OH  1
OH  10
OH  10.
OH  10.5
OH  10.54
OH  10.54k
OH  10.54kO
OH  10.54kOh
OH  10.54kOhm
OH  10.54kOhm

OH  10.54kOhm
The thread 'Rx Thread' (0x1d0) has exited with code 0 (0x0).
The thread 'Tx Thread' (0x224) has exited with code 0 (0x0).
hnhn <------




It appears that in order for one data point (compact 13-character string) to be seen in TextBox1 the program has to do a lot of work and have the subroutine 'Public Sub rS232Comms1_DataRxEvent( ...' be called close to a dozen times (why not 13 or 14 times?). Thus, it appears that not only the loop seen within the subroutine is at work here but also some invisible loop, works behind the scenes. This is what appears to me. What would you say?
its is being called 13 or 14 times ... the loop is happenning because you are receiving that event once per character. The try/catches have nothing to do with the data, they only catch unhandled exceptions ... the problems you are having are with bad data coming accross. Did you get the exception information that occurs ? Also USB vs normal com port sounds like a configuration issue with your machine, before attempting to get the app working make sure you can get data accross the port with a utility such as hyperterminal.

Avatar of judico

ASKER

I don't understand why to get one data point I should call the 'Public Sub rS232Comms1_DataRxEvent( ...' subroutine so many times. Shouldn't calling it once be enough because it sets the DTR and then the receipt of the individual characters is performed by the loop

 For ii = 0 To e.data.Length - 1
            Me.buffer(Me.currentloc + ii) = e.data(ii)
Next ?

If not so, why this loop at all?

As for the exceptions, I'm not getting any messages for these exceptions. I don't know what else to look for.

Hyperterminal -- I tried to get data using it but I can't. With another device which automatically sends data I am able to obtain the data. Here with this device, which needs to be called first in order for it to send data, I am not receiving anything.

By the way, now that you mention the hyperterminal, I have posted a question before regarding the hyperterminal

https://www.experts-exchange.com/questions/21079528/Obtaining-data-from-a-device-through-the-HyperTerminal.html

and I was wondering couldn't it be the solution to all the problems. Aren't there some hooks to the hyperterminal which the program could use to get the data directly from it bypassing the use of special rs232 dll's or code?
because there could be more than 1 byte in data ... but it appears that you are receiving the event with 1 byte everytime.
Avatar of judico

ASKER

Here's one typical exception message:

---------------------------

---------------------------
System.Threading.ThreadAbortException: Thread was being aborted.

   at System.Diagnostics.Debugger.Log(Int32 level, String category, String message)

   at System.Diagnostics.DefaultTraceListener.internalWrite(String message)

   at System.Diagnostics.DefaultTraceListener.Write(String message, Boolean useLogFile)

   at System.Diagnostics.DefaultTraceListener.WriteLine(String message, Boolean useLogFile)

   at System.Diagnostics.DefaultTraceListener.WriteLine(String message)

   at System.Diagnostics.TraceInternal.WriteLine(String message)

   at System.Diagnostics.Debug.WriteLine(String message)

   at RS232_3.Form1.rS232Comms1_DataRxEvent(Object sender, RxEventArgs e) in C:\gregoryyoung\multimeter\Form1.vb:line 152
---------------------------
OK  
---------------------------
thats a typical exception ? thats an error on debug.writeline() ??
what happenned to object reference not set to an instance ? also what happens if you comment out the debug.writelines that are happenning (like 140 per message)
Avatar of judico

ASKER

Indeed, when comment out the debug writelines everything starts working quite smoothly. You can see in the listBox1 window the packets of strings nicely concatenated.

And yet, the 20th or so point comes and the whole thing crashes.

I don't understand what you mean by "what happenned to object reference not set to an instance ?"
before you said you were receiving an object reference not set to an instance issue.
Avatar of judico

ASKER

Oh, yes, I'm sorry. This is exactly the message appearing when the program crashes (afte taking 20 or so points). Until the crash, however, with the debug writelines out, the whole thing appears to work very smoothly, as if it's all set.
where is the stack trace for the one "object reference not set to an instance" ?
btw: I think you are getting those errors because your timer is closing the port (which aborts the threads) you are writing out so much stuff to debug that its taking more than 2 seconds :-/
Avatar of judico

ASKER

See, if I don't set the timer to close the port the program doesn't work -- acquisition of points stops.  I increased the time intervals to 4s and removed all the debug writelines. That led to no improvement -- the program again crashes around 20th point.

I don't know how to get the stack trace for the "object reference not set to an instance".
Avatar of judico

ASKER

Here's one exception I am not usually getting but it started popping up after I tried timer intervals of 1000ms and then reverted back to 2000ms.

---------------------------

---------------------------
System.Threading.ThreadAbortException: Thread was being aborted.

   at RS232.Win32Com.EscapeCommFunction(IntPtr hFile, UInt32 dwFunc)

   at RS232.RS232Comms.SetDTR()

   at RS232_3.Form1.rS232Comms1_DataRxEvent(Object sender, RxEventArgs e) in C:\gregoryyoung\multimeter 2\Form1.vb:line 146
---------------------------
OK  
---------------------------
Avatar of judico

ASKER

Now, upon restart everything is back to normal -- above exception doesn't pop up (the crash at the 20th point is still here, though).
again it is because you are closing the port and then trying to do stuff ....

the only exception I want to see is the "object reference not set to an instance" these are all because you are spending more than 2 seconds processing data then closing the port while you are still processing.
Avatar of judico

ASKER

If I don't close the port in the Tick routine the program executes only once and stops.

In the Tick routine I have

            RS232Comms1.CloseComms()
            Me.Refresh()
            OpenPort()

What puzzles me there is that what should open the port is RS232Comms1.OpenComms() while it actually is OpenPort(). When I try RS232Comms1.OpenComms() the program hangs.

im going to guess that opencomms starts the threads openport actually opens the port and starts the threads ... but i dont know the library
Avatar of judico

ASKER

gregoryyoung,

Thank you very much for your help. I just closed this question and awarded you the 500 points. Unfortunately, I could not get the device to work without crashing beyond somewhere around the 20th point. This is not at all you fault and probably has to do with the device itself. I wonder if it will be worth to continue with it but if you're interested I can send it to you so that you can play with it a little bit, if you have the time. Now I'm planning to move on to a PIC device and would appreciate it very much if you could help me with that if problems with it arise (none so far but who knows). Thanks again and all the best.

judico