|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Problem reading half-duplex virtual serial port with SerialPort clhardware. I have gotten it to work with devices that conect using both real and virtual serial ports, but I have a piece of hardware that the serialport object isn't working for. The non-working device is a barcode scanner that has a USB connection and a virtual serial port driver. The driver says it is only half duplex, so that may be the problem. I downloaded a utility program that monitors serial port traffic, and it can see the data coming in just fine, however when I try to read using the SerialPort object I get the following error: IOException was caught The I/O operation has been aborted because of either a thread exit or an application request. Since I was able to get it to work with other devices, I suspect the problem is with this particular device, probably the driver being half duplex. Are there settings I can use to overcome this? Hi,
Half-duplex is not the issue. What this means is that the device only sends data, it does not receive data from your application. So, anything that you might send will be ignored. That isn't the issue. >> The I/O operation has been aborted because of either a thread exit or anapplication request. << This normally means that there has been a DataReceived event (and a Read or ReadExisting call, for example) AFTER the port has been closed. The only time that I have seen this error is when the application is exited (and the port has been closed), but data is sent by the connected device BEFORE the SerialPort object has been disposed. I presume that this isn't the situation that you are seeing? Can you try something for me to see if this really is a device-specific issue. Please download the VS2005 terminal example from my homepage. Even though it has been written in VB, the issues will be the same for C#. Run it with your device, select the required port, open it and generate a scan. Let me know the result. Dick -- Richard Grier, MVP Hard & Software Author of Visual Basic Programmer's Guide to Serial Communications, Fourth Edition, ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March 2006. See www.hardandsoftware.net for details and contact information. Your program reads the scanner perfectly. I am not sure what I am doing
wrong, I just open the serialport object and call readline or readbyte and it crashes. I get the error on two different computers. I will look at the code again, but I'm not doing anything too complicated so I don't know whats happening. As I said before, I use the same method for reading other pieces of hardware and it works just fine. I figured out how to get it to work. I was just calling readline or readbyte
whenever I wanted to wait for something to come across the port, which apparently worked for some devices. However the better way is to have a function handle the serialport's datarecieved event and read the data inside the handler function. Thanks for your reply, the serial monitor code on your website was extremely helpful. Nate Hello! I got the same problem as yours, I have no idea with it. Could you help me please?
Imports System Imports System.Data Imports System.Windows.Forms Imports System.IO Imports System.IO.Ports Public Class Form1 Dim WithEvents port As New IO.Ports.SerialPort("COM1", 9600, Parity.None, 8, StopBits.One) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try port.ReceivedBytesThreshold = 1 If port.IsOpen = True Then port.Close() End If port.Open() lblError.Text = "COM1 port connected!" lblError.ForeColor = Color.Green TextBox1.Text = "0.000kg" Catch ex As Exception lblError.Text = ex.Message End Try End Sub Private Sub port_DataReceived1(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles port.DataReceived Try TextBox1.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {}) Catch ex As System.IO.IOException MsgBox(ex.Message) End Try End Sub '------------------------------------------------------ ' Delegate and subroutine to update the Textbox control '------------------------------------------------------ Public Delegate Sub myDelegate() Public Sub updateTextBox() Dim tmpStr As String Dim pos As Integer tmpStr = Trim(port.ReadLine) pos = InStr(tmpStr, "GS") If pos > 0 Then tmpStr = Trim(Mid(tmpStr, pos + 2)) If tmpStr <> Trim(TextBox1.Text) Then With TextBox1 .Text = tmpStr '.ScrollToCaret() End With End If End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click port.Close() Me.Close() End Sub End Class |
|||||||||||||||||||||||