????? ?????? ??? ????? ???????? ????????? ???????? Visual Basic .NET

?????? ????????? ?????? ?????????
???? ???????: 823179 - ??? ???????? ???? ????? ????? ??? ???????.
?????? ??? ????? Microsoft Visual Studio 2005 ?? ??? ???????? ???? 904795.
????? ???? | ?? ????

?? ??? ??????

??????

???? ??? ??????? ???? ????? ??? ????? ?????? ??? ??????? ????????? ?????? ?????? ??????? ????????? ???????? Microsoft Visual Basic .NET. ????? ??? ??????? ????? ??? ????? ????????? ???????? ???? ???? ???????? ???? ??? ???????? ?? ??? ???????.

???????

???? ??????? ??????? ??????? ?????? ??? ????? ?????? ???????? ?????? ? ??? ??????? ???? ???????:
  • Microsoft Windows Server 2003 ?? Microsoft Windows XP ?? ???? ??????? Microsoft Windows 2000
  • Visual Basic .NET
????? ??? ??????? ??? ??????? ??? ???????? ???????:
  • ??????? ?? Visual Basic .NET
  • ??????? ?????? ??????? ??????? ?? Visual Basic .NET

?????? ???? ?????? MSComm ?? Visual Basic .NET ?????? ??? ????? ???????

???? ???? ?? ???? ???? ?????? Microsoft .NET Framework ?? ?????? ??? ??????? ????????? ??????????, ????? ??????? ???? ?????? MSComm ?? Microsoft Visual Basic 6.0. ???? ???? ?????? MSComm ????????? ????????? ??????? ????? ?? ?? ???? ????? ??????? ?? ?????? ???????? ?? ???? ???? ??????. ?????? ???????? ????????? ????????? ???????? ???? ???? ??????? ???????:
  1. ??? ????? Microsoft Visual Studio .NET.
  2. ?? ??????? ???? ??? ??? ???? ?? ???? ??? ???????.
  3. ??? ????? Project ? ???? ??? ?????? Visual Basic.
  4. ??? ????? ? ???? ??? ???? ?????? ???????.
  5. ?? ?????? ?????? ???? MyConsoleApplication ??? ?? ???? ??? ?????.

    ?????????? ??? ????? Module1.vb.
  6. ???? ??? ?????? ?????? ??? MyConsoleApplication ??????? ?? ?? ???? ??? ????? ????.
  7. ???? ??? ????? ??????? COM ???? ??? Microsoft ??????? ?????? 6.0 ??? ??? ?????? ???? ??? ????? ?? ???? ??? ?????.

    ?????? ???????? ???? ???? MSComm ??? ????? ?????? COM ??? ??? ?? Microsoft Visual Basic 6.0 ??? ??? ????????? ???? ????? ??? Microsoft Visual Studio .NET ?????.

    ?????? ??? ???? ?? ????????? ??? ????? ???????? ??? ??????? ????? ???? Visual Basic 6.0 ?? Visual Studio .NET ? ???? ??? ??? ??????? ?????? ?????? ?? "????? ??????? ?? Microsoft:
    318597??? ??????? ????? ???? Visual Basic 6.0 ?? Visual Studio .NET
  8. ??????? ????? ?? 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
  9. ???? CRTL + F5 ?????? ??????? ?????? ???????. ??? ????? ????? ??????? ???????:
    ??? ?????? ????????.
    ????? ????? ???????? ??? ??????.
    ????? ??? ???????? ??????? ??? ?????? ????????...
    ????? ???????? ??????? ????? ?? ?????? ????????.
    ????? ?????? ????????.

?????? ??????? ??????? ??????? ????? ??????? ????? API Win32 ?? Visual Basic .NET ?????? ??????? ????????? ????????

?????? ????? ???? ??????? ???????:
  1. ??? ????? Microsoft Visual Studio .NET.
  2. ?? ??????? ???? ??? ??? ???? ?? ???? ??? ???????.
  3. ??? ????? Project ? ???? ??? ?????? Visual Basic.
  4. ??? ????? ? ???? ??? ???? ?????? ???????.
  5. ?? ???? ???? ??? ???? MyConsoleApplication ??? ?? ???? ??? ?????.

    ?????????? ??? ????? Module1.vb.
  6. ????? ????????? ???????? ??????? ??? Module1.vb ??? ????? ?????? ??????? 1 ?????? ???????:
    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
  7. ???? ?????? ???? ????? ???????? ??? ????? ?????? ?? Kernel32.dll

    ??????? ????? ?? ??? ?????? ?? ????? Visual Basic .NET ??????? ??? ??????? ????? ??? ????? ??????? ??????? ??? ????? ?? ??? ?????? ? ??? ?? ???? ?????? ??????? ???? ???? ??????? ??? ????? ?? ??? ??????. ?????? ????? ?? ?????? ???????? ???????? ??????? ??? Module1.vb ??? ??????? ?????? ??????? 1 ?????? ???????:
    '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
  8. ??? ?? ????? ?????? ??? ???? ?????? ?? ???? ??????, ??? ?????? ??? ???? ??? ?????? ??????? ?? ?? ?????? ????????? ??????. ?????? ????? ?? ?????? ???????? ???????? ??????? ????? ??????? ??? Module1.vb ??? ??????? ??????? Sub.

    ?????? ?????? ??????? ?? ???? LPT x ??? ????? ???? "??????? ?????? ???????". ?????? ????? ?????? ?????? "?????" ?? "????? ??????".
    ' 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()
    
  9. ?? ??????? ????? ? ???? ??? ???? ????.
  10. ?? ??????? ????? ? ???? ??? "????" ?????? ???????.

    ?? ????? ???? ?????? ?? ???? ??????:
    ?????? ??? COM1 ??? ?????? ????????
    ????? ???????? ??????? ??? COM1: ??????

    ????? ???????? ??????? ?? COM1: Serial Data
    ?????? ??? ???? LPT1 ??????

    ????? ???????? ??????? ??? LPT1: ??????

    ???? ????? ??????? ENTER ??????
    ?????????? Serial Data ???????? ???? ???? ??????? ?? ?????? ????????.
  11. ?????? ??????? ???? ??????? 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 ?????? ???????.

?????

????? ?? ?????????? ?? ?????? ????? ????? Network (MSDN) ?????? Microsoft ???????:
????? ?????????
http://msdn2.microsoft.com/en-us/library/aa363196.aspx

interoperating ?? ????????? ???????? ??? ????????
http://msdn2.microsoft.com/en-us/library/sd10k43k(vs.71).aspx

System.Runtime.InteropServices ????? ?????
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

"????? ????? ??????? Windows" ? "?????? ???????? ?????????? ??????
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

????? ?? ????????? ??? ????? ?????? AT ???? ??? ??? ??????? ?????? ?????? ?? "????? ??????? ?? Microsoft:
164660AT ???? ??? ????

???????

???? ???????: 823179 - ????? ??? ??????: 16/?? ??????/1428 - ??????: 3.4
????? ???
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
????? ??????: 
kbmt kbvs2005swept kbvs2005doesnotapply kbserial kbsdk kbkern32dll kbdll kbcommport kbapi kb32bitonly kbinterop kbconsole kbconfig kbappdev kbprogramming kbsample kbhowtomaster KB823179 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????823179

????? ???????

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com