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--------------------编程问答-------------------- 从简化代码和设计难度的角度出发,还是建议你采用MSComm控件实现串口通信,代码简单,且运行稳定。程序设计的主旨就是要:去繁求简 --------------------编程问答-------------------- 如果你一定要研究API,我给你提供一个VB6的例子:
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
http://download.csdn.net/detail/veron_04/1339593 --------------------编程问答--------------------
//...
if(hComm == INVALID_HANDLE_VALUE){
hComm = 0;
return FALSE;
}
// 设置捕获事件类型
SetCommMask(hComm, EV_RXFLAG);
// 设置输入输出缓冲区大小
SetupComm(hComm, 4096, 4096);
// 清缓输入输出冲区
PurgeComm(hComm, PURGE_RXABORT|PURGE_RXCLEAR|PURGE_TXABORT|PURGE_TXCLEAR);
// 通讯超时参数设置
COMMTIMEOUTS CommTimeOut;
CommTimeOut.ReadIntervalTimeout = 500; //设定读超时 MAXDWORD
CommTimeOut.ReadTotalTimeoutConstant = 500;
CommTimeOut.ReadTotalTimeoutMultiplier = 500;
CommTimeOut.WriteTotalTimeoutConstant = 500;
CommTimeOut.WriteTotalTimeoutMultiplier = 100; //设定写超时
SetCommTimeouts(hComm,&CommTimeOut); //设置超时
//...
补充:VB , API