Home > Software engineering >  Vb.net call API function to write a serial port application problems
Vb.net call API function to write a serial port application problems

Time:10-27

Machines without a serial port, virtual serial port software VSPD increase virtual serial port, compile environment VS2010 XP Sp3 (system), cut with API function to write a serial port on the vb.net application, createfile can open the serial port, but when settimeouts timeout error, refer to the great god ~ ~
Ps: I write serialport application can be normal use under the virtual serial port,
Procedure is As follows:
 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, & amp; HC0000000, 0, 0, & amp; H3, 0, 0)
If - 1=ComNum Then
MessageBox. Show (" port "& amp; ComNumber & amp; "Is invalid. Please correct Settings")
Init_Com=False
The Exit Function
End the If
'timeout
CtimeOut. ReadIntervalTimeout=20
CtimeOut. ReadTotalTimeoutConstant=1
CtimeOut. ReadTotalTimeoutMultiplier=1
CtimeOut. WriteTotalTimeoutConstant=10
CtimeOut. WriteTotalTimeoutMultiplier=1
Retval=SetCommTimeouts (ComNum CtimeOut) 'return 1, the wrong!!!!!!!!!!!!!!
If retval=1 Then
Retval=GetLastError ()
MessageBox. Show (" port timeouts is invalid "& amp; ComNumber & amp; "Error" & amp; Retval)
Retval=CloseHandle (ComNum)
Init_Com=False
The Exit Function
End the If

Retval=BuildCommDCB (Comsetting BarDCB)
If - 1=retval Then
Retval=GetLastError ()
MessageBox. Show (" invalid device DCB piece "& amp; Comsetting & amp; "Error:" & amp; Retval)
Retval=CloseHandle (ComNum)
Init_Com=False
The Exit Function
End the If

Retval=SetCommState (ComNum BarDCB)
If - 1=retval Then
Retval=GetLastError ()
MessageBox. Show (" invalid device DCB piece "& amp; Comsetting & amp; "Error" & amp; Retval)
Retval=CloseHandle (ComNum)
Init_Com=False
The Exit Function
End the If
Catch the ex As Exception
MessageBox. Show (ex. Message)
End the Try
End the Function

CodePudding user response:

From the perspective of simplify code and design difficulty, or advise you, serial communication is realized by using MSComm control code is simple, and stable running, is the main idea of program design is to: to numerous to Jane

CodePudding user response:

If you must study the API, I give you an example of a VB6:
http://download.csdn.net/detail/veron_04/1339593

CodePudding user response:

 
//...
If (hComm==INVALID_HANDLE_VALUE) {
HComm=0;
Return FALSE;
}
//set to capture the event type
SetCommMask (hComm EV_RXFLAG);

//set the input and output buffer size
SetupComm (hComm, 4096, 4096);

//clear slow input/output impact area
PurgeComm (hComm, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR);

//communication timeout parameter Settings
COMMTIMEOUTS CommTimeOut;
CommTimeOut. ReadIntervalTimeout=500;//set read timeout MAXDWORD
CommTimeOut. ReadTotalTimeoutConstant=500;
CommTimeOut. ReadTotalTimeoutMultiplier=500;
CommTimeOut. WriteTotalTimeoutConstant=500;
CommTimeOut. WriteTotalTimeoutMultiplier=100;//set to write timeout
SetCommTimeouts (hComm, & amp; CommTimeOut);//set the timeout
//...
  •  Tags:  
  • API
  • Related