Serial Port Strange Error!
-
Hello everyone, I am trying to send an array of bytes using the serial port and I get a strange I/O exception. In my opinion there is no reason for the error. Here is the code;
SerialPort comport = new SerialPort("COM1", 57600, Parity.None, 8, StopBits.One);
comport.Open();byte[] data = new byte[16];
comport.Write(data, 0, 16); // This func gives the ExceptionThe details for the exception is "The I/O operation has been aborted because of either a thread exit or an application request". How and why a
write
function can give an exception? Any ideas will be appreciated :) zafer -
Hello everyone, I am trying to send an array of bytes using the serial port and I get a strange I/O exception. In my opinion there is no reason for the error. Here is the code;
SerialPort comport = new SerialPort("COM1", 57600, Parity.None, 8, StopBits.One);
comport.Open();byte[] data = new byte[16];
comport.Write(data, 0, 16); // This func gives the ExceptionThe details for the exception is "The I/O operation has been aborted because of either a thread exit or an application request". How and why a
write
function can give an exception? Any ideas will be appreciated :) zafer -
Hello everyone, I am trying to send an array of bytes using the serial port and I get a strange I/O exception. In my opinion there is no reason for the error. Here is the code;
SerialPort comport = new SerialPort("COM1", 57600, Parity.None, 8, StopBits.One);
comport.Open();byte[] data = new byte[16];
comport.Write(data, 0, 16); // This func gives the ExceptionThe details for the exception is "The I/O operation has been aborted because of either a thread exit or an application request". How and why a
write
function can give an exception? Any ideas will be appreciated :) zaferHi, is the code shown all part of a single method? are multiple threads involved? (including ThreadPool, BackgroundWorker, ...) did you use CheckForIllegalCrossThreadCalls anywhere? are you using one of the asynchronous events, such as DataReceived? where in the code is comport.Close()? you probably simplified your code before showing to such an extent that we cannot help you, unless you tell more and show more code. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
-
Hi, is the code shown all part of a single method? are multiple threads involved? (including ThreadPool, BackgroundWorker, ...) did you use CheckForIllegalCrossThreadCalls anywhere? are you using one of the asynchronous events, such as DataReceived? where in the code is comport.Close()? you probably simplified your code before showing to such an extent that we cannot help you, unless you tell more and show more code. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
Hi, I am getting a similar error. I am just trying to read from a serial COM port. It works fine. Then if I open and close the COM port a few times I get the following error when trying to open again:
The I/O operation has been aborted because of either a thread exit or an application request.
Starnagely enough the error is still tehre if I stop debugging (VisualStuio 2008, vb.net) and start again. The only way I can get it to work again is by opening and closing the port with hyperterminal! Here is my wrapper class:
Public Class clComPort
Private WithEvents comPort As System.IO.Ports.SerialPort
Private write As Boolean = True
Private bAllowSndRcv As Boolean = False
Private thRec As System.Threading.ThreadPublic Event WroteData(ByVal msg As String)
Public Event DataRecieved(ByVal msg As String)
Public Event COMError(ByVal msg As String)Public Function OpenPort(ByVal ComNum As Integer, ByVal baudRate As Integer, ByVal dataBits As Integer, _
ByVal parity As IO.Ports.Parity, ByVal stpBits As IO.Ports.StopBits) As Boolean
bAllowSndRcv = False
Try
'first check if the port is already open
'if its open then close it
If Not IsNothing(comPort) Then
Me.ClosePort()
End If
comPort = New System.IO.Ports.SerialPort()
'set the properties of our SerialPort Object
comPort.BaudRate = Integer.Parse(baudRate)
comPort.DataBits = Integer.Parse(dataBits)
comPort.StopBits = stpBits
comPort.Parity = parity
comPort.PortName = "COM" & ComNum
comPort.ReadTimeout = 500
comPort.WriteTimeout = 500'now open the port Dim b As Boolean = comPort.IsOpen comPort.Open() bAllowSndRcv = True 'return true Return True Catch ex As Exception bAllowSndRcv = False RaiseEvent COMError(ex.Message) Return False End Try
End Function
Public Sub ClosePort()
bAllowSndRcv = False
If IsNothing(comPort) Then Exit Sub
If comPort.IsOpen Then
RemoveHandler comPort.DataReceived, AddressOf comPort_DataReceived
comPort.Close()
End If
comPort.Dispose()
End Sub
Private Sub comPort_DataReceived(ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles comPort.DataReceived
'This event will Receive the data from the selected COM port..
If Not bAllowSndRcv Then Exit Sub
If e.Eve -
Hi, I am getting a similar error. I am just trying to read from a serial COM port. It works fine. Then if I open and close the COM port a few times I get the following error when trying to open again:
The I/O operation has been aborted because of either a thread exit or an application request.
Starnagely enough the error is still tehre if I stop debugging (VisualStuio 2008, vb.net) and start again. The only way I can get it to work again is by opening and closing the port with hyperterminal! Here is my wrapper class:
Public Class clComPort
Private WithEvents comPort As System.IO.Ports.SerialPort
Private write As Boolean = True
Private bAllowSndRcv As Boolean = False
Private thRec As System.Threading.ThreadPublic Event WroteData(ByVal msg As String)
Public Event DataRecieved(ByVal msg As String)
Public Event COMError(ByVal msg As String)Public Function OpenPort(ByVal ComNum As Integer, ByVal baudRate As Integer, ByVal dataBits As Integer, _
ByVal parity As IO.Ports.Parity, ByVal stpBits As IO.Ports.StopBits) As Boolean
bAllowSndRcv = False
Try
'first check if the port is already open
'if its open then close it
If Not IsNothing(comPort) Then
Me.ClosePort()
End If
comPort = New System.IO.Ports.SerialPort()
'set the properties of our SerialPort Object
comPort.BaudRate = Integer.Parse(baudRate)
comPort.DataBits = Integer.Parse(dataBits)
comPort.StopBits = stpBits
comPort.Parity = parity
comPort.PortName = "COM" & ComNum
comPort.ReadTimeout = 500
comPort.WriteTimeout = 500'now open the port Dim b As Boolean = comPort.IsOpen comPort.Open() bAllowSndRcv = True 'return true Return True Catch ex As Exception bAllowSndRcv = False RaiseEvent COMError(ex.Message) Return False End Try
End Function
Public Sub ClosePort()
bAllowSndRcv = False
If IsNothing(comPort) Then Exit Sub
If comPort.IsOpen Then
RemoveHandler comPort.DataReceived, AddressOf comPort_DataReceived
comPort.Close()
End If
comPort.Dispose()
End Sub
Private Sub comPort_DataReceived(ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles comPort.DataReceived
'This event will Receive the data from the selected COM port..
If Not bAllowSndRcv Then Exit Sub
If e.EveHi, this[^] says you shouldn't close and immediately reopen a serial port. suggestion: check the port settings; if no change needed simply clear in- and out-buffer and go on; otherwise include a sleep. BTW: why are you parsing integer parameters such as baudrate? :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
-
Hi, this[^] says you shouldn't close and immediately reopen a serial port. suggestion: check the port settings; if no change needed simply clear in- and out-buffer and go on; otherwise include a sleep. BTW: why are you parsing integer parameters such as baudrate? :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
Hi, Thanks for your answer. Ok, I will try the sleep. Interesting link you sent. What I am worried about is that once it's locked into the:
ERROR The I/O operation has been aborted because of either a thread exit or an application request.
Port Open Failed.it seems to stay there even if I start and stop my application again (both in dev environment in debug config, and using a released exe). The only way to unlock it is by opening and closing the port with hyperterminal. I wonder how I might be able to do this programatically? Is there any ways to run a batch file to open and close a hyperterminal? It's not the best solution but will have to do for a quick-fix... Thanks, Charlie
-
Hi, Thanks for your answer. Ok, I will try the sleep. Interesting link you sent. What I am worried about is that once it's locked into the:
ERROR The I/O operation has been aborted because of either a thread exit or an application request.
Port Open Failed.it seems to stay there even if I start and stop my application again (both in dev environment in debug config, and using a released exe). The only way to unlock it is by opening and closing the port with hyperterminal. I wonder how I might be able to do this programatically? Is there any ways to run a batch file to open and close a hyperterminal? It's not the best solution but will have to do for a quick-fix... Thanks, Charlie
I seem to have solved the issue by modifying my ClosePort sub to:
Public Sub ClosePort()
bAllowSndRcv = False
If IsNothing(comPort) Then Exit Sub
If comPort.IsOpen Then
comPort.DiscardOutBuffer()
RemoveHandler comPort.DataReceived, AddressOf comPort_DataReceived
Threading.Thread.Sleep(200)
comPort.Close()
End If
End Sub