none
Serial issues RRS feed

  • Question

  • Good Afternoon, I am creating an application in VB to do data collection for SPC.  The VB is using RS232 to communicate with a Tektronix TX3 multi meter.  I can send commands to the meter but I get an occasional reply.  The meter communicates with a computer in a terminal program with no issues.  I tried a couple different computers to be sure the meter is sound.  I connected the computer running the VB program to another computer running a terminal program and the commands are getting sent fine and the reply is coming in fine.     With the meter connected to the computer running the VP program, The meter responds to the commands sent correctly and promptly.  I have tried various read,Readline, ReadExisting (this works best).  Ideas and thoughts are welcome.  

    During the Non receiving times, which is most of the time, the event is not triggered, in Terminal program to VB program, there is no issues.     

    Imports System

    Imports System.IO.Ports

    Imports System.Threading

    Public Class Form1

        Dim com1 As New IO.Ports.SerialPort("com1")

        Dim Data As String

        Dim returnStr As String = ""

        Dim readBuffer As String = String.Empty

        Dim Bytenumber As Integer

        Dim ByteToRead As Integer

        Dim byteEnd(2) As Char

        '  Sub form1_load()

        '      AddHandler com1.DataReceived, AddressOf DataReceivedHandler

        '  End Sub

        Private Sub ExitBut_Click(sender As System.Object, e As System.EventArgs) Handles ExitBut.Click

            If com1.IsOpen Then

                com1.DiscardInBuffer()

                com1.DiscardOutBuffer()

            End If

            com1.Close()

            Application.Exit()

        End Sub

        'Parameters below are for the TX3 DMM. A simple 3 wire Null Modem connection.

        Private Sub OpenBtn_Click(sender As System.Object, e As System.EventArgs) Handles OpenBtn.Click

            AddHandler com1.DataReceived, AddressOf DataReceivedHandler

            If com1.IsOpen = False Then

                com1.Open()

                com1.BaudRate = 9600

                com1.Parity = Parity.Even

                com1.DataBits = 7

                com1.StopBits = StopBits.One

                com1.Handshake = Handshake.None

                com1.RtsEnable = False

                com1.ReceivedBytesThreshold = 1

                com1.NewLine = vbCrLf

                com1.ReadTimeout = 10000

                '  com1.WriteLine(TextBox1.Text & vbCr) ' To bw added to the tranmitt button.

                PictureBox1.BackColor = Color.Aquamarine

            End If

        End Sub

        Private Sub ClsBtn_Click(sender As System.Object, e As System.EventArgs) Handles ClsBtn.Click

            If com1.IsOpen = True Then

                com1.DiscardInBuffer()

                com1.DiscardOutBuffer()

                com1.Close()

                TextBox1.Text = "thats All Folkes"

            End If

        End Sub

        Private Sub ClearBtn_Click(sender As System.Object, e As System.EventArgs) Handles ClearBtn.Click

            If com1.IsOpen = True Then

                com1.DiscardInBuffer()

                com1.DiscardOutBuffer()

            End If

            TextBox1.Text = ""

            TextBox2.Text = ""

            Me.BackColor = Color.LightGray

        End Sub

        Private Sub CmdBtn_Click(sender As System.Object, e As System.EventArgs) Handles CmdBtn.Click

            If com1.IsOpen = True Then

                com1.WriteLine(TextBox1.Text)

                PictureBox1.BackColor = Color.DarkGreen

            Else

                TextBox1.Text = "Comport Closed!"

            End If

        End Sub

    #Region "Data entry"

        Private Sub DataReceivedHandler(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

            PictureBox1.BackColor = Color.BlueViolet

            Me.BackColor = Color.Cyan

            If com1.IsOpen = True Then

                Try

                    byteEnd = com1.NewLine.ToCharArray

                    Bytenumber = com1.BytesToRead

                    readBuffer = com1.ReadExisting()

                    'readBuffer = com1.ReadLine

                    Me.Invoke(New EventHandler(AddressOf DoUpdate))

                Catch ex As Exception

                    MsgBox("Read" & ex.Message)

                End Try

            End If

        End Sub

        Private Sub DoUpdate(ByVal sender As System.Object, e As System.EventArgs)

            PictureBox1.BackColor = Color.Green

            TextBox2.Text = readBuffer

            'com1.DiscardInBuffer()

            Timer1.Enabled = True

        End Sub

    #End Region

        'Private Sub DataReceivedEventHandler(sender As Object, e As SerialDataReceivedEventArgs)

        '    Throw New NotImplementedException

        ' End Sub

    End Class

           

    Monday, December 11, 2017 7:58 PM

Answers

  • So the question remains (or perhaps should have been), What does TT and HT do that I am missing??

    Is there a way to view what is coming into the Serial buffer??

    Thank you.

    Jim H

    Are you opening and closing this port more than once per run of the application?  If so then you have multiple DataReceived event handlers

    Try this

        Private WithEvents com1 As New IO.Ports.SerialPort
    
        Private Sub OpenBtn_Click(sender As System.Object, e As System.EventArgs) Handles OpenBtn.Click
    
            'AddHandler com1.DataReceived, AddressOf DataReceivedHandler  'REMOVE THIS line <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    
            'note how boolean values are checked
            If Not com1.IsOpen Then
                'settings
                com1.PortName = "COM1"
                com1.BaudRate = 9600
                com1.Parity = IO.Ports.Parity.Even
                com1.DataBits = 7 'Is this correct???????????????????????
                com1.Handshake = IO.Ports.Handshake.None
                com1.StopBits = IO.Ports.StopBits.One
                com1.RtsEnable = False
                Try 'then open
                    com1.Open()
                    com1.DtrEnable = True
                    PictureBox1.BackColor = Color.Aquamarine
                Catch ex As Exception
                    Stop 'todo - what happens if the open fails
                End Try
    
                ''for testing
                'If com1.IsOpen Then
                '    Try
                '        com1.Close()
                '    Catch ex As Exception
                '        Stop 'todo - what happens if the close fails
                '    End Try
                'End If
            End If
        End Sub
    
        'change this 
        'Private Sub DataReceivedHandler(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        'to
        Private Sub com1_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles com1.DataReceived
            'your code here
        End Sub
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it" - MSDN User JohnWein

    • Marked as answer by Jim_at_work Thursday, December 14, 2017 6:06 PM
    Thursday, December 14, 2017 2:38 PM

All replies

  • I can send commands to the meter but I get an occasional reply.

    If the event occurs more than once (which is likely) then only the last received text will be displayed.  You need to change

      TextBox2.Text = readBuffer

    to

      TextBox2.Text &= readBuffer

    Monday, December 11, 2017 8:39 PM
  •  I would also highly recommend not directly accessing any controls on the form from inside the DataReceived event either.  You are making cross-thread calls by doing that.  I have seen that cause unpredictable behavior more than once.  If you really need to set the Form and PictureBox BackColor,  invoke a delegate to do it.  I see you are doing it for setting the TextBox text to the ReadBuffer so,  not sure why you would skip doing it for accessing other controls.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, December 11, 2017 9:21 PM
    Monday, December 11, 2017 9:02 PM
  • Thank you for the information and your assistance.

    This will all be removed when the issue corrected. 

    I was testing to see if the DataReceived event was being triggered.  The event acts like it is not. When I am communicating with the terminal program on the other PC, I observe a brie Change in color in the picture box.  When the meter is not talking, there is no change, as if there is not reply.  When the system briefly functions with the meter communicating with the VB program, the colors respond as if Terminal program was connected.

    I was able to recreate a similar situation between the VB program and the Terminal program on another PC by changing the parity settings.  I have set the Port settings in the Device Manager as well as through the program.  I'm not sure If this is a stretch, am I looking Parity?  Both computers (with the VB program and the one with Terminal program) when running the Terminal Program communicate with the meter correctly.

    Again thank you for the information.

    Tuesday, December 12, 2017 2:53 PM
  • Thank you for the information, but It had no effect.

    Please read my response below for more information.

    Tuesday, December 12, 2017 2:54 PM
  • I can send commands to the meter but I get an occasional reply.

    If the event occurs more than once (which is likely) then only the last received text will be displayed.  You need to change

      TextBox2.Text = readBuffer

    to

      TextBox2.Text &= readBuffer


    Spot on.

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it" - MSDN User JohnWein

    Tuesday, December 12, 2017 3:11 PM
  • The "Textbox2.Text &= readBuffer doesn't have an effect.

    Another Computer running in terminal mode has no issues Communicating with the VB program in either direction.

    Thank you for the link to Serial Port info, I am reading it.

    Tuesday, December 12, 2017 4:03 PM
  •  I am not sure where you got the settings you are using for the serial port but,  looking at the Pdf User Manual for the TX1 and TX3 meters,  they give no information on the serial port settings that are required.  They do say their is more information on the software installation CD about connecting it to a computer with the serial port.  Did you get those settings for the serialport from the CD?

     If not, and you are just guessing at them,  then you should contact Tektronix and see if they can give you this info or maybe even an API you can use.  Sometimes an API will be available with some code examples of using it.

     Also,  just because you are accessing those controls inside the DataReceived event just for testing,  it still should not be done and still can cause strange unexpected results. Not saying it is,  but i have seen it happen more than once.  8)


    If you say it can`t be done then i`ll try it

    Tuesday, December 12, 2017 4:21 PM
  • Tektronix put out  "Tektronix user programmers guide" version 2.1.  I got this from Tektronix just before they sold there DMM business to Fluke.

    Section 2.2 shows the parameter settings for communications.

    Baud 9600

    Parity: even

    Stop Bits 1

    Flow control None.

    If you want to see this, I can Email it to you.  It isn't large.

    I have removed them from the program and it also has had no effect.     

    Thanks again for your assistance.

    Tuesday, December 12, 2017 4:36 PM
  • Just to clarify:

    I removed the controls from the program with no effect.

    Tuesday, December 12, 2017 4:39 PM
  • Hi Jim,

    As you mentioned terminal program, do you have terminal program that can communicate with your meter? What is it, how do you get it?

    Devices that support program communications have their communication protocols/syntax that programmer should follow. I think that is the user programmers guide you have got. Do you follow that syntax strictly when you type commands in TextBox1?

    You got occasional reply, you can check the reply to figure out what information it contains(perhaps communication failure I think).

    Best Regards,

    Charles He


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 14, 2017 3:30 AM
  • Good Morning Charles,

    I am using Tera Term (TT), It was a free down load.    I also use Hyper Terminal (HT) but went to Tera Term when Hyper Terminal was left out of Windows 7. I use either of this for communicating with PIC microcontrollers also. Either of these two work well with this meter.

    When the comms does work with the meter, The reply is correct for the first several attempts, then it is present but wrong a couple time, then not there at all but the meter still responds to commands to change settings.

    When I use TT or HT, the VP program gives me the correct character every time, no matter how many time I send it as long as I use the ReadExisting.  I use this for the times when the meter is communicating properly.  I have tried ReadLine, ReadChar, and other things with out success. 

    I have followed the manual very strictly with parameters as well as syntax.  I use this meter to set up communications with Mitsubishi PLC's, LabView Applications, Test Point ( similar to Labview) and had have had good results.  The difference (for the most part) is there commination's objects are "canned" and as long as thing (i.e. Parameters, syntax) match (including the wiring), the reply is good.

    I am trying to figure out what HT and TT do that I am missing. (I know it is what I am doing incorrectly).  The code  above in only from the communications page of the project.  The Print, Email, and File Handling all function correctly.

    So the question remains (or perhaps should have been), What does TT and HT do that I am missing??

    Is there a way to view what is coming into the Serial buffer??

    Thank you.

    Jim H

    Thursday, December 14, 2017 1:23 PM
  •  Jim,

     Have you tried a serial port monitoring software to view the sent and received data?  Perhaps try one and see what may be different in the data between a working and non-working test.  I am sure there are a few different programs for doing this if you search the net, although some may cost you.  The link below is one that has a free 15 day trial.  I did not test it but, it appears it would be useful in this situation.

    Serial Port Monitor

     

     ***EDIT***

     Here is a google search result that lists a few (some free)

    Google Serach - "serial port monitoring software"

     Here is a free one...

    Free Serial Port Monitor


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Thursday, December 14, 2017 2:13 PM
    Thursday, December 14, 2017 2:07 PM
  • So the question remains (or perhaps should have been), What does TT and HT do that I am missing??

    Is there a way to view what is coming into the Serial buffer??

    Thank you.

    Jim H

    Are you opening and closing this port more than once per run of the application?  If so then you have multiple DataReceived event handlers

    Try this

        Private WithEvents com1 As New IO.Ports.SerialPort
    
        Private Sub OpenBtn_Click(sender As System.Object, e As System.EventArgs) Handles OpenBtn.Click
    
            'AddHandler com1.DataReceived, AddressOf DataReceivedHandler  'REMOVE THIS line <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    
            'note how boolean values are checked
            If Not com1.IsOpen Then
                'settings
                com1.PortName = "COM1"
                com1.BaudRate = 9600
                com1.Parity = IO.Ports.Parity.Even
                com1.DataBits = 7 'Is this correct???????????????????????
                com1.Handshake = IO.Ports.Handshake.None
                com1.StopBits = IO.Ports.StopBits.One
                com1.RtsEnable = False
                Try 'then open
                    com1.Open()
                    com1.DtrEnable = True
                    PictureBox1.BackColor = Color.Aquamarine
                Catch ex As Exception
                    Stop 'todo - what happens if the open fails
                End Try
    
                ''for testing
                'If com1.IsOpen Then
                '    Try
                '        com1.Close()
                '    Catch ex As Exception
                '        Stop 'todo - what happens if the close fails
                '    End Try
                'End If
            End If
        End Sub
    
        'change this 
        'Private Sub DataReceivedHandler(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        'to
        Private Sub com1_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles com1.DataReceived
            'your code here
        End Sub
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it" - MSDN User JohnWein

    • Marked as answer by Jim_at_work Thursday, December 14, 2017 6:06 PM
    Thursday, December 14, 2017 2:38 PM
  •  I was curios about this too since it was not mentioned.
    com1.DataBits = 7 'Is this correct???????????????????????

     


    If you say it can`t be done then i`ll try it

    Thursday, December 14, 2017 3:46 PM
  • This is mentioned in another post and the communications manual for the meter specifies 7 data bits.

    I implemented the code from dbasnett, the app is functioning.  I did some testing and before I made the changes according to dbasnett, this didn't work exactly, but after I send a command orquerry to the meter, there is a 4 to 6 second pause and then a "<4" ("Time out, command not processed" from the manual) reply to the VB program.  

    The line com1.NewLine = vbCrLf needed to be added. 

    Before seeing the com1.newline, For some reason, I went to the original program and made a query, I got a reply.  After a few query's, the error occurred.  I re-ran dbasnets code, waited for the error went to my original code, it worked.  I did some testing.

    The command / query is 3 bytes, some query's are 4 bytes.  The reply's to the commands are 3 bytes always. The reply's to the query's are (for what I'm asking) 15 bytes.  Consistently, I could send 23 commands or 8 query's. The code from dbasnet would reset something (I haven't found out what yet) and my code will work correctly for several cycles as described.

    This condition seems like something was not being cleared/reset or some kind of "buffer overflow". Can anyone please perhaps tell me what it was mot likely?  It wasn't on the meter side because, I could go directly into a terminal program and the meter would respond.

    Anyway. I will have the application up and running rather soon now.  Thank you all for your time and input.

    dbasnetts article is a good read (well written) on serial ports.

    Best Regards,

    Jim_at_work

    Thursday, December 14, 2017 6:06 PM
  • dbasnetts article is a good read (well written) on serial ports.
     Yes,  dbassnet is very knowledgeable when it comes to working with serial port data.  However,  that is not to say Acamar and others are not.  They are much better than me,  i have not worked with serial ports in many years now.  Anyways,  glad you are getting somewhere with it.  Good luck.  8)

    If you say it can`t be done then i`ll try it

    Thursday, December 14, 2017 7:25 PM