[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[转帖]在VB中利用API进行串口通信

上一篇:[备忘]VB串口通信,遭遇SetCommState总返回False…………
下一篇:[备忘]SQL Server 2005 中创建新的维护计划时出现错误消息:“创建维护计划失败”

添加日期:2011/9/8 2:19:06 快速返回   返回列表 阅读5600次
原帖:http://blog.csdn.net/lyserver/article/details/4153335

啥也不说了,眼泪哗哗的…………

http://www.silabs.com/products/mcu/Pages/USBtoUARTBridgeVCPDrivers.aspx
开始用MSCOMM32.OCX控件写了个串口程序,
谁知道CP210x_VCP_Win_XP_S2K3_Vista_7.exe这驱动(把USB虚拟出一个串口来),对MSCOMM32.OCX支持的不好。
一会支持,一会不支持的,搞了个最新的6.4版装上也不行。

搜了几个小时,转念一想,那就不用MSCOMM32.OCX了,直接API操作串口吧。
搜得此文,复制过来就能用,关键时刻救了我呀~~~~~
严重谢谢作者~~~~


'* ******************************************************* *  
'*    程序名称:basComm.bas  
'*    程序功能:在VB中利用API进行串口通信  
'*    作者:lyserver  
'*    联系方式:http://blog.csdn.net/lyserver  
'* ******************************************************* *  
Option Explicit  
Option Base 0  
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long  
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long  
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long  
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long  
Private Const GENERIC_READ = &H80000000  
Private Const GENERIC_WRITE = &H40000000  
Private Const OPEN_EXISTING = 3  
Private Const INVALID_HANDLE_VALUE = -1  
  
Private Declare Function GetCommState Lib "kernel32" (ByVal nCid As Long, lpDCB As DCB) As Long  
Private Declare Function SetCommState Lib "kernel32" (ByVal hCommDev As Long, lpDCB As DCB) As Long  
Private Declare Function SetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long  
Private Declare Function SetupComm Lib "kernel32" (ByVal hFile As Long, ByVal dwInQueue As Long, ByVal dwOutQueue As Long) As Long  
Private Declare Function PurgeComm Lib "kernel32" (ByVal hFile As Long, ByVal dwFlags As Long) As Long  
Private Const PURGE_TXABORT = &H1     '  Kill the pending/current writes to the comm port.  
Private Const PURGE_RXABORT = &H2     '  Kill the pending/current reads to the comm port.  
Private Const PURGE_TXCLEAR = &H4     '  Kill the transmit queue if there.  
Private Const PURGE_RXCLEAR = &H8     '  Kill the typeahead buffer if there.  
Private Type DCB  
        DCBlength As Long  
        BaudRate As Long  
        fBitFields As Long 'See Comments in Win32API.Txt  
        wReserved As Integer  
        XonLim As Integer  
        XoffLim As Integer  
        ByteSize As Byte  
        Parity As Byte  
        StopBits As Byte  
        XonChar As Byte  
        XoffChar As Byte  
        ErrorChar As Byte  
        EOFChar As Byte  
        EvtChar As Byte  
        wReserved1 As Integer 'Reserved; Do Not Use  
End Type  
Private Type COMMTIMEOUTS  
        ReadIntervalTimeout As Long  
        ReadTotalTimeoutMultiplier As Long  
        ReadTotalTimeoutConstant As Long  
        WriteTotalTimeoutMultiplier As Long  
        WriteTotalTimeoutConstant As Long  
End Type  
  
Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long  
  
'串口操作演示  
Sub Main()  
    Dim hComm As Long  
    Dim szTest As String  
      
    '打开串口1  
    hComm = OpenComm(1)  
      
    If hComm <> 0 Then  
        '设置串口通讯参数  
        SetCommParam hComm  
          
        '设置串口超时  
        SetCommTimeOut hComm, 2, 3  
          
        '向串口写入字符串123  
        szTest = "123"  
        WriteComm hComm, StringToBytes(szTest)  
          
        '读串口  
        szTest = BytesToString(ReadComm(hComm))  
        Debug.Print szTest  
          
        '关闭串口  
        CloseComm hComm  
    End If  
End Sub  
  
'打开串口  
Function OpenComm(ByVal lComPort As Long) As Long  
    Dim hComm As Long  
      
    hComm = CreateFile("COM" & lComPort, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)  
    If hComm = INVALID_HANDLE_VALUE Then  
        OpenComm = 0  
    Else  
        OpenComm = hComm  
    End If  
End Function  
  
'关闭串口  
Sub CloseComm(hComm As Long)  
    CloseHandle hComm  
    hComm = 0  
End Sub  
  
'读串口  
Function ReadComm(ByVal hComm As Long) As Byte()  
    Dim dwBytesRead As Long  
    Dim BytesBuffer() As Byte  
      
    ReDim BytesBuffer(4095)  
    ReadFile hComm, BytesBuffer(0), UBound(BytesBuffer) + 1, dwBytesRead, 0  
    If dwBytesRead > 0 Then  
        ReDim Preserve BytesBuffer(dwBytesRead)  
        ReadComm = BytesBuffer  
    End If  
End Function  
  
'写串口  
Function WriteComm(ByVal hComm As Long, BytesBuffer() As Byte) As Long  
    Dim dwBytesWrite  
      
    If SafeArrayGetDim(BytesBuffer) = 0 Then Exit Function  
    WriteFile hComm, BytesBuffer(0), UBound(BytesBuffer) + 1, dwBytesWrite, 0  
    WriteComm = dwBytesWrite  
End Function  
  
'设置串口通讯参数  
Function SetCommParam(ByVal hComm As Long, Optional ByVal lBaudRate As Long = 9600, _  
        Optional ByVal cByteSize As Byte = 8, Optional ByVal cStopBits As Byte = 0, _  
        Optional ByVal cParity As Byte = 0, Optional ByVal cEOFChar As Long = 26) As Boolean  
          
    Dim dc As DCB  
    If hComm = 0 Then Exit Function  
      
    If GetCommState(hComm, dc) Then  
        dc.BaudRate = lBaudRate  
        dc.ByteSize = cByteSize  
        dc.StopBits = cStopBits  
        dc.Parity = cParity  
        dc.EOFChar = cEOFChar  
          
        SetCommParam = CBool(SetCommState(hComm, dc))  
    End If  
End Function  
  
'设置串口超时  
Function SetCommTimeOut(ByVal hComm As Long, Optional ByVal dwReadTimeOut As Long = 2, _  
        Optional ByVal dwWriteTimeOut As Long = 3) As Boolean  
          
    Dim ct As COMMTIMEOUTS  
    If hComm = 0 Then Exit Function  
      
    ct.ReadIntervalTimeout = dwReadTimeOut '读操作时,字符间超时  
    ct.ReadTotalTimeoutMultiplier = dwReadTimeOut '读操作时,每字节超时  
    ct.ReadTotalTimeoutConstant = dwReadTimeOut '读操作时,固定超时(总超时=每字节超时*字节数+固定超时)  
    ct.WriteTotalTimeoutMultiplier = dwWriteTimeOut '写操作时,每字节超时  
    ct.WriteTotalTimeoutConstant = dwWriteTimeOut '写操作时,固定超时(总超时=每字节超时*字节数+固定超时)  
      
    SetCommTimeOut = CBool(SetCommTimeouts(hComm, ct))  
End Function  
  
'设置串口读写缓冲区大小  
Function SetCommBuffer(ByVal hComm As Long, Optional ByVal dwBytesRead As Long = 1024, _  
        Optional ByVal dwBytesWrite As Long = 512) As Boolean  
      
    If hComm = 0 Then Exit Function  
    SetCommBuffer = CBool(SetupComm(hComm, dwBytesRead, dwBytesWrite))  
End Function  
  
'清空串口缓冲区  
Sub ClearComm(ByVal hComm As Long, Optional ByVal InBuffer As Boolean = True, Optional ByVal OutBuffer As Boolean = True)  
    If hComm = 0 Then Exit Sub  
    If InBuffer And OutBuffer Then '清空输入输出缓冲区  
        PurgeComm hComm, PURGE_TXABORT Or PURGE_RXABORT Or PURGE_TXCLEAR Or PURGE_RXCLEAR  
    ElseIf InBuffer Then '清空输入缓冲区  
        PurgeComm hComm, PURGE_RXABORT Or PURGE_RXCLEAR  
    ElseIf OutBuffer Then '清空输出缓冲区  
        PurgeComm hComm, PURGE_TXABORT Or PURGE_TXCLEAR  
    End If  
End Sub  
  
'辅助函数:BSTR字符串转换为CHAR字符串  
Function StringToBytes(ByVal szText As String) As Byte()  
    If Len(szText) > 0 Then  
        StringToBytes = StrConv(szText, vbFromUnicode)  
    End If  
End Function  
  
'辅助函数:CHAR字符串转换为BSTR字符串  
Function BytesToString(bytesText() As Byte) As String  
    If SafeArrayGetDim(bytesText) <> 0 Then  
        BytesToString = StrConv(bytesText, vbUnicode)  
    End If  
End Function  
  
'辅助函数:获得CHAR字符串长度  
Function Byteslen(bytesText() As Byte) As Long  
    If SafeArrayGetDim(bytesText) <> 0 Then  
        Byteslen = UBound(bytesText) + 1  
    End If  
End Function  

 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved