当前位置:编程学习 > C#/ASP.NET >>

vb.net调用api函数写串口程序的问题

机器上没有串口,虚拟串口软件VSPD增加的虚拟串口,编译环境VS2010(系统XP Sp3),在vb.net下调用api函数写串口程序,createfile是可以打开串口的,但是settimeouts的时候出现超时错误,求教大神~~    
Ps:我serialport写的程序是可以在虚拟串口下正常使用的。
程序如下:
Private Function Init_Com(ByVal ComNumber As String, ByVal Comsetting As String) As Boolean
        Try
            Dim ComSetup As DCB
            Dim Answer As COMSTAT
            Dim Start As COMSTAT
            Dim RetBytes As Long
            Dim retval As Long
            Dim CtimeOut As COMMTIMEOUTS
            Dim BarDCB As DCB
            Dim sa As SECURITY_ATTRIBUTES

            ComNum = CreateFile(ComNumber, &HC0000000, 0, 0, &H3, 0, 0)
            If -1 = ComNum Then
                MessageBox.Show("端口" & ComNumber & "无效,请正确设置")
                Init_Com = False
                Exit Function
            End If
            '超时
            CtimeOut.ReadIntervalTimeout = 20
            CtimeOut.ReadTotalTimeoutConstant = 1
            CtimeOut.ReadTotalTimeoutMultiplier = 1
            CtimeOut.WriteTotalTimeoutConstant = 10
            CtimeOut.WriteTotalTimeoutMultiplier = 1
            retval = SetCommTimeouts(ComNum, CtimeOut)      '返回-1,出错误了!!!!!!!!!!!
            If retval = -1 Then
                retval = GetLastError()
                MessageBox.Show("端口超时设定无效" & ComNumber & "错误" & retval)
                retval = CloseHandle(ComNum)
                Init_Com = False
                Exit Function
            End If

            retval = BuildCommDCB(Comsetting, BarDCB)
            If -1 = retval Then
                retval = GetLastError()
                MessageBox.Show("无效设备DCB块" & Comsetting & "错误:" & retval)
                retval = CloseHandle(ComNum)
                Init_Com = False
                Exit Function
            End If

            retval = SetCommState(ComNum, BarDCB)
            If -1 = retval Then
                retval = GetLastError()
                MessageBox.Show("无效设备DCB块" & Comsetting & "错误" & retval)
                retval = CloseHandle(ComNum)
                Init_Com = False
                Exit Function
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Function
补充:.NET技术 ,  VB.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,