为您推荐:
精华内容
最热下载
问答
  • 5星
    5.47MB zhangjin7422 2021-01-28 10:18:50
  • 5星
    1.45MB zhangjin7422 2021-04-07 09:24:30
  • 5星
    49KB weixin_58444518 2021-05-21 11:46:39
  • 4星
    2KB wu368296910 2013-11-25 17:03:25
  • 5.89MB tuchunr168 2015-01-07 11:14:52
  • 5星
    4KB percychiu 2016-10-23 18:57:40
  • 29KB weixin_42674361 2021-09-30 01:10:21
  • 13KB u011746381 2017-03-20 09:36:50
  • qq_14905655 2015-05-21 05:46:05
  • 摘要:1)VB6如何通过串口发送和接收二进制数据 2)如何一次获取一组串口数据 正文如下: 1)二进制收发 MSComm1.InBufferCount = 0 '清空接收缓冲区 MSComm1.RThreshold = 1 '收到RThreshold个字符数时触发...

    源码:https://download.csdn.net/download/sequh/13709444

    摘要:1)VB6如何通过串口发送和接收二进制数据

               2)如何一次获取一组串口数据

    正文如下:


    1)二进制收发
            MSComm1.InBufferCount = 0               '清空接收缓冲区
            MSComm1.RThreshold = 1                  '收到RThreshold个字符数时触发事件MSComm1_OnComm
            MSComm1.InputMode = comInputModeBinary      '设置接受数据的类型是二进制类型数据
            MSComm1.InputLen = 8                       '一次性从接收缓冲区中读取所有数据(8个字节为一组!!)


    2)发送二进制数据
         ' 发送时,要把二进制数据存储在byte数组中:

         '二进制模式发送:BB 00 22 00 00 22 7E
        Dim v(6) As Byte
        v(0) = &HBB
        v(1) = &H0
        v(2) = &H22
        v(3) = &H0
        v(4) = &H0
        v(5) = &H22
        v(6) = &H7E
        On Error Resume Next
        MSComm1.Output = v


    3)接收二进制数据
    '思路是这样的:


    这里的关键是调用sleep(20),把调用线程从线程调度器的可运行队列中移除20ms,这样程序在这20ms中是不再接收串口数据的。
    然后从接收缓冲区中把数据每次按8个为一组,一组一组地进行取出,直到缓冲区数据全部取完为止。
    这里有个知识点是:每MSComm1.Input一次就MSComm1.InBufferCount减小,减小的正是MSComm1.InputLen = 8 
    'MSComm1.InBufferCount属性:当前接收缓冲区接收到的数据的长度,

    Private Sub MSComm1_OnComm()
        Dim UB1%, UB2%, TM As Integer, i As Integer
        Dim strData As String
        Select Case MSComm1.CommEvent
            Case comEvReceive
                Sleep (20)          '相隔20ms就可以正确接收到24个字节的数据
                Do While MSComm1.InBufferCount > 0
                    TM = TM + 1
                    If TM = 1 Then
                        Rv_data = MSComm1.Input
                    Else
                        tmpRV = MSComm1.Input
                        UB1 = UBound(Rv_data)
                        UB2 = UBound(tmpRV) + 1 '元素比下标大1
                        ReDim Preserve Rv_data(UB1 + UB2)
                        For i = UB1 + 1 To UB1 + UB2
                            Rv_data(i) = tmpRV(i - UB1 - 1)
                        Next i
                    End If
                Loop

                If UBound(Rv_data) = 7 Then     '没接收到EPC
                    'lblTID.Caption = "空"
                Else
                    For i = 8 To 19
                        If Len(Hex(Rv_data(i))) = 1 Then
                            strData = strData & "0" & Hex(Rv_data(i)) & " " '如果只有一个字符,则前补0,如F显示0F,最后补空格
                        Else                                                    '方便显示观察如: 00 0F FE
                            strData = strData & Hex(Rv_data(i)) & " "
                        End If
                    Next
                    
                    strData = strData & "-" & Hex(Rv_data(5))

                    lblTID.Caption = strData
                    lblBg.BackColor = vbGreen
                    lblTID.ForeColor = &H80000012
                    
                    strData = ""
                    newCount = 0
        
        
                    Erase tmpRV
                    Erase Rv_data
                End If
        End Select
    End Sub

     

    展开全文
    sequh 2020-12-17 08:06:20
  • 58KB sequh 2020-12-17 07:54:54
  • 5星
    877KB pengpeng816 2013-11-01 13:26:41
  • 22KB qq_29340431 2015-09-27 18:21:59
  • VB 串口通讯 学习 学习目标: 学习VB 串口通讯 学习内容: 1、 vb 串口通讯设计 2、 通讯数据记录在自动生成的TXT文件内 学习时间: ... 刚学习vb串口通讯,程序和界面还需要很大改进、优化。希望大家能多多给予指正。

    VB 串口通讯 学习

    学习目标:

    学习VB 串口通讯


    学习内容:

    1、 vb 串口通讯设计 2、 通讯数据记录在自动生成的TXT文件内

    学习时间:

    抽出空闲时间进行学习

    学习产出:

    vb程序设计界面、串口传输数据
    以下是具体的程序:
    Private Sub Command1_Click()

    If MSComm1.PortOpen = True Then
    MsgBox (“串口已打开”)

    Else

    MSComm1.CommPort = 3
    MSComm1.Settings = “9600,N,8,1”

    MSComm1.InBufferSize = 1024 '接收缓冲区的大小,默认值为1024
    MSComm1.OutBufferSize = 1024 '发送缓冲区的大小,默认值为1024
    MSComm1.OutBufferCount = 0 '当前接收缓冲区接收到的数据的长度,对InBufferCount赋值MSComm1.InBufferCount:=0,可以清空接受缓冲区
    MSComm1.InBufferCount = 0 '当前发送缓冲区中数据的长度,对OutBufferCount 赋值MSComm1.OutBufferCount:=0,可以清空发送缓冲区
    MSComm1.InputMode = comInputModeText '以文本方式取回数据
    'MSComm1.InputMode = comInputModeBinary '设置接收数据模式为二进制形式
    MSComm1.InputLen = 0 '一次从Input属性中读取数据的长度, MSComm1.InputLen:=1,表示一次读取一个字节,如果MSComm1.InputLen:=0,则表示一次读取全部数据
    MSComm1.SThreshold = 0 '一次发送所有数据 ,发送数据时不产生OnComm 事件
    MSComm1.RThreshold = 1 '每接收1个字节就产生一个OnComm 事件

    MSComm1.PortOpen = True

    End If

    End Sub

    Private Sub Command2_Click()

    If MSComm1.PortOpen = False Then
    MsgBox (“串口已关闭”)

    Else

    MSComm1.PortOpen = Flse

    End If

    End Sub

    Private Sub Command3_Click()

    Open "F:\vb.txt" For Output As #1
    Print #1, Text1.Text
    Close
    

    End Sub

    Private Sub MSComm1_OnComm()

    Dim CommData As String

            MSComm1.InputLen = 0     '读入缓冲区全部内容
            CommData = MSComm1.Input '读入到缓冲区
            Text1.Text = CommData
    

    Dim str
    Open “F:\vb.txt” For Append As #1
    str = Text1.Text
    Write #1, str
    Close #1

    End Sub

    生成的TXT文件
    上面是在F盘生成的txt文件。

    在txt文件内记录到的通讯数据
    以上是记录在txt文件内的数据。

    刚学习vb串口通讯,程序和界面还需要很大改进、优化。希望大家能多多给予指正。

    展开全文
    yukuaidere 2020-09-11 16:39:12
  • 2KB hookie1990 2012-10-17 17:52:31
  • 84KB zpp350622950 2015-08-15 11:53:01
  • 210KB weixin_39748670 2018-06-22 08:44:39
  • 3星
    7KB feiyang8690365 2009-10-18 20:07:47
  • 56KB wenyuexunyin 2019-11-05 10:04:32
  • 5星
    59.63MB amen6225 2013-10-15 11:32:27
  •  MSComm 控件通过串行端口传输和接收数据,为应用程序提供串行...MSComm控件在串口编程时非常方便,程序员不必去花时间去了解较为复杂的API函数,而且在VC、VB、Delphi等语言中均可使用。Microsoft Communicati...

           转载:https://blog.csdn.net/dongyue786/article/details/8177047

      MSComm 控件通过串行端口传输和接收数据,为应用程序提供串行通讯功能。MSComm控件在串口编程时非常方便,程序员不必去花时间去了解较为复杂的API函数,而且在VC、VB、Delphi等语言中均可使用。 Microsoft Communications Control(以下简称MSComm)是Microsoft公司提供的简化Windows下串行通信编程的ActiveX控件,它为应用程序提供了通过串行接口收发数据的简便方法。具体的来说,它提供了两种处理通信问题的方法:一是事件驱动(Event-driven)方法,一是查询法。

      1.MSComm控件两种处理通讯的方式 

      1.1 事件驱动方式

         事件驱动通讯是处理串行端口交互作用的一种非常有效的方法。在许多情况下,在事件发生时需要得到通知,例如,在串口接收缓冲区中有字符,或者 Carrier Detect (CD) 或 Request To Send (RTS) 线上一个字符到达或一个变化发生时。在这些情况下,可以利用 MSComm 控件的 OnComm 事件捕获并处理这些通讯事件。OnComm 事件还可以检查和处理通讯错误。所有通讯事件和通讯错误的列表,参阅 CommEvent 属性。在编程过程中,就可以在OnComm事件处理函数中加入自己的处理代码。这种方法的优点是程序响应及时,可靠性高。每个MSComm 控件对应着一个串行端口。如果应用程序需要访问多个串行端口,必须使用多个 MSComm 控件。

      1.2 查询方式 

         查询方式实质上还是事件驱动,但在有些情况下,这种方式显得更为便捷。在程序的每个关键功能之后,可以通过检查 CommEvent 属性的值来查询事件和错误。如果应用程序较小,并且是自保持的,这种方法可能是更可取的。例如,如果写一个简单的电话拨号程序,则没有必要对每接收一个字符都产生事件,因为唯一等待接收的字符是调制解调器的“确定”响应。 

     

      2.MSComm控件的常用属性

       MSComm1.Settings:=’9600,n,8,1’  //设置波特率 ,校验位,数据位,停止位

       Settings属性: 设置串口的波特率 ,校验位,数据位,停止位, ’9600,n,8,1’表示波特率为9600,无奇偶校验,数据位为8为,1位停止位   

       MSComm1.InBufferSize:=1024  //接受缓冲区大小 

       InBufferSize 属性:接收缓冲区的大小,默认值为1024,也可以自己设定,比如, MSComm1.InBufferSize:=2000,则接收缓冲区的大小为2000字节   

       MSComm1.OutBufferSize:=1024  //发送缓冲区大小 

       OutBufferSize属性:发送缓冲区的大小,默认值为1024,也可以自己设定。  

       MSComm1.InBufferCount:=0  //清空接受缓冲区 

       InBufferCount属性:当前接收缓冲区接收到的数据的长度, count:=MSComm1.InBufferCount,count值接收缓冲区接收到的数据的长度,对InBufferCount赋值      

       MSComm1.InBufferCount:=0  //可以清空接受缓冲区

       MSComm1.OutBufferCount:=0  //清空发送缓冲区 

       OutBufferCount属性:当前发送缓冲区中数据的长度,对OutBufferCount 赋值 

               MSComm1. OutBufferCount:=0  //可以清空发送缓冲区

       MSComm1.InputMode:=comInputModeText  // 以文本方式取回数据 

          MSComm1.InputMode:=comInputModeBinary  //设置接收数据模式为二进制形式 

       InputMode属性:串口接收数据的模式, comInputModeText(0)表示以文本(ASCII)方式取回数据,

               comInputModeBinary(1)表示以二进制方式取回数据

               comInputModeText, comInputModeBinary为预定义常量,分别表示0,1

       RcvByte:=MSComm1.Input 

       Input属性:通过Input属性可以读取串口中接收到的数据,RcvByte:=MSComm1.Input表示读取串口接收到的数据 ,其中RcvByte的数据类型为: array of  Byte 或array  of  Variant.

       在读取之前先设置RcvByte的长度:SetLength(RcvByte,len),如果一次读取所有数据,则SetLength(RcvByte, MSComm1.InBufferCount)

       MSComm1.Output:= OutputDat

       Output属性:通过Output属性可以发送数据, MSComm1.Output:=OutputDat,将OutputDat中的数据发送出去,其中OutputDat数据类型array of  Byte,

       发送前要设置OutputDat的长度, SetLength(OutputDat,len),然后向OutputDat中填入数据,再清空发送缓冲区MSComm1.OutBufferCount:=0,然后再发送MSComm1.Output:=OutputDat

       MSComm1.InputLen:=0  // 一次读取所有数据 

       InputLen属性:一次从Input属性中读取数据的长度, MSComm1.InputLen:=1,表示一次读取一个字节,如果MSComm1.InputLen:=0,则表示一次读取全部数据

       MSComm1.SThreshold:=0  //一次发送所有数据 ,发送数据时不产生OnComm 事件

       SThreshold属性:通过该属性设置产生OnComm 事件(发送时产生)的阀值,若MSComm1.SThreshold:=0, 则一次发送所有数据 ,发送数据时不产生OnComm 事件

       若MSComm1.SThreshold:=5,当发送缓冲区的字节数从5字节减少到4字节时, 产生OnComm 事件

       MSComm1.RThreshold:=1  //每接收1个字节就产生一个OnComm 事件 

       RThreshold属性: 通过该属性设置产生OnComm 事件(接收时产生)的阀值,若MSComm1.RThreshold:=0,不产生OnComm 事件,

       若MSComm1.RThreshold:=5,接收缓冲区每收到5字节时,则产生OnComm 事件

       MSComm1.PortOpen:=True  //打开串口

       PortOpen属性:设置端口的打开与关闭,打开端口MSComm1.PortOpen:=True,

       MSComm1.PortOpen:=False  //关闭端口

       MSComm1.CommPort:=CommPort 

       CommPort属性:设置端口号, MSComm1.CommPort:=1, 设置端口号为COM1

       CommEvent属性: 常用的两个comEvReceive, comEvSend


      3.常数   

    HandShake 常数
    常数描述
    comNone0无握手
    comXonXoff1XOn/Xoff 握手
    comRTS2Request-to-send/clear-to-send 握手
    comRTSXOnXOff3Request-to-send 和 clear-to-send 握手皆可

     

    OnComm 常数
    常数描述
    comEvSend1发送事件
    comEvReceive2接收事件
    comEvCTS3clear-to-send 线变化
    comEvDSR 4data-set ready 线变化
    comEvCD5carrier detect 线变化
    comEvRing6振铃检测
    comEvEOF7文件结束

     

    Error常数
    常数描述
    comEventBreak1001接收到中断信号 
    comEventCTSTO1002Clear-to-send 超时 
    comEventDSRTO1003Data-set ready 超时
    comEventFrame1004帧错误
    comEventOverrun1006端口超速 
    comEventCDTO1007Carrier detect 超时
    comEventRxOver1008接收缓冲区溢出
    comEventRxParity1009Parity 错误 
    comEventTxFull1010传输缓冲区满 
    comEventDCB1011检索端口 设备控制块 (DCB) 时的意外错误 

     

    InputMode常数
    常数描述
    comInputModeText0(缺省)通过 Input 属性以文本方式取回数据
    comInputModeBinary1通过 Input 属性以二进制方式检取回数据

     

      4.属性及应用说明
        CDHolding属性:通过查询 Carrier Detect (CD) 线的状态确定当前是否有传输。Carrier Detect 是从调制解调器发送到相联计算机的一个信号,指示调制解调器正在联机。

        该属性在设计时无效,在运行时为只读。语法 object.CDHolding 设置值:CDHolding

        属性的设置值为: 

        设置           描述 

        True Carrier Detect   线为高电平 

        False Carrier Detect   线为低电平 
        说明:注意当 Carrier Detect 线为高电平 (CDHolding = True) 且超时时,MSComm 控件设置CommEvent 属性为 comEventCDTO(Carrier Detect 超时错误),并产生 OnComm 事件。

        注意 在主机应用程序中捕获一个丢失的传输是特别重要的,例如一个公告板,因为呼叫者可以随时挂起(放弃传输)。Carrier Detect 也被称为 Receive Line Signal Detect (RLSD)。

        数据类型 Boolean

        DSRHolding属性:确定 Data Set Ready (DSR) 线的状态。Data Set Ready 信号由调制解调器发送到相连计算机,指示作好操作准备。该属性在设计时无效,在运行时为只读。
        语法:object.DSRHolding
        object 所在处表示对象表达式,其值是“应用于”列表中的对象。
        DSRHolding 属性返回以下值:
        值            描述 
        True Data Set Ready   线高 
        False Data Set Ready     线低 
        说明:当 Data Set Ready 线为高电平 (DSRHolding = True) 且超时时,MSComm 控件设置 CommEvent 属性为 comEventDSRTO(数据准备超时)并产生 OnComm 事件。
        当为 Data Terminal Equipment (DTE) 机器写 Data Set Ready/Data Terminal Ready 握手例程时该属性是十分有用的。数据类型:Boolean

        Setting属性: 设置并返回波特率、奇偶校验、数据位、停止位参数。
        语法: object.Settings[ = value]
        说明:当端口打开时,如果 value 非法,则 MSComm 控件产生错误 380(非法属性值)。
        Value 由四个设置值组成,有如下的格式:
        "BBBB,P,D,S"
        BBBB 为波特率,P 为奇偶校验,D 为数据位数,S 为停止位数。value 的缺省值是:"9600,N,8,1"

        InputLen属性:设置并返回 Input 属性从接收缓冲区读取的字符数。
        语法 object.InputLen [ = value]
        InputLen 属性语法包括下列部分:
        value 整型表达式,说明 Input 属性从接收缓冲区中读取的字符数。 
        说明:InputLen 属性的缺省值是 0。设置 InputLen 为 0 时,使用 Input 将使 MSComm 控件读取接收缓冲区中全部的内容。
        若接收缓冲区中 InputLen 字符无效,Input 属性返回一个零长度字符串 ("")。在使用 Input 前,用户可以选择检查 InBufferCount 属性来确定缓冲区中是否已有需要数目的字符。

        该属性在从输出格式为定长数据的机器读取数据时非常有用。

        EofEnable属性:确定在输入过程中 MSComm 控件是否寻找文件结尾 (EOF) 字符。如果找到 EOF 字符,将停止输入并激活 OnComm 事件,此时 CommEvent 属性设置为 comEvEOF,
        语法:object.EOFEnable [ = value ]
        EOFEnable 属性语法包括下列部分:
        value 布尔表达式,确定当找到 EOF 字符时,OnComm 事件是否被激活,如“设置值”中所描述。 
        value 的设置值:
        True     当 EOF 字符找到时 OnComm 事件被激活。 
        False (缺省)当 EOF 字符找到时 OnComm 事件不被激活。 
        说明:当 EOFEnable 属性设置为 False,OnComm 控件将不在输入流中寻找 EOF 字符。

      5.错误消息
       下表列出 MSComm 控件可以捕获的错误:

    描述
    380     无效属性值 comInvalidPropertyValue
    383     属性为只读 comSetNotSupported
    394     属性为只读 comGetNotSupported 
    8000   端口打开时操作不合法 comPortOpen
    8001   超时值必须大于 0 
    8002   无效端口号 comPortInvalid
    8003   属性只在运行时有效 
    8004   属性在运行时为只读 
    8005   端口已经打开 comPortAlreadyOpen
    8006   设备标识符无效或不支持该标识符 
    8007   不支持设备的波特率 
    8008   指定的字节大小无效 
    8009   缺省参数错误 
    8010     硬件不可用(被其它设备锁定)
    8011   函数不能分配队列 
    8012   设备没有打开 comNoOpen 
    8013   设备已经打开 
    8014   不能使用 comm 通知 
    8015   不能设置 comm 状态 comSetCommStateFailed
    8016     不能设置 comm 事件屏蔽
    8018   仅当端口打开时操作才有效 comPortNotOpen 
    8019     设备忙 
    8020     读 comm 设备错误 comReadError
    8021     为该端口检索设备控制块时的内部错误 comDCBError

    转载于:https://www.cnblogs.com/ricoo/p/10028166.html

    展开全文
    abbilglf665483 2019-09-26 05:03:16
  • 4星
    197KB action20 2008-07-14 09:22:28
  • 116KB pilihuo182175954 2016-06-01 15:40:18
  •  ' Debug.Print "获取串口DCB:" & flag    Dim SetDb() As String  SetDb = Split(Comsettings, ",")  If UBound(SetDb) >= 3 Then  typDCB.BaudRate = CLng(SetDb(0)) '定义波特率  If ...
    Option Explicit
    

    'Private Type COMSTAT
    '    fCtsHold As Long
    '    fDsrHold As Long
    '    fRlsdHold As Long
    '    fXoffHold As Long
    '    fXoffSent As Long
    '    fEof As Long
    '    fTxim As Long
    '    fReserved As Long
    '    cbInQue As Long
    '    cbOutQue As Long
    'End Type
    Public LinkSitu As String

    Public PortMemory As String
    Public AllBlueMemory As String
    Public BlueMemory As String

    Private Type COMSTAT
        fBitFields As Long                                     ' See Comment in Win32API.Txt COMSTAT
        cbInQue As Long
        cbOutQue As Long
    End Type

    Private Type COMMTIMEOUTS
        ReadIntervalTimeout As Long
        ReadTotalTimeoutMultiplier As Long
        ReadTotalTimeoutConstant As Long
        WriteTotalTimeoutMultiplier As Long
        WriteTotalTimeoutConstant As Long
    End Type

    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 OVERLAPPED
        Internal As Long
        InternalHigh As Long
        offset As Long
        OffsetHigh As Long
        hEvent As Long
    End Type

    Private Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
    End Type


    Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Private Declare Function GetLastError Lib "kernel32" () As Long
    Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As OVERLAPPED) As Long
    Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As OVERLAPPED) As Long 'OVERLAPPED
    Private Declare Function SetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
    Private Declare Function GetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
    Private Declare Function GetOverlappedResult Lib "kernel32" (ByVal hFile As Long, lpOverlapped As OVERLAPPED, lpNumberOfBytesTransferred As Long, ByVal bWait As Long) As Long
    Private Declare Function BuildCommDCB Lib "kernel32" Alias "BuildCommDCBA" (ByVal lpDef As String, lpDCB As DCB) As Long
    Private Declare Function SetCommState Lib "kernel32" (ByVal hCommDev As Long, lpDCB As DCB) As Long
    Private Declare Function GetCommState Lib "kernel32" (ByVal nCid As Long, lpDCB As DCB) As Long
    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 FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long
    Private Declare Function CreateEvent Lib "kernel32" Alias "CreateEventA" (lpEventAttributes As SECURITY_ATTRIBUTES, ByVal bManualReset As Long, ByVal bInitialState As Long, ByVal lpName As String) As Long
    Private Declare Function SetCommMask Lib "kernel32" (ByVal hFile As Long, ByVal dwEvtMask As Long) As Long
    Private Declare Function SetEvent Lib "kernel32" (ByVal hEvent As Long) 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 Declare Function ClearCommError Lib "kernel32" (ByVal hFile As Long, lpErrors As Long, lpStat As COMSTAT) As Long
    Private Declare Function WaitCommEvent Lib "kernel32 " (ByVal hFile As Long, lpEvtMask As Long, lpOverlapped As OVERLAPPED) As Long
    Private Declare Function ResetEvent Lib "kernel32 " (ByVal hFile As Long) As Long

    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    Private Const INVALID_HANDLE_VALUE = -1
    Private Const GENERIC_WRITE = &H40000000
    Private Const GENERIC_READ = &H80000000
    Private Const OPEN_EXISTING = 3
    Private Const FILE_ATTRIBUTE_NORMAL = &H80
    Private Const FILE_FLAG_OVERLAPPED = &H40000000
    Private Const DTR_CONTROL_DISABLE = &H0
    Private Const RTS_CONTROL_ENABLE = &H1
    Private Const PURGE_RXABORT = &H2
    Private Const PURGE_RXCLEAR = &H8
    Private Const PURGE_TXABORT = &H1
    Private Const PURGE_TXCLEAR = &H4
    Private Const ERROR_IO_PENDING = 997
    Private Const STATUS_WAIT_0 = &H0
    Private Const WAIT_OBJECT_0 = (STATUS_WAIT_0 + 0)
    Private Const WAIT_TIMEOUT = 258&
    Public Const PROCESS_QUERY_INFORMATION = &H400
    Public Const PROCESS_TERMINATE = &H1
    Private Const EV_RXCHAR = &H1                '  Any Character received

    Private m_OverlappedRead As OVERLAPPED
    Private m_OverlappedWrite As OVERLAPPED

    Private com_Handle As Long
    Private com_RxBy As Long
    Private com_TxBy As Long

    Public Property Get ReceivedByte() As Long
        ReceivedByte = com_RxBy
    End Property
    Public Property Get SendedByte() As Long
        SendedByte = com_TxBy
    End Property

    Public Property Let ReceivedByte(x As Long)
        com_RxBy = 0
    End Property
    Public Property Let SendedByte(x As Long)
        com_TxBy = 0
    End Property

    Public Property Get Handle() As Long
        Handle = com_Handle
    End Property

    'Public Property Let Handle(id As Long)
    '    com_Handle = id
    'End Property

    '*************************************************************************
    '**函 数 名:OpenPort
    '**ComPort:形式如:COM1、COM2、LPT1等等
    '**Comsettings:形式如:"9600,n,8,1"
    '**lngInSize:写入缓冲区大小
    '**lngOutSize:写出缓冲区大小
    '*************************************************************************
    Public Function OpenPort(ComPort As String, Comsettings As String, Optional lngInSize As Long = 1024, Optional lngOutSize As Long = 1024) As Long
        On Error GoTo handelinitcom
        Dim RetVal As Long
        '定义标志值
        Dim flag As Long

        '定义设备控制块
        Dim typDCB As DCB

        Dim CtimeOut As COMMTIMEOUTS, dcbs As DCB
        Dim strCOM As String, strConfig As String

        '    strCOM = "COM" & Format(ComNumber, "0")
        strCOM = ComPort
        '    Com_Handle = CreateFile(strCOM, GENERIC_READ Or GENERIC_WRITE, 0, ByVal 0, _
             '                 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_OVERLAPPED, 0)

        com_Handle = CreateFile(strCOM, _
                GENERIC_READ Or GENERIC_WRITE, 0, ByVal 0, _
                OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_OVERLAPPED, 0)

        If com_Handle = INVALID_HANDLE_VALUE Then
            OpenPort = -1
            Exit Function
        End If

        '********获取出错信息********
        Dim errNum As Long
        errNum = GetLastError()
        '    Debug.Print "出错信息:" & errNum

        '********获取设备控制块********
        flag = GetCommState(com_Handle, typDCB)
        '    Debug.Print "获取串口DCB:" & flag
       
        Dim SetDb() As String
        SetDb = Split(Comsettings, ",")

        If UBound(SetDb) >= 3 Then
            typDCB.BaudRate = CLng(SetDb(0))                             '定义波特率
            If UCase(SetDb(1)) = "N" Then                                    'NOPARITY
               typDCB.Parity = 0                                    'NOPARITY                               '无校验位
            Else
               typDCB.Parity = 1
            End If
            typDCB.ByteSize = CByte(SetDb(2))                                   '数据位
           
            typDCB.StopBits = CByte(SetDb(3))                                   '停止位 0/1/2 = 1/1.5/2
        Else
            typDCB.BaudRate = 460800                             '定义波特率
            typDCB.Parity = 0                                  'NOPARITY                               '无校验位
            typDCB.ByteSize = 8                                '数据位
            typDCB.StopBits = 0                                '停止位 0/1/2 = 1/1.5/2
        End If
     

        '********设置串口参数********
        flag = SetCommState(com_Handle, typDCB)
        '    Debug.Print "设置串口参数:" & flag

        '********设置缓冲区大小********
        flag = SetupComm(com_Handle, lngInSize, lngOutSize)
        '    flag = SetupComm(com_Handle, 8192, 8192)

        CtimeOut.ReadIntervalTimeout = -1                      '0
        CtimeOut.ReadTotalTimeoutConstant = 0                  '2500
        CtimeOut.ReadTotalTimeoutMultiplier = 0               '0
        CtimeOut.WriteTotalTimeoutConstant = 0             '20  '2500
        CtimeOut.WriteTotalTimeoutMultiplier = 0            '200  '0
        '********超时设置********
        flag = SetCommTimeouts(com_Handle, CtimeOut)

        flag = SetCommMask(com_Handle, EV_RXCHAR)              '设置监视的事件为接收到字符
        '********清空读写缓冲区********
        Call PurgeComm(com_Handle, PURGE_RXABORT Or PURGE_RXCLEAR Or PURGE_TXABORT Or PURGE_TXCLEAR)    '清除缓冲区

        If flag = -1 Then
            RetVal = GetLastError()
            OpenPort = flag
            RetVal = CloseHandle(com_Handle)
            Exit Function
        End If

        '获取信号句柄
        Dim lpEventAttributes1 As SECURITY_ATTRIBUTES
        Dim lpEventAttributes2 As SECURITY_ATTRIBUTES

        m_OverlappedRead.hEvent = CreateEvent(lpEventAttributes1, 1, 0, 0)
        m_OverlappedWrite.hEvent = CreateEvent(lpEventAttributes2, 1, 0, 0)

        '判断设置参数是否成功   设置输入和输出缓冲区是否成功
        If m_OverlappedRead.hEvent = 0 Or m_OverlappedWrite.hEvent = 0 Then
            RetVal = GetLastError()
            OpenPort = RetVal
            If (m_OverlappedRead.hEvent <> 0) Then CloseHandle (m_OverlappedRead.hEvent)
            If (m_OverlappedWrite.hEvent <> 0) Then CloseHandle (m_OverlappedWrite.hEvent)
            Call CloseHandle(com_Handle)
            com_Handle = 0
            Exit Function
        End If

        OpenPort = 0
        Exit Function
    handelinitcom:
        Call CloseHandle(com_Handle)
        com_Handle = 0
        OpenPort = -1
        Exit Function
    End Function

    '*************************************************************************
    '**函 数 名:ClosePort
    '*************************************************************************
    Public Function ClosePort() As Long
        If com_Handle = INVALID_HANDLE_VALUE Then
            Exit Function
        End If

        Call SetCommMask(com_Handle, 0)
        Call SetEvent(m_OverlappedRead.hEvent)
        Call SetEvent(m_OverlappedWrite.hEvent)

        If (m_OverlappedRead.hEvent <> 0) Then CloseHandle (m_OverlappedRead.hEvent)
        If (m_OverlappedWrite.hEvent <> 0) Then CloseHandle (m_OverlappedWrite.hEvent)

        If CloseHandle(com_Handle) <> 0 Then
            ClosePort = 0
        Else
            ClosePort = -1
        End If

        com_Handle = INVALID_HANDLE_VALUE
        frmConnection.CHUANKOUFLAG = 0
    End Function

    '*************************************************************************
    '**函 数 名:ClearInBuf
    '**输    入:无
    '**输    出:无
    '**功能描述:清空输入缓冲区
    '*************************************************************************
    Public Function ClearInBuf() As Long
        If (com_Handle = INVALID_HANDLE_VALUE) Then
            ClearInBuf = 1
            Exit Function
        End If
        Call PurgeComm(com_Handle, PURGE_RXABORT Or PURGE_RXCLEAR)
        ClearInBuf = 0
    End Function

    '*************************************************************************
    '**函 数 名:ClearOutBuf
    '**输    入:无
    '**输    出:(Long) -
    '**功能描述:清空输出缓冲区
    '*************************************************************************
    Public Function ClearOutBuf() As Long
        If (com_Handle = INVALID_HANDLE_VALUE) Then
            ClearOutBuf = 1
            Exit Function
        End If
        Call PurgeComm(com_Handle, PURGE_TXABORT Or PURGE_TXCLEAR)
        ClearOutBuf = 0
    End Function

    '*************************************************************************
    '**函 数 名:SendData
    '**输    入:bytBuffer()(Byte) - 数据
    '**        :lngSize(Long)     - 数据长度
    '**输    出:(Long) -
    '**功能描述:发送数据
    '*************************************************************************
    Public Function SendData(bytBuffer() As Byte, lngSize As Long) As Long

        On Error GoTo Routine_Exit                                   '打开错误陷阱
        Dim errNum As Long
        Dim flag As Long
        Dim i As Long
        If (com_Handle = 0) Then
            SendData = 1
            Exit Function
        End If

        Dim dwBytesWritten As Long
        Dim bWriteStat As Long
        Dim ComStats As COMSTAT
        Dim dwErrorFlags As Long

        '    dwBytesWritten = lngSize

        Call ClearCommError(com_Handle, dwErrorFlags, ComStats)

        bWriteStat = WriteFile(com_Handle, bytBuffer(0), lngSize, dwBytesWritten, m_OverlappedWrite)
        '>>正常编译时候就这样就可以了
        Call GetOverlappedResult(com_Handle, m_OverlappedWrite, dwBytesWritten, 1)    '等待直到发送完毕
        '<<正常编译时候就这样就可以了
        '    ''>>这样在调试状态下可以的或在编译为P代码的情况下是可以正常运行
        '    If Not bWriteStat Then
        '        If GetLastError() = ERROR_IO_PENDING Then
        '            Call GetOverlappedResult(com_Handle, m_OverlappedWrite, dwBytesWritten, 1)    '等待直到发送完毕
        '        End If
        '    End If
        '    ''<<这样在调试状态下可以的或在编译为P代码的情况下是可以正常运行

        com_TxBy = com_TxBy + dwBytesWritten
        SendData = dwBytesWritten
        ClearOutBuf                                            '清除缓冲区

        ''    '发送数据
        ''    For i = 0 To UBound(bytBuffer)
        ''        flag = WriteFile(Com_Handle, bytBuffer(i), 1, dwBytesWritten, m_OverlappedWrite)
        ''        If Not flag Then
        ''            '获取出错码
        ''            errNum = GetLastError()
        ''            If (errNum = ERROR_IO_PENDING) Then
        ''                flag = 0
        ''                flag = GetOverlappedResult(Com_Handle, m_OverlappedWrite, dwBytesWritten, 1)
        ''                SendData = SendData + dwBytesWritten
        ''                Debug.Print "errNum = ERROR_IO_PENDING"
        ''            Else
        ''            End If
        ''        End If
        ''
        ''        '        '间隔时间(用于需要设定每字节间间隔时间的发送协议)
        ''        '                Sleep (intIntervalTime)
        ''    Next

        Exit Function
        '----------------
    Routine_Exit:
        SendData = -1
    End Function

    '*************************************************************************
    '**函 数 名:ReadData
    '**输    入:bytBuffer()(Byte) - 读取到的数据
    '**        :Outtime(Long)     - 等待时间ms
    '**输    出:(Long) -读取的字节数量
    '**功能描述:读取数据
    '*************************************************************************
    'Public Function ReadData(bytBuffer() As Byte, lngSize As Long, Optional Outtime As Long = 2000) As Long
    Public Function ReadData(bytBuffer() As Byte, Optional lngSize As Long = 255, Optional Outtime As Long = 2000) As Long
        On Error GoTo Routine_Exit                                   '打开错误陷阱

        If (com_Handle = 0) Then
            ReadData = 0
            Exit Function
        End If

        Dim lngBytesRead As Long
        Dim fReadStat As Long
        Dim dwRes  As Long

        Dim lngErrorFlags As Long
        Dim lngStatus As Long
        Dim udtCommStat As COMSTAT
        Dim evtMask As Long
        Dim ret As Long

    '        lngBytesRead = lngSize

    '    清除之前的一切错误与获取当前的状态
        lngStatus = ClearCommError(com_Handle, lngErrorFlags, _
                udtCommStat)

            Debug.Print "udtCommStat.cbInQue " & udtCommStat.cbInQue

    '    读数据
        If lngStatus <> 0 And udtCommStat.cbInQue > 0 And lngSize > 0 Then
            If lngSize = 255 And udtCommStat.cbInQue > 255 Then
                lngSize = udtCommStat.cbInQue
            End If

            ReDim Preserve bytBuffer(lngSize) As Byte

            fReadStat = ReadFile(com_Handle, bytBuffer(0), lngSize, lngBytesRead, m_OverlappedRead)
            com_RxBy = com_RxBy + lngBytesRead

            If fReadStat = 0 Then

                Call PurgeComm(com_Handle, PURGE_RXABORT Or PURGE_RXCLEAR Or PURGE_TXABORT Or PURGE_TXCLEAR)    '清除缓冲区
    '                    lngStatus = GetLastError
    '                    If lngStatus = ERROR_IO_PENDING Then
    '                           Call PurgeComm(com_Handle, PURGE_RXABORT Or PURGE_RXCLEAR Or PURGE_TXABORT Or PURGE_TXCLEAR) '清除缓冲区
    '                    Else
    '                        ' Some other error occurred.
    '                        lngBytesRead = -1
    '                        '                lngStatus = SetCommErrorEx("CommRead (ReadFile)", _
    '                                                      '                        Com_Handle)
    '                        GoTo Routine_Exit
    '                    End If
            End If

            ClearInBuf                                         '清除缓冲区
        End If
        ReadData = lngBytesRead

        Exit Function
    Routine_Exit:
        ReadData = 0
    End Function
    '*************************************************************************
    '**函 数 名:Class_Initialize
    '*************************************************************************
    Private Sub Class_Initialize()
       com_Handle = INVALID_HANDLE_VALUE
       com_RxBy = 0
       com_TxBy = 0
    End Sub

    '*************************************************************************
    '**函 数 名:Class_Terminate
    '*************************************************************************
    Private Sub Class_Terminate()
        Call ClosePort
    End Sub


    展开全文
    qq_32432741 2017-12-21 09:11:59
  • 23KB weixin_38748580 2021-05-07 09:35:48
  • qq_15578843 2018-12-27 08:52:57
  • qq_37136189 2017-03-29 12:12:23
  • 32.47MB qq_38837337 2017-11-23 16:55:22
  • 5星
    1.61MB w632307153 2012-08-08 18:37:10
  • 4星
    1.96MB qq5181688 2015-09-03 19:26:31
  • 118KB weixin_39298772 2019-09-25 15:32:31
  • dxt_1515 2016-09-02 10:36:03
  • qq_22732827 2020-05-30 09:36:15

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 2,230
精华内容 892
关键字:

vb串口收发