Gorgon_Meducer 发表于 2005-9-17 17:42:56

[分项]一个从老贴中挖掘出来的关于VB串口通讯的类

UART.cls

-------------------------

Option Explicit



'-------------------------------------------------------------------------------'

'    类说明:该类作为串口通讯属性结构体,在类间传递数据                         '

'-------------------------------------------------------------------------------'



'-------------------------------'

'         通讯属性结构体      '

'-------------------------------'

Public ComUART                      As Object               '串口对象    '

Public DelayTimer                   As Object               '定时器对象'

Public ComTransferMaxWaitTime       As Integer            '最大等待时间'

Public WaitTime                     As Integer            '延时计数器'

Public UARTPortChoose               As Integer            '串口选择    '

Public UARTSetting                  As String               '串口设定    '









UARTPort.cls

-----------------------



Option Explicit



'-------------------------------------------------------------------------------'

'    类说明:该类提供了串口通讯的基本函数                                       '

'    版本:v1.0                                                               '

'    作者:傻孩子                                                            '

'    日期:2005年7月12日                                                      '

'                                                                               '

'---------------------------------------------------------------------------'

'    [功能说明]                                                               '

'      1、允许设定串口的基本通讯状态                                          '

'      2、提供了基于单字节模式下的基本收发字节函数                              '

'                                                                               '

'-------------------------------------------------------------------------------'



Private MachineUART                  As UART



'-------------------------------------------------------------------------------'

'函数说明:串口连接函数                                                       '

'说明:    根据设定,连接相应的串口                                           '

'输入:    串口对象、定时器对象   串口选择、串口设定字符串                  '

'输出:    打开串口的操作结果(Boolean)                                        '

'-------------------------------------------------------------------------------'

Public Function StartConnect(TransferUART As UART) As Boolean

On Error GoTo Do_With_It_In_StartConnect:

         

      Set MachineUART = TransferUART

         

      With MachineUART

   

            .ComUART = .ComUART

            .DelayTimer = .ComTransferMaxWaitTime

      

            .ComUART.CommPort = .UARTPortChoose

            .ComUART.Settings = .UARTSetting

   

            ' 告诉控件读入整个缓冲区。

            .ComUART.InputLen = 1

            .ComUART.InputMode = comInputModeBinary

            ' 打开端口。

      

            .ComUART.PortOpen = True

            StartConnect = True

      

      End With

         

Exit Function

Do_With_It_In_StartConnect:

'----------错误处理程序----------

   StartConnect = False

   Err.Clear

End Function



'-------------------------------------------------------------------------------'

'函数说明:串口单字节发送函数                                                 '

'说明:    发送指定的字节,并且等待指定的回应                                 '

'输入:    需要发送的字节等待回应的字符(给/0时表示不等待回应)             '

'输出:    发送是否成功(是否受到了指定的回应)                               '

'-------------------------------------------------------------------------------'

Public Function SendBytes(Datas As Byte, WaitSymble As Byte) As Boolean

On Error GoTo Do_With_It_In_SendBytes

    Dim Temp() As Byte

    Dim TempData() As Byte

    Dim a As Integer

    Dim Flag As Boolean

    ReDim TempData(0 To 0)

    ReDim Temp(0 To 0)

   

    With MachineUART

      TempData(0) = Datas

      .ComUART.Output = TempData()

      .WaitTime = .ComTransferMaxWaitTime

      .DelayTimer.Enabled = True

      

   

      If WaitSymble = 0 Then

            Flag = True

      Else

            Flag = False

      End If



re:



      If .ComUART.InBufferCount = 0 Then

            DoEvents

            GoTo Judge

      End If

   

   

      Temp = .ComUART.Input

      DoEvents



   

      For a = 0 To 0

   

            If WaitSymble = 0 Then

                Flag = True

            End If

         

            If Temp(a) = WaitSymble Then

                Flag = True

            End If

      Next a

   

Judge:

   

      If Flag = False And .WaitTime > 0 Then

            GoTo re:

      End If

   

      If Flag = False Then

            SendBytes = False

      Else

            SendBytes = True

      End If

   

      .DelayTimer.Enabled = False

      SendBytes = True

         

    End With

   

    Exit Function



Do_With_It_In_SendBytes:

'-----------------错误处理程序-------------------'

    SendBytes = False

    Err.Clear

End Function



'-------------------------------------------------------------------------------'

'函数说明:串口单字节接收函数                                                 '

'说明:    等待字节,并且根据要求做出回应                                     '

'输入:    需要等待的内容   回应的内容   工作模式                           '

'输出:    接收是否成功                                                       '

'-------------------------------------------------------------------------------'

Public Function ReceiveBytes(Datas As Byte, SendSymble As Byte, Models) As Boolean

'-----------------------------------'

'Models                说明       '

'-----------------------------------'

'    0         等待一个值并立即反 '

'                回SendSymble的信号 '

'                受到的数据放在Dates'

'    1         等待一个特定的值返 '

'                回SendSymble的信号 '

'    2         等待一个值,放在   '

'                Dates里面,不返回'

'    3         等待一个特定的值, '

'                不返回             '

'    4         收到什么返回什么   '

'-----------------------------------'

On Error GoTo Do_With_It_In_ReceiveBytes

    Dim Temp() As Byte

    Dim TempData() As Byte

    Dim Flag As Boolean

    ReDim Temp(0 To 0)

    ReDim TempData(0 To 0)

   

   

   

    With MachineUART

      .WaitTime = 5

      TempData(0) = SendSymble

      .DelayTimer.Enabled = True

   

      Select Case Models

   

            Case Is = 0

                '等待一个值,立即返回SendSymble信号,并将数据放到Dates变量里

                Flag = False

                Do

                  DoEvents

                  If .ComUART.InBufferCount > 0 Then

                        Temp = .ComUART.Input

                        Datas = Temp(0)

                        .ComUART.Output = TempData

                        Flag = True

                  End If

                Loop While Flag = False And .WaitTime > 0

            

                If Flag = False Then

                  ReceiveBytes = False

                Else

                  ReceiveBytes = True

                End If

                .DelayTimer.Enabled = False

                Exit Function

         

            Case Is = 1

                '等待一个特定的值,立即返回SendSymble信号

                Flag = False

                Do

                  DoEvents

                  If .ComUART.InBufferCount > 0 Then

                        Temp = .ComUART.Input

                     

                        If Datas = Temp(0) Then

                            .ComUART.Output = TempData

                            Flag = True

                        End If

                  End If

                Loop While Flag = False And .WaitTime > 0

            

                If Flag = False Then

                  ReceiveBytes = False

                Else

                  ReceiveBytes = True

                End If

                .DelayTimer.Enabled = False

                Exit Function

            

            Case Is = 2

                '等待一个值,将其放入Datas ,不返回

                Flag = False

                Do

                  DoEvents

                  If .ComUART.InBufferCount > 0 Then

                        Temp = .ComUART.Input

                        Datas = Temp(0)

                        Flag = True

                  End If

                Loop While Flag = False And .WaitTime > 0

            

                If Flag = False Then

                  ReceiveBytes = False

                Else

                  ReceiveBytes = True

                End If

                .DelayTimer.Enabled = False

                Exit Function

            

            Case Is = 3

                '等待一个特定的值,不返回

                Flag = False

                Do

                  DoEvents

                  If .ComUART.InBufferCount > 0 Then

                        Temp = .ComUART.Input

                     

                        If Datas = Temp(0) Then

                            Flag = True

                        End If

                  End If

                Loop While Flag = False And .WaitTime > 0

            

                If Flag = False Then

                  ReceiveBytes = False

                Else

                  ReceiveBytes = True

                End If

                .DelayTimer.Enabled = False

                Exit Function

         

            Case Is = 4

                '收到什么返回什么

                Flag = False

                Do

                  DoEvents

                  If .ComUART.InBufferCount > 0 Then

                        Temp = .ComUART.Input

                        Datas = Temp(0)

                        .ComUART.Output = Temp

                        Flag = True

                  End If

                Loop While Flag = False And .WaitTime > 0

            

                If Flag = False Then

                  ReceiveBytes = False

                Else

                  ReceiveBytes = True

                End If

                .DelayTimer.Enabled = False

                Exit Function



      End Select

    End With

Do_With_It_In_ReceiveBytes:

'-------------错误处理程序----------------'

    Err.Clear

    ReceiveBytes = False

End Function





该类的是用方法:

1、在窗体中添加两个控件:MSComm控件和Timer控件。

2、在模块中声明两个类。分别用于使用如上的UARTPort类,其中UART类是用来做传输数据的结构体的。

   例如:

   Public MachineTransferWork            As UARTPort

   Public MachineInfo                      As UART

3、在Timer控件中填写相应的代码作为延时操作的依据:

   例如:

   Private Sub Timer_TransferDelay_Timer()



    With MachineTransferWork

      If .WaitTime > 0 Then

            .WaitTime = .WaitTime - 1

      Else

            Timer_TransferDelay.Enabled = False

      End If

    End With

   

   End Sub

4、使用的前,先初始化一下各个类。

   例如:

   Set MachineTransferWork = New UARTPort

   Set MachineInfo = New UART

5、描述一下要打开的串口的信息,填写UART类的各个内容。其中串口设定的内容请参照MSComm Setting属性的设置方法。

   通过Set方法为对象变量赋值。也就是把串口控件,定时器控件赋给相应的变量。

6、使用

   1)打开一个串口:

    MachineTransferWork.StartConnect(MachineInfo)      '如果成功,函数返回True

   2)发送一个字节,并且等待下位机返回内容作为校验。

    MachineTransferWork.SendBytes(&HAC,&HAC)         '发送AC,等待AC ,成功返回True

   3)发送一个字节,不要求校验

    MachineTransferWork.SendBytes(&HAC,0)            '发送AC,不要求校验,成功返回True

   4)等待接收一个字节,收到以后返回信号            

    MachineTransferWork.ReceiveBytes(&HA1,&HAC,1)      '等待接收A1,收到以后返回AC

   (接收字节的其他模式参见函数模式说明)



……

1q2ww2q 发表于 2009-1-30 01:45:58

哈哈 爱死你啦

vincent.r 发表于 2009-1-30 13:18:11

mark

gaj8 发表于 2009-12-11 20:53:24

不错~~~

cancerlock 发表于 2010-10-12 20:17:25

备用
页: [1]
查看完整版本: [分项]一个从老贴中挖掘出来的关于VB串口通讯的类