正文

2个串口同时与2个单片机进行通讯2005-09-25 22:38:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/iamben250/5335.html

分享到:

给你一个串口类的框架,实现的代码由你自已根据情况完成。
其中的蓝色代码为你的实现函数,可以另写API的函数库或封装了MSCOMM控件的函数库。

Option Explicit

Public Enum EnuBaudRate
   enuBaudRate_110 = 110
   enuBaudRate_300 = 300
   enuBaudRate_600 = 600
   enuBaudRate_1200 = 1200
   enuBaudRate_2400 = 2400
   enuBaudRate_4800 = 4800
   enuBaudRate_5208 = 5208
   enuBaudRate_9600 = 9600
   enuBaudRate_14400 = 14400
   enuBaudRate_19200 = 19200
   enuBaudRate_38400 = 38400
   enuBaudRate_56000 = 56000
   enuBaudRate_57600 = 57600
   enuBaudRate_115200 = 115200
   enuBaudRate_128000 = 128000
   enuBaudRate_256000 = 256000
End Enum

Public Enum EnuParityCheck
   enuParityCheck_No = 0
   enuParityCheck_Odd = 1
   enuParityCheck_Even = 2
   enuParityCheck_Mark = 3
   enuParityCheck_Space = 4
End Enum

Public Enum EnuByteSize
   enuByteSize_Four = 4
   enuByteSize_Five = 5
   enuByteSize_Six = 6
   enuByteSize_Serven = 7
   enuByteSize_Eight = 8
End Enum

Public Enum EnuStopBits
   enuStopBits_One = 0
   enuStopBits_One5 = 1
   enuStopBits_Two = 2
End Enum

Private mlngPortID As Long
Private mblnLockPortID As Boolean

Public Property Let PortID(ByVal vlngPortID As Long)
   If Not mblnLockPortID Then
      mlngPortID = vlngPortID
   End If
End Property

Public Property Get PortID() As Long
   PortID = mlngPortID
End Property

Public Function OpenPort(ByVal venuBaudRate As EnuBaudRate, ByVal venuParityCheck As EnuParityCheck, _
                           ByVal venuByteSize As EnuByteSize, ByVal venuStopBits As EnuStopBits) As Boolean
   Dim lngReturn As Long
  
   OpenPort = False
   If PortID > 0 And PortID <= 32 Then
      If Not mblnLockPortID Then
         lngReturn = YourPortOpen(PortID, venuBaudRate, venuParityCheck, venuByteSize, venuStopBits)
         If CBool(lngReturn) Then
             OpenPort = True
             mblnLockPortID = True
         End If
      Else
         OpenPort = True
      End If
   End If
End Function

Public Function IsOpened() As Boolean
   IsOpened = mblnLockPortID
End Function

Public Function ClosePort() As Boolean
   Dim lngReturn As Long
  
   ClosePort = True
   If mblnLockPortID Then
      lngReturn = YourPortClose(PortID)
      If CBool(lngReturn) Then
         ClosePort = True
         mblnLockPortID = False
      End If
   End If
End Function

Public Function ReadData(ByRef rarrbytDataBuf() As Byte, ByVal vlngBufSize As Long) As Long
   Dim intReadLBound As Integer
  
   ReadData = 0
   If mblnLockPortID Then
      intReadLBound = LBound(rarrbytDataBuf)
      ReadData = YourPortReadData(PortID, rarrbytDataBuf(intReadLBound), vlngBufSize)
   End If
End Function

Public Function SendData(ByRef rarrbytDataBuf() As Byte, ByVal vlngBufSize As Long) As Long
   Dim intSendLBound As Integer
  
   SendData = 0
   If mblnLockPortID Then
      intSendLBound = LBound(rarrbytDataBuf)
      SendData = YourPortSendData(PortID, rarrbytDataBuf(intSendLBound), vlngBufSize)
   End If
End Function

Public Function ChangeParity(ByVal venuParityCheck As EnuParityCheck) As Boolean
   Dim lngReturn As Long

   ChangeParity = False
   If mblnLockPortID Then
      lngReturn = YourPortChangeParity(PortID, venuParityCheck)
      If CBool(lngReturn) Then
         ChangeParity = True
      End If
   End If
End Function

Public Function GetPortBufDataLen(ByRef rlngBufSize As Long) As Long
   GetPortBufDataLen = 0
   If mblnLockPortID Then
      GetPortBufDataLen = YourPortGetPortBufDataLen(PortID, rlngBufSize)
   End If
End Function

Public Sub SetShareMode(ByVal vblnSharePort As Boolean)
   If mblnLockPortID Then
      Call YourPortSetShareMode(PortID, vblnSharePort)
   End If
End Sub

Public Sub SetAddrMode(ByVal vblnDataFrameAddr As Boolean)
   If mblnLockPortID Then
      Call YourPortSetAddrMode(PortID, vblnDataFrameAddr)
   End If
End Sub

Public Sub SetAddrByteLen(ByVal vlngAddrByteLen As Long)
   If mblnLockPortID Then
      Call YourPortSetAddByteLen(PortID, vlngAddrByteLen)
   End If
End Sub

Public Function SendAndWaitReply(ByRef rarrbytWriteBuf() As Byte, ByVal vlngWriteBufLen As Long, _
                                    ByRef rarrbytReadBuf() As Byte, ByVal vlngReadBufLen As Long, _
                                    ByRef rlngReturnBufLen As Long, ByVal vlngWaitFirstByteTime As Long) As Boolean
   Dim lngReturnBufLen As Long
   Dim intWriteLBound As Integer
   Dim intReadLBound As Integer
   Dim lngReturn As Long

   SendAndWaitReply = False
   If mblnLockPortID Then
      intWriteLBound = LBound(rarrbytWriteBuf)
      intReadLBound = LBound(rarrbytReadBuf)
     
      lngReturn = YourPortSendAndWaitReply(PortID, rarrbytWriteBuf(intWriteLBound), vlngWriteBufLen, rarrbytReadBuf(intReadLBound), _
                                                     vlngReadBufLen, lngReturnBufLen, vlngWaitFirstByteTime)
      rlngReturnBufLen = lngReturnBufLen
      If CBool(lngReturn) Then
         SendAndWaitReply = True
      End If
   End If
End Function

Public Sub ClearRxBuf()
   If mblnLockPortID Then
      YourPortClearRxBuf (PortID)
   End If
End Sub

Public Function GetRxBufDataLen() As Long
   GetRxBufDataLen = 0
   If mblnLockPortID Then
      GetRxBufDataLen = YourPortGetRxBufDataLen(PortID)
   End If
End Function

Public Function GetByteInterval() As Long
   GetByteInterval = 0
   If mblnLockPortID Then
      GetByteInterval = YourPortGetByteInterval(PortID)
   End If
End Function

Public Sub SetByteInterval(ByVal vlngByteInterval As Long)
   If mblnLockPortID Then
      Call YourPortSetByteInterval(PortID, vlngByteInterval)
   End If
End Sub

Public Function GetRxBufSize() As Long
   GetRxBufSize = 0
   If mblnLockPortID Then
      GetRxBufSize = YourPortGetRxBufSize(PortID)
   End If
End Function

Public Sub SetRxBufSize(ByVal vlngRxBufSize As Long)
   If mblnLockPortID Then
      Call YourPortSetRxBufSize(PortID, vlngRxBufSize)
   End If
End Sub

Private Sub Class_Initialize()
   PortID = 0
   mblnLockPortID = False
End Sub

阅读(2604) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册