Private Sub MDIForm_Load()
Dim i As Integer
MSComm1.InputMode = 0'MSComm1.InputMode = 1
MSComm1.InputLen = 1
MSComm1.RThreshold = 1
MSComm1.InBufferCount = 0
End Sub
Private Sub MSComm1_OnComm()
Timer6.Enabled = True
'Static m As Integer
Static SP As Integer
Static DataRecieve(500) As String 'Byte
Static DatAddr As String '
Dim TempData1 As Long
Dim TempData2 As Long   ''''''''''''''
Dim DataBus As Variant
Dim i As Integer, j As Integer
Dim Check 'As Long
Dim CheckCode As Byte       '和校验码
Dim Test_Data As String
Dim cmdC As String
Dim content As Long
Select Case MSComm1.CommEvent
    '错误
        Case comEventBreak      ' 收到 Break。
        Case comEventCDTO       ' CD (RLSD) 超时。
        Case comEventCTSTO      ' CTS Timeout。
        Case comEventDSRTO      ' DSR Timeout。
        Case comEventFrame      ' Framing Error
        Case comEventOverrun    ' 数据丢失。
        Case comEventRxOver     ' 接收缓冲区溢出。
        'Case comEventRxParity   ' 奇偶校验。硬件检测到奇偶校验错误。
         '   If MSComm1.PortOpen = False Then
          '      MSComm1.PortOpen = True
           ' End If
            'message = "03"  ' 重发
            'SEND (message)
            'm = 0
        Case comEventTxFull     ' 传输缓冲区已满。
        Case comEventDCB        ' 获取 DCB 时意外错误事件
        Case comEvCD            ' CD 线状态变化。
        Case comEvCTS           ' CTS 线状态变化。
        Case comEvDSR           ' DSR 线状态变化。
        Case comEvRing          ' Ring Indicator 变化。
        Case comEvReceive       ' 收到 Rthreshold 个字符。
            m = m + 1
            DataBus = MSComm1.Input
            DataRecieve(m - 1) = DataBus 'MSComm1.Input
            If DataRecieve(m - 1) = ":" Then 'Asc(":") Then
              SP = m - 1
            End If

        Case comEvSend          ' 传输缓冲区有 Sthreshold 个字符
        Case comEvEOF           ' 输入数据流中发现 EOF 字符
End Select
.....
.....
End sub
我将232数据线的发送端与接收端短接,实现接收数据的测试,发送数据MainWin.MSComm1.Output = ":01070061926"
当我将inputmode设为1,datarecieve(500)设为byte时,接受报错type mismatch,这是为什么?Inputmode设为1时,数据通过 Input 属性以二进制形式取回,不就是按照字节接受吗?无论什么数据传到串口,它实际接收到的不都是二进制字节吗?为什么不能存到byte数组中呢
还有
我在watch window里添上一个在oncomm事件里定义的static变量数组datarecieve(500),MSComm1.Input,module里定义一个integer m,在oncomm事件中每接收到一个字符m自加1,同时datarecieve(m)在timer1中判断m>10时进入一个断点,然后想观察datarecieve的值为什么进入断电后,看不到datarecieve的值呢,难道在timer6的中断中只能看到全局变量与在timer6中定义的变量吗?如果想看到在其它过程定义的变量该怎么做呢?

解决方案 »

  1.   

    DataBus 已经是一个数组了,DataRecieve(m - 1) 却是一个单纯的 Byte,数组是不能直接赋值为 Byte 的。
    你应该将 DataBus 的成员 DataBus(0)、DataBus(1) 逐个赋给 DataRecieve 的成员。
      

  2.   

    databus怎么会是数组,我设的
    MSComm1.InputLen = 1
    MSComm1.RThreshold = 1,也就是数据缓冲区只有一个字符,怎么会以数组形式存储的呢?这个mscomm控件到底是以什么机制存储数据的呢?
      

  3.   

    长度为一个字节也还是数组,你可以用 IsArray(DataBus) 确认一下。
      

  4.   

    改为Private Sub MDIForm_Load()
    Dim i As Integer
    MSComm1.InputMode = 1
    MSComm1.InputLen = 1
    MSComm1.RThreshold = 1
    MSComm1.InBufferCount = 0
    End Sub 
    Private Sub MSComm1_OnComm()
    Timer6.Enabled = True
    Static m As Integer
    Static SP As Integer
    Static DataRecieve(500) As Byte......
    Dim DataBus As Variant

    Dim i As Integer, j As Integer
    Dim Check 'As Long
    Dim CheckCode As Byte       '和校验码
    Dim Test_Data As String
    Dim cmdC As String
    Dim content As Long
    Select Case MSComm1.CommEvent
        '错误
            Case comEventBreak      ' 收到 Break。
            Case comEventCDTO       ' CD (RLSD) 超时。
            Case comEventCTSTO      ' CTS Timeout。
            Case comEventDSRTO      ' DSR Timeout。
            Case comEventFrame      ' Framing Error
            Case comEventOverrun    ' 数据丢失。
            Case comEventRxOver     ' 接收缓冲区溢出。
            'Case comEventRxParity   ' 奇偶校验。硬件检测到奇偶校验错误。
             '   If MSComm1.PortOpen = False Then
              '      MSComm1.PortOpen = True
               ' End If
                'message = "03"  ' 重发
                'SEND (message)
                'm = 0
            Case comEventTxFull     ' 传输缓冲区已满。
            Case comEventDCB        ' 获取 DCB 时意外错误事件
            Case comEvCD            ' CD 线状态变化。
            Case comEvCTS           ' CTS 线状态变化。
            Case comEvDSR           ' DSR 线状态变化。
            Case comEvRing          ' Ring Indicator 变化。
            Case comEvReceive       ' 收到 Rthreshold 个字符。
                m = m + 1
                DataBus = MSComm1.Input
                DataRecieve(m - 1) = DataBus(0) 'MSComm1.Input
                If DataRecieve(m - 1) = Asc(":") Then
                  SP = m - 1
                End If 
    ......
    反正有些搞不懂vb的数据类型,MSComm1.InputMode = 0,Static DataRecieve(500) As string也可以运行不报错,但是运行到后来就会出现系统堆栈不够的警告,究其原因是DataRecieve(500) As string的问题,vb还真是麻烦...
      

  5.   

    Out of stack space (Error 28)
       The stack is a working area of memory that grows and shrinks dynamically with the demands of your executing program. This error has the following causes and solutions: You have too many active Function, Sub, or Property procedure calls. 
    Check that procedures aren't nested too deeply. This is especially true with recursive procedures, that is, procedures that call themselves. Make sure recursive procedures terminate properly. Use the Calls dialog box to view which procedures are active (on the stack).Your local variables require more local variable space than is available. 
    Try declaring some variables at the module level instead. You can also declare all variables in the procedure static by preceding the Property, Sub, or Function keyword with Static. Or you can use the Static statement to declare individual Static variables within procedures.You have too many fixed-length strings. 
    Fixed-length strings in a procedure are more quickly accessed, but use more stack space than variable-length strings, because the string data itself is placed on the stack. Try redefining some of your fixed-length strings as variable-length strings. When you declare variable-length strings in a procedure, only the string descriptor (not the data itself) is placed on the stack. You can also define the string at module level where it requires no stack space. Variables declared at module level are Public by default, so the string is visible to all procedures in the module.
    You have too many nested DoEvents function calls. 
    Use the Calls dialog box to view which procedures are active on the stack.Your code triggered an event cascade. 
    An event cascade is caused by triggering an event that calls an event procedure that's already on the stack. An event cascade is similar to an unterminated recursive procedure call, but it's less obvious, since the call is made by Visual Basic rather than by an explicit call in your code. Use the Calls dialog box to view which procedures are active (on the stack).
      

  6.   

    Dim Ip() 
    Select Case MSComm1.CommEvent
        Case comEvReceive        Ip = MSComm1.Input
            For i = LBound(Ip) To UBound(Ip)
                ……………………
            Next