Article ID: 841700 - Last Review: November 16, 2007 - Revision: 1.4
BUG: You receive an "error reading comm device" 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
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.
The MSComm control uses the "return quickly" feature of the Windows COM port
driver. If you use RDP 5.2 for port redirection, the status of the "return
quickly" feature is set to pending. This setting affects the functionality of the MSComm control.
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:
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.
Click the Local Resources
tab.
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.
Click OK. The Log On to
Windows dialog box appears.
In the User name box, type your user
name.
In the Password box, type your
password, and then click OK to connect to the terminal
server.
On drive C of the terminal server, create a text file. Name
the text file Testfile.txt.
Type Hello World in the Testfile.txt
file.
Save the Testfile.txt file, and then close the
file.
Start Visual Basic 6.0 on the terminal server.
On the File menu, click New
Project. The New Project dialog box
appears.
Click Standard EXE, and then click
OK. By default, a form that is named Form1 is
created.
On the Project menu, click
References. The References – Project1 dialog
box appears.
Under Available References, click to
select the Microsoft Scripting Runtime library, and then click
OK.
On the Project menu, click
Components. The Components dialog box
appears.
Click the Controls tab.
On the Controls tab, click to select the
Microsoft Comm Control 6.0 control, and then click
OK.
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.
On the View menu, click
Code to view the code window.
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 = "C:\Testfile.txt"
'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 = "57600,N,8,1"
' 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
On the Run menu, click
Start. The Form1 form appears.
Click Command1. You receive the error
message that is mentioned in the "Symptoms" section.