|
-
Oct 29th, 2008, 04:13 PM
#1
Thread Starter
Addicted Member
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:
Public Class Form1
Dim COM1 As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("COM1", 38400, IO.Ports.Parity.Odd, 7, IO.Ports.StopBits.One)
Dim Ring_Vel As Integer = 1
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'First run setup
COM1.WriteLine("1PW" & vbCr & vbCr)
COM1.WriteLine("1RSF" & vbCr)
COM1.WriteLine("1AC=100000" & vbCr)
COM1.WriteLine("1VL=" & Ring_Vel & vbCr)
VScrollBar1.Value = Ring_Vel
Label3.Text = Ring_Vel
End Sub
Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll
'Speed Controller
Ring_Vel = VScrollBar1.Value * 1000
COM1.WriteLine("1VL=" & Ring_Vel & vbCr)
If Button1.Enabled = False Then COM1.WriteLine("1RFN" & vbCr)
Label3.Text = VScrollBar1.Value
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Start Button
Button1.Enabled = False
Button2.Enabled = True
COM1.WriteLine("1RFN" & vbCr)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'Stop Button
Button1.Enabled = True
Button2.Enabled = False
COM1.WriteLine("1ST" & vbCr)
End Sub
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.
-
Oct 29th, 2008, 04:21 PM
#2
Re: Problem with COM1, errors....
maybe the write buffer is to small
SerialPort1.WriteBufferSize=?????????
is the device you are talking to replying?
-
Oct 29th, 2008, 04:33 PM
#3
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
-
Oct 29th, 2008, 04:43 PM
#4
Hyperactive Member
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....
-
Oct 29th, 2008, 04:47 PM
#5
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|