locked
Why do I read the baudrate=0? _ wm6+vb RRS feed

  • Question

  • I use wm6+vb

    My code is as below.

    I had a question:

    I use function "GetCommState" to get the DCB's return data =True

    But

    Why did I read the baudrate=0 ?

    Could you suggest me any idea ?

    ------------------------------------------------------------

    Imports System.Runtime.InteropServices
    Public Class Form1
        Dim infileHandler As Long
        Dim home_DCB As New DCB()
        Dim home_comm As Boolean
        Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
            connect()
        End Sub

        Public Sub connect()
            Dim inPort As Short = 6
            infileHandler = CreateFile("COM" & inPort & ":", _
                                        &HC0000000, 0, 0, 3, 0, 0)
            Application.DoEvents()

            home_comm = GetCommState(infileHandler, home_DCB)
            'MsgBox(home_comm)
            MsgBox("baud=" & home_DCB.BaudRate)
        End Sub

        <DllImport("coredll.dll")> _
    Private Shared Function CreateFile(ByVal lpFileName As String, _
                                       ByVal dwDesiredAccess As Integer, _
                                       ByVal dwShareMode As Integer, _
                                       ByVal lpSecurityAttributes As Integer, _
                                       ByVal dwCreationDisposition As Integer, _
                                       ByVal dwFlagsAndAttributes As Integer, _
                                       ByVal hTemplateFile As Integer) As Integer
        End Function

        '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

        <DllImport("coredll.dll", EntryPoint:="GetCommState")> _
        Public Shared Function GetCommState(ByVal hFile As Integer, ByRef dcb As DCB) As Boolean
        End Function
    End Class

    Saturday, April 9, 2011 7:06 AM

Answers

  • you need to initialize the DCBLength parameter to the size of the DCB structure before calling GetCommState().

    http://msdn.microsoft.com/en-us/library/bb202767.aspx

    http://msdn.microsoft.com/en-us/library/aa932472.aspx

    -PaulH

    • Proposed as answer by Jesse Jiang Friday, April 22, 2011 3:50 AM
    • Marked as answer by Jesse Jiang Thursday, April 28, 2011 3:01 AM
    Sunday, April 10, 2011 6:46 AM

All replies

  • you need to initialize the DCBLength parameter to the size of the DCB structure before calling GetCommState().

    http://msdn.microsoft.com/en-us/library/bb202767.aspx

    http://msdn.microsoft.com/en-us/library/aa932472.aspx

    -PaulH

    • Proposed as answer by Jesse Jiang Friday, April 22, 2011 3:50 AM
    • Marked as answer by Jesse Jiang Thursday, April 28, 2011 3:01 AM
    Sunday, April 10, 2011 6:46 AM
  •  

    Hi,

     

    I'm very interested in hearing more about your issue, did you solve your problem?

    If so please share the solution with us and mark the right answers.

    If not please let me know and I will tray to help you find further solutions.

     

    Have a nice day!

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 13, 2011 8:52 AM
  • Thanks for your reply

    I will try it later,then post the my reult

    Thanks

    Wednesday, April 13, 2011 8:58 AM