您的位置:首页 > 编程语言 > VB

如何使用 Visual Basic .NET 访问串行端口和并行端口

2010-03-24 11:20 537 查看
http://support.microsoft.com/kb/823179/zh-cn

本文分步介绍了如何使用 Microsoft Visual Basic .NET 访问串行端口和并行端口。本文还提供了一个代码示例来说明本文中讨论的概念。

要求

下面列出了推荐使用的硬件、软件、网络基础设施以及所需的 Service Pack:

Microsoft Windows Server 2003、Microsoft Windows XP 或 Microsoft Windows 2000
Visual Basic .NET

本文假定您熟悉下列主题:

利用 Visual Basic .NET 编程
Visual Basic .NET 中的平台调用服务

使用 Visual Basic .NET 中的 MSComm 控件访问串行端口

由于不存在 Microsoft .NET Framework 类来访问可连接到您的计算机的通信资源,您可以使用 Microsoft Visual Basic 6.0 中的
MSComm 控件。MSComm 控件支持通过串行端口传输和接收数据,从而能够为应用程序提供串行通信。要使用调制解调器实现基本串行通信,请按照下列步骤操作:

启动 Microsoft Visual Studio .NET。
在“文件”菜单上,指向“新建”,然后单击“项目”。
在“项目类型”下,单击“Visual Basic 项目”。
在“模板”下,单击“控制台应用程序”。
在“名称”框中,键入 MyConsoleApplication,然后单击“确定”。

默认情况下,系统会创建 Module1.vb。
右键单击“MyConsoleApplication”项目,然后单击“添加引用”。
单击“COM”选项卡,单击“组件名称”下的“Microsoft Comm Control 6.0”,单击“选择”,然后单击“确定”。

注意:要使用 MSComm 控件,必须在安装了 Microsoft Visual Studio .NET 的计算机上安装 Microsoft Visual Basic 6.0 的相关 COM 组件。

有关在 Visual Basic .NET 中使用 Visual Basic 6.0 控件时的许可问题的更多信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
318597
(http://support.microsoft.com/kb/318597/ ) 在 Visual Studio 2005 或 Visual Studio .NET 中使用 Visual Basic 6.0 控件时收到“You do not have a license to use this control”(您未获授权,无法使用此控件)错误消息

使用下面的代码示例替换 Module1.vb 中的代码。
Imports MSCommLib

Module Module1

Sub Main()
'New a MSComm control
Dim MSComm1 As MSComm
MSComm1 = New MSComm
' Buffer to hold input string.
Dim Buffer As String
' Use the COM1 serial port.
MSComm1.CommPort = 1
' 9600 baud, no parity, 8 data, and 1 stop bit.
MSComm1.Settings = "9600,N,8,1"
' Tell the control to read the whole buffer when Input is used.
MSComm1.InputLen = 0
' Open the serial port.
MSComm1.PortOpen = True
Console.WriteLine("Open the serial port.")
' Tell the control to make the Input property return text data.
MSComm1.InputMode() = InputModeConstants.comInputModeText
'Clear the receive buffer.
MSComm1.InBufferCount() = 0
' Send the attention command to the modem.
MSComm1.Output = "ATV1Q0" & Chr(13)
Console.WriteLine("Send the attention command to the modem.")
Console.WriteLine("Wait for the data to come back to the serial port...")
' Make sure that the modem responds with "OK".
' Wait for the data to come back to the serial port.
Do
Buffer = Buffer & MSComm1.Input
Loop Until InStr(Buffer, "OK" & vbCrLf)
' Read the "OK" response data in the serial port.
' Close the serial port.
Console.WriteLine("Read the OK response data in the serial port.")
MSComm1.PortOpen = False
Console.WriteLine("Close the serial port.")
End Sub

End Module


按 Ctrl+F5 生成和运行该项目。您会收到以下输出消息:
打开串行端口。

将 attention 命令发送至调制解调器。

等待数据返回至串行端口...

读取串行端口中的 OK 响应数据。

关闭串行端口。

使用平台调用服务调用 Visual Basic .NET 中的 Win32 API 函数以访问串行端口和并行端口

为此,请按照下列步骤操作:

启动 Microsoft Visual Studio .NET。
在“文件”菜单上,指向“新建”,然后单击“项目”。
在“项目类型”下,单击“Visual Basic 项目”。
在“模板”下,单击“控制台应用程序”。
在“名称”文本框中,键入 MyConsoleApplication,然后单击“确定”。

默认情况下,系统会创建 Module1.vb。
在“Module Module1”语句前面向 Module1.vb 中添加如下代码:
Option Strict On

' Define a CommException class that inherits from the ApplicationException class,
' and then throw an object of type CommException when you receive an error message.
Class CommException
Inherits ApplicationException
Sub New(ByVal Reason As String)
MyBase.New(Reason)
End Sub
End Class


声明 Kernel32.dll 中的结构、常量和对外部函数的引用

要从托管 Visual Basic .NET 应用程序中调用非托管函数,必须声明对作为参数传递给非托管函数的结构的引用,而且必须声明作为参数传递给非托管函数的常量。要进行上述声明,请在
Module Module1 语句后面向 Module1.vb 中添加如下代码:
'Declare structures.
Public Structure DCB
Public DCBlength As Int32
Public BaudRate As Int32
Public fBitFields As Int32 'See Comments in Win32API.Txt
Public wReserved As Int16
Public XonLim As Int16
Public XoffLim As Int16
Public ByteSize As Byte
Public Parity As Byte
Public StopBits As Byte
Public XonChar As Byte
Public XoffChar As Byte
Public ErrorChar As Byte
Public EofChar As Byte
Public EvtChar As Byte
Public wReserved1 As Int16 'Reserved; Do Not Use
End Structure

Public Structure COMMTIMEOUTS
Public ReadIntervalTimeout As Int32
Public ReadTotalTimeoutMultiplier As Int32
Public ReadTotalTimeoutConstant As Int32
Public WriteTotalTimeoutMultiplier As Int32
Public WriteTotalTimeoutConstant As Int32
End Structure

'Declare constants.
Public Const GENERIC_READ As Int32 = &H80000000
Public Const GENERIC_WRITE As Int32 = &H40000000
Public Const OPEN_EXISTING As Int32 = 3
Public Const FILE_ATTRIBUTE_NORMAL As Int32 = &H80
Public Const NOPARITY As Int32 = 0
Public Const ONESTOPBIT As Int32 = 0

'Declare references to external functions.
Public Declare Auto Function CreateFile Lib "kernel32.dll" _
(ByVal lpFileName As String, ByVal dwDesiredAccess As Int32, _
ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As IntPtr, _
ByVal dwCreationDisposition As Int32, ByVal dwFlagsAndAttributes As Int32, _
ByVal hTemplateFile As IntPtr) As IntPtr

Public Declare Auto Function GetCommState Lib "kernel32.dll" (ByVal nCid As IntPtr, _
ByRef lpDCB As DCB) As Boolean

Public Declare Auto Function SetCommState Lib "kernel32.dll" (ByVal nCid As IntPtr, _
ByRef lpDCB As DCB) As Boolean

Public Declare Auto Function GetCommTimeouts Lib "kernel32.dll" (ByVal hFile As IntPtr, _
ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean

Public Declare Auto Function SetCommTimeouts Lib "kernel32.dll" (ByVal hFile As IntPtr, _
ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean

Public Declare Auto Function WriteFile Lib "kernel32.dll" (ByVal hFile As IntPtr, _
ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToWrite As Int32, _
ByRef lpNumberOfBytesWritten As Int32, ByVal lpOverlapped As IntPtr) As Boolean

Public Declare Auto Function ReadFile Lib "kernel32.dll" (ByVal hFile As IntPtr, _
ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToRead As Int32, _
ByRef lpNumberOfBytesRead As Int32, ByVal lpOverlapped As IntPtr) As Boolean

Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal hObject As IntPtr) As Boolean


在访问串行端口或并行端口之前,必须先获取相应端口的句柄,然后配置端口通信。为此,请在 Sub Main 语句后面向 Module1.vb 中添加以下初始化代码。

注意:要使用 LPTx 端口建立通信,必须停止后台打印程序服务。为此,请使用管理工具中的服务工具。
' Declare the local variables that you will use in the code.
Dim hSerialPort, hParallelPort As IntPtr
Dim Success As Boolean
Dim MyDCB As DCB
Dim MyCommTimeouts As COMMTIMEOUTS
Dim BytesWritten, BytesRead As Int32
Dim Buffer() As Byte

' Declare the variables to use for encoding.
Dim oEncoder As New System.Text.ASCIIEncoding
Dim oEnc As System.Text.Encoding = oEncoder.GetEncoding(1252)

' Convert String to Byte().
Buffer = oEnc.GetBytes("Test")
Try
' Access the serial port.
Console.WriteLine("Accessing the COM1 serial port")
' Obtain a handle to the COM1 serial port.
hSerialPort = CreateFile("COM1", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, _
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
' Verify that the obtained handle is valid.
If hSerialPort.ToInt32 = -1 Then
Throw New CommException("Unable to obtain a handle to the COM1 port")
End If
' Retrieve the current control settings.
Success = GetCommState(hSerialPort, MyDCB)
If Success = False Then
Throw New CommException("Unable to retrieve the current control settings")
End If
' Modify the properties of the retrieved DCB structure as appropriate.
' WARNING: Make sure to modify the properties according to their supported values.
MyDCB.BaudRate = 9600
MyDCB.ByteSize = 8
MyDCB.Parity = NOPARITY
MyDCB.StopBits = ONESTOPBIT
' Reconfigure COM1 based on the properties of the modified DCB structure.
Success = SetCommState(hSerialPort, MyDCB)
If Success = False Then
Throw New CommException("Unable to reconfigure COM1")
End If
' Retrieve the current time-out settings.
Success = GetCommTimeouts(hSerialPort, MyCommTimeouts)
If Success = False Then
Throw New CommException("Unable to retrieve current time-out settings")
End If
' Modify the properties of the retrieved COMMTIMEOUTS structure as appropriate.
' WARNING: Make sure to modify the properties according to their supported values.
MyCommTimeouts.ReadIntervalTimeout = 0
MyCommTimeouts.ReadTotalTimeoutConstant = 0
MyCommTimeouts.ReadTotalTimeoutMultiplier = 0
MyCommTimeouts.WriteTotalTimeoutConstant = 0
MyCommTimeouts.WriteTotalTimeoutMultiplier = 0
' Reconfigure the time-out settings, based on the properties of the modified COMMTIMEOUTS structure.
Success = SetCommTimeouts(hSerialPort, MyCommTimeouts)
If Success = False Then
Throw New CommException("Unable to reconfigure the time-out settings")
End If
' Write data to COM1.
Console.WriteLine("Writing the following data to COM1: Test")
Success = WriteFile(hSerialPort, Buffer, Buffer.Length, BytesWritten, IntPtr.Zero)
If Success = False Then
Throw New CommException("Unable to write to COM1")
End If
' Read data from COM1.
Success = ReadFile(hSerialPort, Buffer, BytesWritten, BytesRead, IntPtr.Zero)
If Success = False Then
Throw New CommException("Unable to read from COM1")
End If
Catch ex As Exception
Console.WriteLine(Ex.Message)
Finally
' Release the handle to COM1.
Success = CloseHandle(hSerialPort)
If Success = False Then
Console.WriteLine("Unable to release handle to COM1")
End If
End Try

Try
' Parallel port.
Console.WriteLine("Accessing the LPT1 parallel port")
' Obtain a handle to the LPT1 parallel port.
hParallelPort = CreateFile("LPT1", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, _
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
' Verify that the obtained handle is valid.
If hParallelPort.ToInt32 = -1 Then
Throw New CommException("Unable to obtain a handle to the LPT1 port")
End If
' Retrieve the current control settings.
Success = GetCommState(hParallelPort, MyDCB)
If Success = False Then
Throw New CommException("Unable to retrieve the current control settings")
End If
' Modify the properties of the retrieved DCB structure as appropriate.
' WARNING: Make sure to modify the properties according to their supported values.
MyDCB.BaudRate = 9600
MyDCB.ByteSize = 8
MyDCB.Parity = NOPARITY
MyDCB.StopBits = ONESTOPBIT
' Reconfigure LPT1 based on the properties of the modified DCB structure.
Success = SetCommState(hParallelPort, MyDCB)
If Success = False Then
Throw New CommException("Unable to reconfigure LPT1")
End If
' Retrieve the current time-out settings.
Success = GetCommTimeouts(hParallelPort, MyCommTimeouts)
If Success = False Then
Throw New CommException("Unable to retrieve current time-out settings")
End If
' Modify the properties of the retrieved COMMTIMEOUTS structure as appropriate.
' WARNING: Make sure to modify the properties according to their supported values.
MyCommTimeouts.ReadIntervalTimeout = 0
MyCommTimeouts.ReadTotalTimeoutConstant = 0
MyCommTimeouts.ReadTotalTimeoutMultiplier = 0
MyCommTimeouts.WriteTotalTimeoutConstant = 0
MyCommTimeouts.WriteTotalTimeoutMultiplier = 0
' Reconfigure the time-out settings, based on the properties of the modified COMMTIMEOUTS structure.
Success = SetCommTimeouts(hParallelPort, MyCommTimeouts)
If Success = False Then
Throw New CommException("Unable to reconfigure the time-out settings")
End If
' Write data to LPT1.
' Note: You cannot read data from a parallel port by calling the ReadFile function.
Console.WriteLine("Writing the following data to LPT1: Test")
Success = WriteFile(hParallelPort, Buffer, Buffer.Length, BytesWritten, IntPtr.Zero)
If Success = False Then
Throw New CommException("Unable to write to LPT1")
End If
Catch ex As Exception
Console.WriteLine(Ex.Message)
Finally
' Release the handle to LPT1.
Success = CloseHandle(hParallelPort)
If Success = False Then
Console.WriteLine("Unable to release handle to LPT1")
End If
End Try

Console.WriteLine("Press ENTER to quit")
Console.ReadLine()


在“生成”菜单上,单击“生成解决方案”。
在“调试”菜单上,单击“启动”以运行该应用程序。

您可能会在控制台中看到下面的文本:
正在访问 COM1 串行端口

正在将下面的数据写入 COM1 中:测试

从 COM1 中读取下面的数据:串行数据

正在访问 LPT1 并行端口

正在将下面的数据写入 LPT1 中:测试

按 Enter 可退出
注意:串行数据表示从串行端口读取的数据。
要关闭该应用程序,请在控制台中按 Enter。

代码示例列表 (Module1.vb)

在使用以下代码示例之前,请将 COM1 替换为串行端口的名称,并将 LPT1 替换为并行端口的名称。

注意:只有在将串行和并行设备连接到计算机上相应的端口时,下面的代码才有效。如果在不连接这些设备的情况下运行程序,该程序将无限期等待下去。
Option Strict On

' Define a CommException class that inherits from the ApplicationException class.
' Then throw an object of type CommException when you receive an error message.
Class CommException
Inherits ApplicationException
Sub New(ByVal Reason As String)
MyBase.New(Reason)
End Sub
End Class

Module Module1
'Declare structures
Public Structure DCB
Public DCBlength As Int32
Public BaudRate As Int32
Public fBitFields As Int32
Public wReserved As Int16
Public XonLim As Int16
Public XoffLim As Int16
Public ByteSize As Byte
Public Parity As Byte
Public StopBits As Byte
Public XonChar As Byte
Public XoffChar As Byte
Public ErrorChar As Byte
Public EofChar As Byte
Public EvtChar As Byte
Public wReserved1 As Int16 'Reserved; Do Not Use
End Structure

Public Structure COMMTIMEOUTS
Public ReadIntervalTimeout As Int32
Public ReadTotalTimeoutMultiplier As Int32
Public ReadTotalTimeoutConstant As Int32
Public WriteTotalTimeoutMultiplier As Int32
Public WriteTotalTimeoutConstant As Int32
End Structure

'Declare constants.
Public Const GENERIC_READ As Int32 = &H80000000
Public Const GENERIC_WRITE As Int32 = &H40000000
Public Const OPEN_EXISTING As Int32 = 3
Public Const FILE_ATTRIBUTE_NORMAL As Int32 = &H80
Public Const NOPARITY As Int32 = 0
Public Const ONESTOPBIT As Int32 = 0

'Declare references to external functions.
Public Declare Auto Function CreateFile Lib "kernel32.dll" _
(ByVal lpFileName As String, ByVal dwDesiredAccess As Int32, _
ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As IntPtr, _
ByVal dwCreationDisposition As Int32, ByVal dwFlagsAndAttributes As Int32, _
ByVal hTemplateFile As IntPtr) As IntPtr

Public Declare Auto Function GetCommState Lib "kernel32.dll" (ByVal nCid As IntPtr, _
ByRef lpDCB As DCB) As Boolean

Public Declare Auto Function SetCommState Lib "kernel32.dll" (ByVal nCid As IntPtr, _
ByRef lpDCB As DCB) As Boolean

Public Declare Auto Function GetCommTimeouts Lib "kernel32.dll" (ByVal hFile As IntPtr, _
ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean

Public Declare Auto Function SetCommTimeouts Lib "kernel32.dll" (ByVal hFile As IntPtr, _
ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean

Public Declare Auto Function WriteFile Lib "kernel32.dll" (ByVal hFile As IntPtr, _
ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToWrite As Int32, _
ByRef lpNumberOfBytesWritten As Int32, ByVal lpOverlapped As IntPtr) As Boolean

Public Declare Auto Function ReadFile Lib "kernel32.dll" (ByVal hFile As IntPtr, _
ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToRead As Int32, _
ByRef lpNumberOfBytesRead As Int32, ByVal lpOverlapped As IntPtr) As Boolean

Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal hObject As IntPtr) As Boolean

Sub Main()

' Declare local variables that you will use in the code.
Dim hSerialPort, hParallelPort As IntPtr
Dim Success As Boolean
Dim MyDCB As DCB
Dim MyCommTimeouts As COMMTIMEOUTS
Dim BytesWritten, BytesRead As Int32
Dim Buffer() As Byte

' Declare variables to use for encoding.
Dim oEncoder As New System.Text.ASCIIEncoding
Dim oEnc As System.Text.Encoding = oEncoder.GetEncoding(1252)

' Convert String to Byte().
Buffer = oEnc.GetBytes("Test")

Try
' Serial port.
Console.WriteLine("Accessing the COM1 serial port")
' Obtain a handle to the COM1 serial port.
hSerialPort = CreateFile("COM1", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, _
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
' Verify that the obtained handle is valid.
If hSerialPort.ToInt32 = -1 Then
Throw New CommException("Unable to obtain a handle to the COM1 port")
End If
' Retrieve the current control settings.
Success = GetCommState(hSerialPort, MyDCB)
If Success = False Then
Throw New CommException("Unable to retrieve the current control settings")
End If
' Modify the properties of the retrieved DCB structure as appropriate.
' WARNING: Make sure to modify the properties according to their supported values.
MyDCB.BaudRate = 9600
MyDCB.ByteSize = 8
MyDCB.Parity = NOPARITY
MyDCB.StopBits = ONESTOPBIT
' Reconfigure COM1 based on the properties of the modified DCB structure.
Success = SetCommState(hSerialPort, MyDCB)
If Success = False Then
Throw New CommException("Unable to reconfigure COM1")
End If
' Retrieve the current time-out settings.
Success = GetCommTimeouts(hSerialPort, MyCommTimeouts)
If Success = False Then
Throw New CommException("Unable to retrieve current time-out settings")
End If
' Modify the properties of the retrieved COMMTIMEOUTS structure as appropriate.
' WARNING: Make sure to modify the properties according to their supported values.
MyCommTimeouts.ReadIntervalTimeout = 0
MyCommTimeouts.ReadTotalTimeoutConstant = 0
MyCommTimeouts.ReadTotalTimeoutMultiplier = 0
MyCommTimeouts.WriteTotalTimeoutConstant = 0
MyCommTimeouts.WriteTotalTimeoutMultiplier = 0
' Reconfigure the time-out settings, based on the properties of the modified COMMTIMEOUTS structure.
Success = SetCommTimeouts(hSerialPort, MyCommTimeouts)
If Success = False Then
Throw New CommException("Unable to reconfigure the time-out settings")
End If
' Write data to COM1.
Console.WriteLine("Writing the following data to COM1: Test")
Success = WriteFile(hSerialPort, Buffer, Buffer.Length, BytesWritten, IntPtr.Zero)
If Success = False Then
Throw New CommException("Unable to write to COM1")
End If
' Read data from COM1.
Success = ReadFile(hSerialPort, Buffer, BytesWritten, BytesRead, IntPtr.Zero)
If Success = False Then
Throw New CommException("Unable to read from COM1")
End If
Catch ex As Exception
Console.WriteLine(Ex.Message)
Finally
' Release the handle to COM1.
Success = CloseHandle(hSerialPort)
If Success = False Then
Console.WriteLine("Unable to release handle to COM1")
End If
End Try

Try
' Parallel port.
Console.WriteLine("Accessing the LPT1 parallel port")
' Obtain a handle to the LPT1 parallel port.
hParallelPort = CreateFile("LPT1", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, _
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
' Verify that the obtained handle is valid.
If hParallelPort.ToInt32 = -1 Then
Throw New CommException("Unable to obtain a handle to the LPT1 port")
End If
' Retrieve the current control settings.
Success = GetCommState(hParallelPort, MyDCB)
If Success = False Then
Throw New CommException("Unable to retrieve the current control settings")
End If
' Modify the properties of the retrieved DCB structure as appropriate.
' WARNING: Make sure to modify the properties according to their supported values.
MyDCB.BaudRate = 9600
MyDCB.ByteSize = 8
MyDCB.Parity = NOPARITY
MyDCB.StopBits = ONESTOPBIT
' Reconfigure LPT1 based on the properties of MyDCB.
Success = SetCommState(hParallelPort, MyDCB)
If Success = False Then
Throw New CommException("Unable to reconfigure LPT1")
End If
' Reconfigure LPT1 based on the properties of the modified DCB structure.
Success = GetCommTimeouts(hParallelPort, MyCommTimeouts)
If Success = False Then
Throw New CommException("Unable to retrieve current time-out settings")
End If
' Modify the properties of the retrieved COMMTIMEOUTS structure as appropriate.
' WARNING: Make sure to modify the properties according to their supported values.
MyCommTimeouts.ReadIntervalTimeout = 0
MyCommTimeouts.ReadTotalTimeoutConstant = 0
MyCommTimeouts.ReadTotalTimeoutMultiplier = 0
MyCommTimeouts.WriteTotalTimeoutConstant = 0
MyCommTimeouts.WriteTotalTimeoutMultiplier = 0
' Reconfigure the time-out settings, based on the properties of the modified COMMTIMEOUTS structure.
Success = SetCommTimeouts(hParallelPort, MyCommTimeouts)
If Success = False Then
Throw New CommException("Unable to reconfigure the time-out settings")
End If
' Write data to LPT1.
' Note: You cannot read data from a parallel port by calling the ReadFile function.
Console.WriteLine("Writing the following data to LPT1: Test")
Success = WriteFile(hParallelPort, Buffer, Buffer.Length, BytesWritten, IntPtr.Zero)
If Success = False Then
Throw New CommException("Unable to write to LPT1")
End If
Catch ex As Exception
Console.WriteLine(Ex.Message)
Finally
' Release the handle to LPT1.
Success = CloseHandle(hParallelPort)
If Success = False Then
Console.WriteLine("Unable to release handle to LPT1")
End If
End Try

Console.WriteLine("Press ENTER to quit")
Console.ReadLine()

End Sub

End Module


疑难解答

运行该应用程序时,您可能会看到下面的错误消息:

System.NullReferenceException:未将对象引用设置到对象的实例。

出现此错误消息的原因是函数声明不正确。如果声明中包含 ByVal 参数而不包含 ByRef 参数,则通常会出现此错误消息。

在调用 ReadFile 函数时,应用程序可能会无限期等待下去。如果将已检索的 COMMTIMEOUTS 结构中的读取超时设置为零,则通常会出现这种情况。要解决此问题,请适当修改
COMMTIMEOUTS 结构的属性。

参考
有关更多信息,请访问下面的 Microsoft Developer Network (MSDN) 网站: 通信资源http://msdn2.microsoft....

有关更多信息,请访问下面的 Microsoft Developer Network (MSDN) 网站:
通信资源

http://msdn2.microsoft.com/en-us/library/aa363196.aspx (http://msdn2.microsoft.com/en-us/library/aa363196.aspx)

与非托管代码交互操作

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconinteroperatingwithunmanagedcode.asp
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconinteroperatingwithunmanagedcode.asp)

System.Runtime.InteropServices 命名空间

http://msdn2.microsoft.com/en-us/library/system.runtime.interopservices(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.runtime.interopservices(vs.71).aspx)

DllImportAttribute 类

http://msdn2.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute(vs.71).aspx
(http://msdn2.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute(vs.71).aspx)

Windows API 和其他动态链接库

http://msdn2.microsoft.com/en-us/library/aa141322(office.10).aspx (http://msdn2.microsoft.com/en-us/library/aa141322(office.10).aspx)

了解句柄

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/modcore/html/deovrUnderstandingHandles.asp (http://msdn2.microsoft.com/en-us/library/aa141354(office.10).aspx)

MSComm 控件

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/comm98/html/vbobjcomm.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/comm98/html/vbobjcomm.asp)

有关 AT 调制解调器命令的更多信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
164660
(http://support.microsoft.com/kb/164660/ ) AT 调制解调器命令参考
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: