Click here to Skip to main content
15,900,589 members
Home / Discussions / C#
   

C#

 
AnswerRe: xml Pin
PIEBALDconsult12-Oct-09 13:28
mvePIEBALDconsult12-Oct-09 13:28 
QuestionStrong Names Pin
Matt Cavanagh12-Oct-09 10:36
Matt Cavanagh12-Oct-09 10:36 
AnswerRe: Strong Names Pin
Not Active12-Oct-09 11:02
mentorNot Active12-Oct-09 11:02 
AnswerRe: Strong Names Pin
Jacobb Michael12-Oct-09 17:55
Jacobb Michael12-Oct-09 17:55 
QuestionSerial Port Strange Error! Pin
zafersavas12-Oct-09 10:28
zafersavas12-Oct-09 10:28 
AnswerRe: Serial Port Strange Error! Pin
I Believe In GOD12-Oct-09 10:33
I Believe In GOD12-Oct-09 10:33 
AnswerRe: Serial Port Strange Error! Pin
Luc Pattyn12-Oct-09 11:14
sitebuilderLuc Pattyn12-Oct-09 11:14 
GeneralRe: Serial Port Strange Error! Pin
charlie363620-Oct-09 5:25
charlie363620-Oct-09 5:25 
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.Thread

  Public 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.EventType = System.IO.Ports.SerialData.Chars Then
      thRec = New System.Threading.Thread(AddressOf ReceiveData)
      thRec.IsBackground = True
      thRec.Priority = System.Threading.ThreadPriority.Highest
      thRec.Start()
      thRec.Sleep(10)
    End If

  End Sub
  Private Sub ReceiveData()
    If Not bAllowSndRcv Then Exit Sub
    'Sub to Receive Data from the Serial Port, Will Run in a Thread
    Dim bRead, nRead As Integer
    ' Dim returnStr As String = ""
    Dim ascStr As String = ""

    bRead = comPort.BytesToRead 'Number of Bytes to read
    Dim cData(bRead - 1) As Byte

    comPort.Encoding = System.Text.Encoding.GetEncoding(65001)

    nRead = comPort.Read(cData, 0, bRead)  'Reading the Data
    For Each b As Byte In cData
      ascStr += Chr(b)        'Ascii String
      '  returnStr += Hex(b).PadLeft(2, "0")     'Hex String (Modified Padding, to intake compulsory 2 chars, mainly in case of 0)
    Next
    RaiseEvent DataRecieved(ascStr)
  End Sub
  Private Sub comPort_ErrorReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialErrorReceivedEventArgs) Handles comPort.ErrorReceived
    RaiseEvent COMError(e.EventType.ToString)
  End Sub
End Class



And here is the stack:

at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)    
at System.IO.Ports.InternalResources.WinIOError()    
at System.IO.Ports.SerialStream.InitializeDCB(Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Boolean discardNull)    
at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)    
at System.IO.Ports.SerialPort.Open()    
at COMPort.clComPort.OpenPort(Int32 ComNum, Int32 baudRate, Int32 dataBits, Parity parity, StopBits stpBits) in D:\Source\MiscVB.Net\COMPort\clComPort.vb:line 33"	String


Any ideas? Thanks,
Charlie
GeneralRe: Serial Port Strange Error! Pin
Luc Pattyn20-Oct-09 6:05
sitebuilderLuc Pattyn20-Oct-09 6:05 
GeneralRe: Serial Port Strange Error! Pin
charlie363620-Oct-09 22:30
charlie363620-Oct-09 22:30 
GeneralRe: Serial Port Strange Error! Pin
charlie363620-Oct-09 22:40
charlie363620-Oct-09 22:40 
QuestionMerge datasets having null values Pin
sri1312-Oct-09 9:08
sri1312-Oct-09 9:08 
AnswerRe: Merge datasets having null values Pin
I Believe In GOD12-Oct-09 10:01
I Believe In GOD12-Oct-09 10:01 
AnswerRe: Merge datasets having null values Pin
cjoki12-Oct-09 10:01
cjoki12-Oct-09 10:01 
QuestionCheck Box in Header Column of DataGridview ? Pin
Jacobb Michael12-Oct-09 5:49
Jacobb Michael12-Oct-09 5:49 
AnswerRe: Check Box in Header Column of DataGridview ? Pin
stancrm12-Oct-09 5:52
stancrm12-Oct-09 5:52 
Questionvs 2005 c#, need guidance on recording command line arguments to an msi (microsoft installer) Pin
cjoki12-Oct-09 5:22
cjoki12-Oct-09 5:22 
AnswerRe: vs 2005 c#, need guidance on recording command line arguments to an msi (microsoft installer) Pin
Not Active12-Oct-09 5:53
mentorNot Active12-Oct-09 5:53 
GeneralRe: vs 2005 c#, need guidance on recording command line arguments to an msi (microsoft installer) Pin
cjoki12-Oct-09 7:56
cjoki12-Oct-09 7:56 
GeneralRe: vs 2005 c#, need guidance on recording command line arguments to an msi (microsoft installer) Pin
cjoki12-Oct-09 9:44
cjoki12-Oct-09 9:44 
QuestionConvert Stream as an Excel Pin
satsumatable12-Oct-09 5:15
satsumatable12-Oct-09 5:15 
AnswerRe: Convert Stream as an Excel Pin
Md. Marufuzzaman12-Oct-09 5:38
professionalMd. Marufuzzaman12-Oct-09 5:38 
AnswerMy Vote of 1 Pin
Keith Barrow12-Oct-09 6:44
professionalKeith Barrow12-Oct-09 6:44 
AnswerRe: Convert Stream as an Excel Pin
Dave Kreskowiak12-Oct-09 6:57
mveDave Kreskowiak12-Oct-09 6:57 
QuestionRetrieve data from AR REmedy ODBC server Pin
Praveen Raghuvanshi12-Oct-09 4:13
professionalPraveen Raghuvanshi12-Oct-09 4:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.