Private Function OpenThePort(cPort as String, cBaud as String, cParity as String, cData as String, tStops asString) As Boolean ' Open the sub-process of the serial port Dim lResult as Long Dim lHandle as Long Dim DCB_COMM as DCB Dim cDCBConfig as String lHandle = CreateFile(cPort, GENERIC_READ Or GENERIC_WRITE, 0&, 0&, OPEN_EXISTING, 0&, 0&) If lHandle = -1 Then 'Failed to open the serial port OpenThePort = False MsgBox "The serial port may be occupied by another application!" lResult = CloseHandle (lHandle) 'Close the serial port first and then open it If lResult = 0 Then OpenThePort Exit Function End If End If cDCBConfig.band = 2400 'Set DCB cDCBConfig.parity = None cDCBConfig.data = 8 cDCBConfig.stop = 1 lResult = BuildCommDCB (cDCBConfig, DCB_COMM) 'Configure a DCB structure according to user settings If lResult = 0 Then OpenThePort = False MsgBox "Unable to create DCB device control block" Exit Function End If lResult = SetCommState (lHandle, DCB_Comm) 'Actually set the DCB of a serial port If lResult = 0 Then OpenThePort = False MsgBox "Unable to create DCB device control block" Exit Function End If OpenThePort = True End Function Private Sub SendHand ( ) 'Sub-process for sending handshake signals Dim Nchars As Long Static Readbuff As String * 1 Static Writebuff As String * 1 Dim lpDCB As DCB Dim lRet As Long Dim lHandle As Long Dim lpOverlapped As OVERLAPPED Dim RNum As Integer MsgBox "Please plug the card reader into serial port 2!", 48, "Prompt Window" lHandle = OpenThePort(COMM1,2400,None,8,1) lRet = PurgeComm(lHandle, 1) 'Purge the output buffer lRet = PurgeComm(lHandle, 0) 'Clear input buffer lRet = GetCommState (lHandle, lpDCB) 'Get the status of the communication port Shand: Writebuff$= Chr$(&H8F) lRet = WriteFile (lHandle,Writebuff$,1,Nchars,lpOverlapped) 'Send handshake signal into serial port buffer If lRet <= 0 Then MsgBox "Send operation error, card handshake signal was not sent successfully", 16 GoTo Shand 'Resend if unsuccessful Else GoTo Qtest End If GoTo Shand Qtest: Readbuff$ = " " 'Clear the buffer to empty Do While lHandle 'Loop query serial port RNum = 0 'Set the pointer of serial port reading times to 0 ReadAgain: lRet = ReadFile(lHandle, Readbuff$, 1, Nchars, lpOverlapped) If lRet < 0 Then MsgBox "Error reading response signal", 16 End If If lRet = 0 Then If RNum > 1000 Then 'Only read the serial port 1000 times to avoid an infinite loop MsgBox "The card is not properly inserted or the card is not connected to the serial port!" GoTo CloseP End If RNum = RNum + 1 GoTo ReadAgain End If If Hex$(Asc(Readbuff)) <> Hex$(&HFF) Then GoTo Shand 'If the return code is incorrect, return and continue to send handshake signals Else Label1.Caption = "The handshake signal is:" +Hex$(Asc(Readbuff$)) Msgbox "The handshake signal is correct and the connection is correct" GoTo CloseP End If Loop CloseP:lRet = CloseHandle( lHandle ) If lRet = 0 Then MsgBox "Serial communication port closed successfully", 48, "Prompt Window" End If End Sub It should be noted that when the PC communicates with the MCU system, the data in the MCU data storage area (RAM) is hexadecimal, and the data transmitted on the signal line is the binary form of the ASCII code of the hexadecimal number; while the ANSI code is used in the Windows system, and the ANSI code is only the same as the ASCII code in the first 126. That is, the string of ASCII codes of hexadecimal numbers received under Win95 can be converted to ANSI code first and then restored to hexadecimal numbers under Win95. Specifically: Code$=Hex$(Asc (Readbuff$)) In addition, due to the change in the data type of the 32-bit API function parameter, all integer parameters are converted to long integers (Long) to support 32-bit processing, which is especially true when setting the return value. 6 Conclusion The above hardware and software have achieved relatively ideal results in our practice. The software saves the hardware cost, and by adding handshake signals in the communication software of the PIC16F84 microcontroller system and the PC, the purpose of software data verification is achieved, and higher communication reliability is obtained. References 1 MICROCHIP CO. PIC16/17 MICROCO-NTROLLER DATA BOOK. 1995/1996 2 Li Dongxing et al. PIC16CXX Series MCU Application Design. Gaoqi Electronic Technology Co., Ltd., 1996.10 3 Darwin Boyle et al. Visual Basic 4 Developer's Guide. Beijing: Machinery Industry Press, 1997. |