Fail setting commstate - PullRequest
       21

Fail setting commstate

0 голосов
/ 31 мая 2018

Я не могу установить commstate, используя метод CreateFile.Я успешно открываю коммюник, но не могу установить команду commstate.Любое направление будет с благодарностью.

Код:

Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic
Public Class P2
Inherits MarshalByRefObject

'CreateFile
<DllImport("kernel32.dll")> Private Shared Function CreateFile _
      (<MarshalAs(UnmanagedType.LPStr)> ByVal lpFileName As String, _
       ByVal dwDesiredAccess As Long, _
       ByVal dwShareMode As Long, _
       ByVal lpSecurityAttributes As Long, _
       ByVal dwCreationDisposition As Long, _
       ByVal dwFlagsAndAttributes As Long, _
       ByVal hTemplateFile As Long) As Long
End Function
'GetCommState
<DllImport("kernel32.dll")> Private Shared Function GetCommState( _
    ByVal hCommDev As Long, ByRef lpDCB As dcbType) As Long
End Function
'ReadFile P2
<DllImport("kernel32.dll")> Private Shared Function ReadFile(ByVal hFile _
              As Long, ByVal lpBuffer As String, ByVal _
              nNumberOfBytesToRead As Long, lpNumberOfBytesRead _
              As Long, lpOverlapped As Long) As Boolean
End Function
'SetCommState
<DllImport("kernel32.dll")> Private Shared Function SetCommState(ByVal _
                             hCommDev As Long, lpDCB As dcbType) As Long
End Function
'WriteFile  
<DllImport("kernel32.dll")> Private Shared Function WriteFile( _
ByVal hFile As Long, ByVal lpBuffer As String, _
ByVal nNumberOfBytesToWrite As Long, _
ByRef lpNumberOfBytesWritten As Long, _
ByRef lpOverlapped As Long) As Boolean
End Function
'CloseHandle
<DllImport("kernel32.dll")> Private Shared Function CloseHandle( _
        ByVal hObject As Integer) As Integer
End Function
'GetLastError
<DllImport("kernel32.dll")> Private Shared Function GetLastError() As Integer
End Function
'SetupComm
<DllImport("kernel32.dll")> Private Shared Function SetupComm(ByVal hFile _
                            As Long, ByVal dwInQueue As Long, ByVal _
                            dwOutQueue As Long) As Long
End Function
'SetCommTimeouts
<DllImport("kernel32.dll")> Private Shared Function SetCommTimeouts(ByVal hFile As _
                            Long, lpCommTimeouts As COMMTIMEOUTS) As Long
End Function
'EscapeCommFunction
<DllImport("kernel32.dll")> Private Shared Function EscapeCommFunction(ByVal hFile As _
                             Long, ByVal dwFunc As Long) As Boolean
End Function
'PurgeComm
<DllImport("kernel32.dll")> Private Shared Function PurgeComm(ByVal hFile As Long, ByVal _
                             dwFlags As Long) As Long
End Function


'OVERLAPPED
<StructLayout(LayoutKind.Sequential)> Structure OVERLAPPED
    Public Internal As Long
    Public InternalHigh As Long
    Public offset As Long
    Public OffsetHigh As Long
    Public hEvent As Long
End Structure
'COMMTIMEOUTS
<StructLayout(LayoutKind.Sequential)> Structure COMMTIMEOUTS
    Public ReadIntervalTimeout As Long
    Public ReadTotalTimeoutMultiplier As Long
    Public ReadTotalTimeoutConstant As Long
    Public WriteTotalTimeoutMultiplier As Long
    Public WriteTotalTimeoutConstant As Long
End Structure
'dcbType
<StructLayout(LayoutKind.Sequential)> Structure dcbType
    Public DCBlength As Long
    Public BaudRate As Long
    Public Bits1 As Long
    Public wReserved As Integer
    Public XonLim As Integer
    Public XoffLim As Integer
    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 Integer
End Structure

Private timeouts As COMMTIMEOUTS        
'Private dcbPort As New DCB
Private DCB As dcbType                  


Private ByteStr As String
Private InverseBuffer As String
Private WorkByte As String

Private ATR As String
Private PreATR As String
Private PostATR As String
Private AtrLen

Private DATA As String
Private preDATA As String
Private postDATA As String

Private temp1
Private Temp2
Private Temp3
Private Temp4
Public Cpos As Integer
Public Npos As Integer
Public Byte_Value
Public byteLen As Integer
Private Nibble As Integer
Private Term_Count As Long

Private numRead As Long
Private icond As Boolean
Private hPort As Long
Private BufferIn As String
Private BufferIncount As Integer
Private Property Timer As Single
'Private InBuff As String
<VBFixedString(1)> Public InBuff As String



Private Const INVALID_HANDLE_VALUE = -1
Private Const OPEN_EXISTING = 3
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const PARITYSTRING = "NONE"
Private Const NOPARITY = 0
Private Const ODDPARITY = 1
Private Const EVENPARITY = 2
Private Const MARKPARITY = 3
Private Const SPACEPARITY = 4
Private Const ONESTOPBIT = 0
Private Const ONE5STOPBITS = 1
Private Const TWOSTOPBITS = 2

Public Const ERR_INVALIDPROPERTY = 31000
Public Const fBinary = &H1&
Public Const fParity = &H2&
Public Const fOutxCtsFlow = &H4&
Public Const fOutxDsrFlow = &H8&
Public Const fDtrControl = &H30&
Public Const fDsrSensitivity = &H40&
Public Const fTXContinueOnXoff = &H80&
Public Const fOutX = &H100&
Public Const fInX = &H200&
Public Const fErrorChar = &H400&
Public Const fNull = &H800&
Public Const fRtsControl = &H3000&
Public Const fAbortOnError = &H4000&
Public Const DTR_CONTROL_DISABLE = &H0
Public Const DTR_CONTROL_ENABLE = &H1
Public Const DTR_CONTROL_HANDSHAKE = &H2
Public Const RTS_CONTROL_DISABLE = &H0
Public Const RTS_CONTROL_ENABLE = &H1
Public Const RTS_CONTROL_HANDSHAKE = &H2
Public Const RTS_CONTROL_TOGGLE = &H3
Public Const ResetBaud = 9600
Public Const P2DataBaud = 19200
Private Const CLRRTS = 4
Private Const PURGE_RXCLEAR = &H8
Private Const PURGE_TXCLEAR = &H4

'Recieve the AnswerToRest
Public Sub ResetForATR()

    CloseCOMM()

    'Open ComPort Here
    hPort = CreateFile("COM1", GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)

    If hPort < 1 Then
        Throw New Exception("Can't open the comport! (Errorcode :" & GetLastError().ToString())
    End If
    If hPort = INVALID_HANDLE_VALUE Then
        MessageBox.Show(Port & " Error: invalid port or already in use.")
        Exit Sub
    End If
    'MessageBox.Show(Port + ":  OPENED!")

    icond = SetupComm(hPort, 8192, 1024)        'checked: SetupComm, icond, hPort
    timeouts.ReadTotalTimeoutConstant = 10
    timeouts.ReadTotalTimeoutMultiplier = 0
    timeouts.ReadIntervalTimeout = 0
    timeouts.WriteTotalTimeoutConstant = 100
    timeouts.WriteTotalTimeoutMultiplier = 0

    icond = SetCommTimeouts(hPort, timeouts)    'checked: SetCommTimeouts, icond, hPort, timeouts
    icond = GetCommState(hPort, DCB)            'checked: GetCommState, icond, hPort, DCB

    SetDCBits(fBinary, 1)
    SetDCBits(fParity, 0)
    SetDCBits(fOutxCtsFlow, 1)
    SetDCBits(fOutxDsrFlow, 1)
    SetDCBits(fDtrControl, DTR_CONTROL_ENABLE)
    SetDCBits(fDsrSensitivity, 1)
    SetDCBits(fTXContinueOnXoff, 0)
    SetDCBits(fOutX, 0)
    SetDCBits(fInX, 0)
    SetDCBits(fErrorChar, 0)
    SetDCBits(fNull, 0)
    SetDCBits(fDtrControl, DTR_CONTROL_HANDSHAKE)
    SetDCBits(fRtsControl, RTS_CONTROL_ENABLE)
    SetDCBits(fAbortOnError, 0)

    DCB.BaudRate = ResetBaud
    DCB.ByteSize = 8
    DCB.Parity = NOPARITY
    DCB.StopBits = ONESTOPBIT

    icond = SetCommState(hPort, DCB)            'checked: SetCommState, hPort, DCB, icond
    icond = SetCommTimeouts(hPort, timeouts)    'checked: SetCommTimeouts, hPort, timeouts, icond

    'init the ATR by dropping RTS
    icond = EscapeCommFunction(hPort, CLRRTS)   'checked; EscapeCommFunction, icond, hPort, CLRRTS
    'Wait(0.1)
    If icond = False Then
        MessageBox.Show("Error Setting COM State.") 'Failing Here........
        CloseHandle(hPort)
        Exit Sub

    End If
 MessageBox.Show("Made it to ReadATR!")
    'read input BufferIn to get ATR data
    ReadATR()
 End Sub

   Private Sub SetDCBits(Pos As Long, val As Integer)
    Dim ip As Integer
    Dim imul As Long
    Dim poz As Long

    imul = 1
    poz = Pos
    For ip = 1 To 32
        If (poz And 1) Then Exit For
        poz = poz / 2
        imul = imul * 2
    Next
        DCB.Bits1 = DCB.Bits1 And (Not Pos) Or (imul * val)
End Sub
...