Results 1 to 5 of 5

Thread: Problem with COM1, errors....

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Problem with COM1, errors....

    Hello I'm having an issue with a serial connection program. It connects a motor controller to a computer via RS232.

    Here is the code:

    VB Code:
    1. Public Class Form1
    2.     Dim COM1 As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("COM1", 38400, IO.Ports.Parity.Odd, 7, IO.Ports.StopBits.One)
    3.     Dim Ring_Vel As Integer = 1
    4.  
    5.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    6.         'First run setup
    7.         COM1.WriteLine("1PW" & vbCr & vbCr)
    8.         COM1.WriteLine("1RSF" & vbCr)
    9.         COM1.WriteLine("1AC=100000" & vbCr)
    10.         COM1.WriteLine("1VL=" & Ring_Vel & vbCr)
    11.         VScrollBar1.Value = Ring_Vel
    12.         Label3.Text = Ring_Vel
    13.     End Sub
    14.  
    15.  
    16.     Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll
    17.         'Speed Controller
    18.         Ring_Vel = VScrollBar1.Value * 1000
    19.         COM1.WriteLine("1VL=" & Ring_Vel & vbCr)
    20.         If Button1.Enabled = False Then COM1.WriteLine("1RFN" & vbCr)
    21.         Label3.Text = VScrollBar1.Value
    22.     End Sub
    23.  
    24.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    25.         'Start Button
    26.         Button1.Enabled = False
    27.         Button2.Enabled = True
    28.         COM1.WriteLine("1RFN" & vbCr)
    29.     End Sub
    30.  
    31.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    32.         'Stop Button
    33.         Button1.Enabled = True
    34.         Button2.Enabled = False
    35.         COM1.WriteLine("1ST" & vbCr)
    36.     End Sub
    37. End Class

    If I change the VScroll1 quickly and often I get this error:
    The I/O operation has been aborted because of either a thread exit or an application request.


    Why is it doing this? I tried to put the COM1.WriteLine on a timer (.5 sec) so it doesnt overload the buffers or something. And it will still do it.

    I dont understand bc I have another program that has a loop, and every cycle of the loop it sends something to the unit. It was able to reach nearly 30 sends per second.

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Problem with COM1, errors....

    maybe the write buffer is to small

    SerialPort1.WriteBufferSize=?????????

    is the device you are talking to replying?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Problem with COM1, errors....

    .WriteLine may raise an exception so it should be in a try catch block

    Code:
        Dim anEx As New Exception
        'instead of
        'COM1.WriteLine("1AC=100000" & vbCr)
        'try this
        '
        'anEx = myComWriteLine("1AC=100000" & vbCr)
        '    If anEx Is Nothing Then
        ''io was ok
        '        Stop
        '    Else
        ''process exception
        '        Stop
        '    End If
        Private Function myComWriteLine(ByVal toBeWritten As String) As Exception
            Try
                COM1.WriteLine(toBeWritten)
            Catch ex As Exception
                Return ex
            End Try
            Return Nothing
        End Function
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: Problem with COM1, errors....

    how do you prevent it from sending output more often then every .5 seconds? The scrolling event can be raised very quickly. I'm not sure if the WriteLine() method blocks or not but if it doesn't then you could be sending overlapping messages perhaps not waiting long enough for your device to respond....

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Problem with COM1, errors....

    the default for WriteTimeOut is -1 which blocks until the write is complete.

    are you manually scrolling the scroll bar when the error occurs?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width