Attribute VB_Name = "Module1"
Public Const StepsOfOneMM As Long = 80000  '1mm = 80000/2steps, pull motor
Public Const StepsOfOneMM1 As Long = 12800 * 2 '1mm = 12800/2steps, fire motor
Public Const COfOneMW As Double = 2360   '1mw = 4000
Public Const COfOneMW1 As Double = 2089   '1mw = 4000
Public Const CountOfOneSCCM As Double = 20.36 '1SCCM = 20.48s
Public Const CountOfOneSCCM1 As Double = 24.85 '1SCCM = 20.48s
Public Const CountOfOneSCCM2 As Double = 20.36 * 2 '1SCCM = 20.48s
Public Const CountOfOneSCCM3 As Double = 24.85 * 2 '1SCCM = 20.48s
Public FireMotorMove, FireMotorDirection, PullMotorMove, CommunicateDone As Boolean
Public CommNumber As String
Public IData As Double
Public TimeValue As Single
Public TimeValue1 As Single
Public ColorLength, ColorLength1 As Integer
Public PreviousX, PreviousY, PreviousX1, PreviousY1, PreviousY2, PreviousY3
Public PowerData(14399) As Single
Public DataPointer As Integer
Public CountOfOneMW As Double
Public CountOfOneMW1 As Double
Public FirstData As Integer
Public TestData As Integer
Public TestData1 As Integer
Public FireMotorMoveDistance As Single
Public PDLTest As Boolean
Public MaxDuty As Double
Public MinDuty As Double
Public MinPort1 As Integer, MaxPort1 As Integer, MinPort2 As Integer, MaxPort2 As Integer'subroutine which send parameters to microchip and tell it move immediately
Public Sub SendParAndMove(MotorNumber As Integer, StepNumber As Long, StepsOfOneSecond As Long, MoveDirection As Boolean, MotorReturn As Boolean)
'    Call CommunicateWithMicrochip    Dim MotorDelay As Byte
    Dim OneStepBit As Integer
    Dim OutputBuffer As String
    
    Call ShakeHandWithMicrochip
    If CommunicateDone = False Then
        Exit Sub
    End If
    
    OutputBuffer = ""
        
    OutputBuffer = OutputBuffer & Chr(MotorNumber)
    
    OneStepBit = StepNumber Mod 10
    OutputBuffer = OutputBuffer & Chr(OneStepBit)
    StepNumber = (StepNumber - OneStepBit) / 10
    
    OneStepBit = StepNumber Mod 10
    OutputBuffer = OutputBuffer & Chr(OneStepBit)
    StepNumber = (StepNumber - OneStepBit) / 10
    
    OneStepBit = StepNumber Mod 10
    OutputBuffer = OutputBuffer & Chr(OneStepBit)
    StepNumber = (StepNumber - OneStepBit) / 10
    
    OneStepBit = StepNumber Mod 10
    OutputBuffer = OutputBuffer & Chr(OneStepBit)
    StepNumber = (StepNumber - OneStepBit) / 10
    
    OneStepBit = StepNumber Mod 10
    OutputBuffer = OutputBuffer & Chr(OneStepBit)
    StepNumber = (StepNumber - OneStepBit) / 10
    
    OneStepBit = StepNumber Mod 10
    OutputBuffer = OutputBuffer & Chr(OneStepBit)
    StepNumber = (StepNumber - OneStepBit) / 10
    
    OneStepBit = StepNumber Mod 10
    OutputBuffer = OutputBuffer & Chr(OneStepBit)
    StepNumber = (StepNumber - OneStepBit) / 10
    
    OneStepBit = StepNumber Mod 10
    OutputBuffer = OutputBuffer & Chr(OneStepBit)
    
    OneStepBit = StepsOfOneSecond Mod 10
    OutputBuffer = OutputBuffer & Chr(OneStepBit)
    StepsOfOneSecond = (StepsOfOneSecond - OneStepBit) / 10
    
    OneStepBit = StepsOfOneSecond Mod 10
    OutputBuffer = OutputBuffer & Chr(OneStepBit)
    StepsOfOneSecond = (StepsOfOneSecond - OneStepBit) / 10
    
    OneStepBit = StepsOfOneSecond Mod 10
    OutputBuffer = OutputBuffer & Chr(OneStepBit)
    StepsOfOneSecond = (StepsOfOneSecond - OneStepBit) / 10
    
    OneStepBit = StepsOfOneSecond Mod 10
    OutputBuffer = OutputBuffer & Chr(OneStepBit)
    StepsOfOneSecond = (StepsOfOneSecond - OneStepBit) / 10
    
    OneStepBit = StepsOfOneSecond Mod 10
    OutputBuffer = OutputBuffer & Chr(OneStepBit)
    
    If MoveDirection = True Then
        OutputBuffer = OutputBuffer & Chr(127)
    Else
        OutputBuffer = OutputBuffer & Chr(0)
    End If
    
    If MotorReturn = True Then
        OutputBuffer = OutputBuffer & Chr(127)
    Else
        OutputBuffer = OutputBuffer & Chr(0)
    End If
    
    Form1.MSComm1.Output = OutputBuffer
    
'    Do While Form1.MSComm1.OutBufferCount <> 0
'    Loop
    
'    Form1.MSComm1.PortOpen = False
    
End Sub

解决方案 »

  1.   

    Public Sub BeginRecord()
        Dim IData1C As Integer
        Dim IDataC As Integer
        Dim IData2C As Integer
        Dim IData3C As Integer
        
        
        form2.Label51.Caption = "0.0"
        form2.Label34.Caption = "0.5"
        form2.Label33.Caption = Format("1.0", ".0")
        form2.Label32.Caption = Format("1.5", ".0")
        form2.Label31.Caption = Format("2.0", ".0")
        form2.Label30.Caption = Format("2.5", ".0")
        form2.Label29.Caption = Format("3.0", ".0")
        form2.Label38.Caption = Format("3.5", ".0")
        form2.Label37.Caption = Format("4.0", ".0")
        form2.Label36.Caption = Format("4.5", ".0")
        form2.Label35.Caption = Format("5.0", ".0")
            
        TimeValue = 0
        TimeValue1 = 0
        
        ColorLength = 255
        ColorLength1 = 255
        
        PreviousX = 0
        PreviousY = 120
        PreviousX1 = 0
        PreviousY1 = 120
        PreviousY2 = form2.Picture1.Height - 120
        PreviousY3 = form2.Picture2.Height - 120
        
        DataPointer = 0
        PowerData(DataPointer) = PreviousY
        DataPointer = DataPointer + 1
        PowerData(DataPointer) = PreviousY2
        
        Call ShakeHandWithMicrochip
        If CommunicateDone = False Then
            Exit Sub
        End If
        
        Form1.MSComm1.Output = Chr(3)
        
        Call WaitAWord(IDataC)
        Call WaitAWord(IData1C)
        Call WaitAWord(IData2C)
        Call WaitAWord(IData3C)
        
        form2.Label58.Caption = Format(IData2C / CountOfOneSCCM2, "0")
        form2.Label57.Caption = Format(IData3C / CountOfOneSCCM, "0")
        
    '    Do While Form1.MSComm1.OutBufferCount <> 0
    '    Loop
        
    '    Form1.MSComm1.PortOpen = False
        
        If IDataC = 0 Then
            MsgBox ("通道1功率不能为0!")
            Exit Sub
        End If
        
        IData = IDataC / CountOfOneMW
        
        If form2.Text3.Text <> "" Then
            IData = form2.Text3.Text
        Else
            form2.Text3.Text = Format(IData, "0.####")
        End If
        
        form2.Timer1.Enabled = True
        Form1.Timer2.Enabled = False
        
    End SubPublic Sub ShakeHandWithMicrochip()    On Error GoTo Next_Port
        If CommNumber = "" Then
            Form1.MSComm1.CommPort = 1
    '    Else
    '        Form1.MSComm1.CommPort = CommNumber
        End If
    Comm_Start:
        If Form1.MSComm1.PortOpen = False Then
            Form1.MSComm1.PortOpen = True
        End If
        
        Form1.MSComm1.Output = "?"    'inquiry if 8032 is on
        
        Time1 = Timer()
        Do
            If Form1.MSComm1.Input = "." Then     '8032 send "." to answer
                CommNumber = Form1.MSComm1.CommPort
                GoTo ShakeHandDone
            End If
            Time2 = Timer()
        Loop While Time2 - Time1 < 2
        
        Form1.MSComm1.PortOpen = False
        
        If CommNumber = "" Then
            If Form1.MSComm1.CommPort = 2 Then   'fail
                MsgRtnValue = MsgBox("通讯失败!", vbOKOnly, "失败")
                CommunicateDone = False
                Exit Sub
            Else
                Form1.MSComm1.CommPort = Form1.MSComm1.CommPort + 1
                GoTo Comm_Start
            End If
        Else
            If Form1.MSComm1.CommPort = CommNumber Then
                If CommNumber = 1 Then
                    Form1.MSComm1.CommPort = 2
                Else
                    Form1.MSComm1.CommPort = 1
                End If
                GoTo Comm_Start
            Else
                MsgRtnValue = MsgBox("通讯失败!", vbOKOnly, "失败")
                CommunicateDone = False
                CommNumber = ""
                Exit Sub
            End If
        End If
            
    Next_Port:
        Form1.MSComm1.PortOpen = False
        If CommNumber = "" Then
            If Form1.MSComm1.CommPort = 2 Then   'fail
                MsgRtnValue = MsgBox("通讯失败!", vbOKOnly, "失败")
                CommunicateDone = False
                Exit Sub
            Else
                Form1.MSComm1.CommPort = Form1.MSComm1.CommPort + 1
                GoTo Comm_Start
            End If
        Else
            If Form1.MSComm1.CommPort = CommNumber Then
                If CommNumber = 1 Then
                    Form1.MSComm1.CommPort = 2
                Else
                    Form1.MSComm1.CommPort = 1
                End If
                GoTo Comm_Start
            Else
                MsgRtnValue = MsgBox("通讯失败!", vbOKOnly, "失败")
                CommunicateDone = False
                Exit Sub
            End If
        End If    
    ShakeHandDone:      'done
        CommunicateDone = True
        
    End Sub
      

  2.   

    我的一个学通信同学让我注释的,我对vb一点都不了解,只是懂得点vc,所以看的懂部分语法,不知道vb里的什么消息机制,各种控件调用是不是和vc一样的~~~~~~~?同学还说你们学计算机都2年了,还不懂vb?我寒了一阵!!我手上连本vb的书都没有。
    只看了几本vc的书。刚刚在基础类发了这个问题,我被彻底的鄙视了~~~~~~~~~那位大侠顺便帮我注释注释呀~~~~~~~~
      

  3.   

    这段程序好像是用vb的mscomm控件编的和伺服控制系统通信的.并通过计算机来控制该伺服驱动的.
    你从哪弄来的啊!
      

  4.   

    代码不完全,
    只有和微处理器的握手通信,输出控制和输入显示啊.
    我要源码.email:[email protected]
      

  5.   

    不错,是步进电机的控制程序。
    ShakeHandWithMicrochip是初始化串口并向步进电机控制芯片发初始化命令的;
    SendParAndMove执行某步控制
    BeginRecord执行某个控制逻辑啊1、程序不全
    2、谁有这个心思去注释一段别人写的程序啊,自己看吧。
      

  6.   

    samwzhang() 说得不错,注释别人写的程序是很痛苦的一件事情,给钱都不做~