Microsoft KB Archive/841700

= BUG: You receive an &quot;error reading comm device&quot; error message when you try to read from a COM port that is on a remote computer by using the MSComm control in Visual Basic 6.0 =

Article ID: 841700

Article Last Modified on 11/16/2007

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Learning Edition, when used with:
 * Microsoft Windows Server 2003, Standard Edition (32-bit x86)

-





SYMPTOMS
When you try to perform a read operation on a COM port by using the MSComm control in Microsoft Visual Basic 6.0, you receive the following error message:

Run-time error '8020':

Error reading comm device

Note This problem occurs only if the following conditions are true:
 * You have connected to a terminal server by using Remote Desktop Protocol (RDP) 5.2 for port redirection.
 * The terminal server is running Microsoft Windows Server 2003.



CAUSE
The MSComm control uses the &quot;return quickly&quot; feature of the Windows COM port driver. If you use RDP 5.2 for port redirection, the status of the &quot;return quickly&quot; feature is set to pending. This setting affects the functionality of the MSComm control.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to reproduce the problem
Note Make sure that the COM1 port is enabled on your local computer and on the remote computer. Also, verify that you have RDP 5.2 on your local computer. To obtain RDP 5.2, visit the following Microsoft Web site:

http://www.microsoft.com/downloads/details.aspx?FamilyID=e2ff8fb5-97ff-47bc-bacc-92283b52b310&DisplayLang=en

 Connect a modem to the COM1 port of your local computer. Restart your local computer. Install the correct driver for your modem. Connect to a remote computer that is running Windows Server 2003 through Microsoft Terminal Services by using the RDP 5.2 protocol. To do this, follow these steps:  Click Start, click Run, type mstsc, and then click OK. The Remote Desktop Connection dialog box appears. In the Computer box, type the Internet Protocol (IP) address of the terminal server that is running Windows Server 2003 and that you want to connect to, and then click Options.</li> Click the Local Resources tab.</li> In the Local devices section, make sure that the Serial ports check box is selected, and then click Connect. The Remote Desktop Connection Security Warning dialog box appears.</li> Click OK. The Log On to Windows dialog box appears.</li> In the User name box, type your user name.</li> In the Password box, type your password, and then click OK to connect to the terminal server.</li></ol> </li> On drive C of the terminal server, create a text file. Name the text file Testfile.txt.</li> Type Hello World in the Testfile.txt file.</li> Save the Testfile.txt file, and then close the file.</li> Start Visual Basic 6.0 on the terminal server.</li> On the File menu, click New Project. The New Project dialog box appears.</li> Click Standard EXE, and then click OK. By default, a form that is named Form1 is created.</li> On the Project menu, click References. The References – Project1 dialog box appears.</li> Under Available References, click to select the Microsoft Scripting Runtime library, and then click OK.</li> On the Project menu, click Components. The Components dialog box appears.</li> Click the Controls tab.</li> <li>On the Controls tab, click to select the Microsoft Comm Control 6.0 control, and then click OK.</li> <li>Add an MSComm control and a command button to the Form1 form. By default, the MSComm1 MSComm control and the Command1 command button are added to the form.</li> <li>On the View menu, click Code to view the code window.</li> <li> In the code window, add the following code. Option Explicit Const HANDSHAKING_NONE = 0 Const INPUTMODE_TEXT = 0

Private Sub Command1_Click Dim FilePath As String Dim FileSystem As FileSystemObject Dim oFile As Object

'Make sure that you have the Testfile.txt file on drive C of your computer. FilePath = &quot;C:\Testfile.txt&quot;

'Create a new FileSystemObject object, and then open the Testfile.txt file for reading. Set FileSystem = New FileSystemObject Set oFile = FileSystem.OpenTextFile(FilePath, ForReading)

'Specify the communication port for the MSComm control. MSComm1.CommPort = 1 Call ReadWrite(oFile, MSComm1) End Sub

Sub ReadWrite(File As Object, MyPort As MSComm)

'This procedure performs the following tasks: '1.Configures and opens the MyPort port. '2.Reads the contents of the File object, and then stores the data in a buffer that is named OutBuffer. '3.Writes the data from the OutBuffer buffer to the MyPort port. '4.Reads the data from the MyPort port to a buffer that is named InBuffer.

Dim OutBuffer As Variant Dim InBuffer As Variant MyPort.Settings = &quot;57600,N,8,1&quot; ' Configure the port. MyPort.Handshaking = HANDSHAKING_NONE MyPort.EOFEnable = False MyPort.RThreshold = 0 MyPort.SThreshold = 0 MyPort.InputMode = INPUTMODE_TEXT ' Specify the buffer size. MyPort.InBufferSize = 1024 ' Open the port. MyPort.PortOpen = True ' Read the buffer and transmit each line over the COM port. Do       'Read each line of data from the file. OutBuffer = File.ReadLine ' Configure the port to read the whole buffer. MyPort.InputLen = 0 ' Write the data, and then read it. MyPort.Output = OutBuffer InBuffer = MyPort.Input Loop While (File.AtEndOfStream = False) ' Close the port. MyPort.PortOpen = False End Sub </li> <li>On the Run menu, click Start. The Form1 form appears.</li> <li>Click Command1. You receive the error message that is mentioned in the &quot;Symptoms&quot; section.</li></ol>

<div class="references_section">