LinMot_RS485_單軸 VB介面範例


LinMot-- RS485--VB  範例
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
'                                Communication
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Private Sub Form_Load()
If (MSComm1.PortOpen = True) Then MSComm1.PortOpen = False
MSComm1.CommPort = 1
MSComm1.PortOpen = True
MSComm1.Settings = "57600,N,8,1"
End Sub
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
'                                Random Value
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Private Function MyRnd() As Integer              '產生亂數0~9
Randomize
MyRnd = Int(Rnd * 10)                       
End Function
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
'                          Function:    format   10 to 16
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Function format16(ByVal distText As TextBox, ByVal kind As Integer, ByVal Num As Integer) As String
Dim dist, dist1 As TextBox
Set dist = distText
Dim Data, Data1 As Long
Dim i As Long
Select Case kind
 Case 0: i = 10000                             'Position Increment  (from 1mm to 0.1um)
 Case 1: i = 1000000                           'Max Velocity (from 1m/s to 0.001u)
 Case 2: i = 100000                            'Acceleration or Deceleration (from 1m/s^2 to 0.01u)
End Select
Data = Val(dist.Text)
Data1 = (Data * i)
d = Hex(Val(Data1))                            'Hex   16位元字串
Select Case Len(d)
             Case 1: dd = "0000000" & d
             Case 2: dd = "000000" & d
             Case 3: dd = "00000" & d
             Case 4: dd = "0000" & d
             Case 5: dd = "000" & d
             Case 6: dd = "00" & d
             Case 7: dd = "0" & d
             Case 8: dd = d
End Select
' (Lowest_MiddleLow_MiddleHigh_Highest)
LL = Right(dd, 2)
ML = Mid(dd, 5, 2)
MH = Mid(dd, 3, 2)
HH = Left(dd, 2)
Select Case Num
             Case 0: format16 = LL
             Case 1: format16 = ML
             Case 2: format16 = MH
             Case 3: format16 = HH
End Select
End Function
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
'                       Go to User-Defined Demand Distance
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Private Sub Axis1_RunDist_Click()
Dim BinaryAxisOne_PosDem(1 To 25) As Byte
BinaryAxisOne_PosDem(1) = &H1
BinaryAxisOne_PosDem(2) = &H13
BinaryAxisOne_PosDem(3) = &H15
BinaryAxisOne_PosDem(4) = &H2
BinaryAxisOne_PosDem(5) = &H0                                                                 'Write Motion Command Interface
BinaryAxisOne_PosDem(6) = &H2                                                                 'Write Motion Command Interface
BinaryAxisOne_PosDem(7) = CByte("&H" + "1" + CStr(MyRnd()))             '(7)個位數 need to change every time
BinaryAxisOne_PosDem(8) = &H1                                                                 '011xh:VAI Increment Dem Pos
BinaryAxisOne_PosDem(9) = CByte("&H" + format16(Axis1_dist, 0, 0))    '(9)-(12):  Position Increment
BinaryAxisOne_PosDem(10) = CByte("&H" + format16(Axis1_dist, 0, 1))   '(Lowest_MiddleLow_MiddleHigh_Highest)
BinaryAxisOne_PosDem(11) = CByte("&H" + format16(Axis1_dist, 0, 2))
BinaryAxisOne_PosDem(12) = CByte("&H" + format16(Axis1_dist, 0, 3))    
BinaryAxisOne_PosDem(13) = CByte("&H" + format16(Axis1_vel, 1, 0))    '(13)-(16): Max Velocity
BinaryAxisOne_PosDem(14) = CByte("&H" + format16(Axis1_vel, 1, 1))
BinaryAxisOne_PosDem(15) = CByte("&H" + format16(Axis1_vel, 1, 2))
BinaryAxisOne_PosDem(16) = CByte("&H" + format16(Axis1_vel, 1, 3))
BinaryAxisOne_PosDem(17) = CByte("&H" + format16(Axis1_acc, 2, 0))    '(17)-(20): Acceleration
BinaryAxisOne_PosDem(18) = CByte("&H" + format16(Axis1_acc, 2, 1))
BinaryAxisOne_PosDem(19) = CByte("&H" + format16(Axis1_acc, 2, 2))
BinaryAxisOne_PosDem(20) = CByte("&H" + format16(Axis1_acc, 2, 3))
BinaryAxisOne_PosDem(21) = CByte("&H" + format16(Axis1_dec, 2, 0))    '(21)-(24): Deceleration
BinaryAxisOne_PosDem(22) = CByte("&H" + format16(Axis1_dec, 2, 1))
BinaryAxisOne_PosDem(23) = CByte("&H" + format16(Axis1_dec, 2, 2))
BinaryAxisOne_PosDem(24) = CByte("&H" + format16(Axis1_dec, 2, 3))
BinaryAxisOne_PosDem(25) = &H4
MSComm1.Output = BinaryAxisOne_PosDem
End Sub
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
'                              Move to Position 10 mm
'
'                     MaxVel= 0.1m/s; Acc= 1m/s^2; Dec= 1m/s^2;
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Private Sub AxisOne_MoveToPos_10_Click()
Dim BinaryAxisOne(1 To 25) As Byte
BinaryAxisOne(1) = &H1
BinaryAxisOne(2) = &H13
BinaryAxisOne(3) = &H15
BinaryAxisOne(4) = &H2
BinaryAxisOne(5) = &H0
BinaryAxisOne(6) = &H2
BinaryAxisOne(7) = &H8         '個位數 need to change every time
BinaryAxisOne(8) = &H1         '010xh:VAI Go To Pos
BinaryAxisOne(9) = &HA0
BinaryAxisOne(10) = &H86
BinaryAxisOne(11) = &H1
BinaryAxisOne(12) = &H0
BinaryAxisOne(13) = &HA0
BinaryAxisOne(14) = &H86
BinaryAxisOne(15) = &H1
BinaryAxisOne(16) = &H0
BinaryAxisOne(17) = &HA0
BinaryAxisOne(18) = &H86
BinaryAxisOne(19) = &H1
BinaryAxisOne(20) = &H0
BinaryAxisOne(21) = &HA0
BinaryAxisOne(22) = &H86
BinaryAxisOne(23) = &H1
BinaryAxisOne(24) = &H0
BinaryAxisOne(25) = &H4
MSComm1.Output = BinaryAxisOne
End Sub
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
'                              Move to Position 0 mm
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Private Sub AxisOne_MoveToPos_0_Click()
Dim BinaryAxisOne(1 To 13) As Byte
BinaryAxisOne(1) = &H1
BinaryAxisOne(2) = &H13
BinaryAxisOne(3) = &H9
BinaryAxisOne(4) = &H2
BinaryAxisOne(5) = &H0
BinaryAxisOne(6) = &H2
BinaryAxisOne(7) = &H4         'need to change every time
BinaryAxisOne(8) = &H2
BinaryAxisOne(9) = &H0
BinaryAxisOne(10) = &H0
BinaryAxisOne(11) = &H0
BinaryAxisOne(12) = &H0
BinaryAxisOne(13) = &H4
MSComm1.Output = BinaryAxisOne
End Sub
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
'                         Positive Demand Position 3 mm
'
'                   MaxVel= 0.1m/s; Acc= 1m/s^2; Dec= 1m/s^2;
'------------------------------------------------------------------------------------------------------------------------------------'
'                                ** Parameter **
'   (1): Fix ID telegram start (&H1)
'   (2): Destination node ID (MACID)
'   (3): Telegram length
'   (4): Fix ID start data (&H2)
'   (5): Message Sub ID
'   (6): Message Main ID
'   (7): Motion Cmd Intf Header Low Byte   (count)
'   (8): Motion Cmd Intf Header High Byte
'   (9)-(12):  Position Increment   (Lowest_MiddleLow_MiddleHigh_Highest)
'   (13)-(16): Max Velocity         (Lowest_MiddleLow_MiddleHigh_Highest)
'   (17)-(20): Acceleration         (Lowest_MiddleLow_MiddleHigh_Highest)
'   (21)-(24): Deceleration         (Lowest_MiddleLow_MiddleHigh_Highest)
'   (25): Fix ID telegram end (&H4)
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Private Sub AxisOne_PosDemPos_3_Click()
Dim BinaryAxisOne_PosDem(1 To 25) As Byte
BinaryAxisOne_PosDem(1) = &H1
BinaryAxisOne_PosDem(2) = &H13
BinaryAxisOne_PosDem(3) = &H15
BinaryAxisOne_PosDem(4) = &H2
BinaryAxisOne_PosDem(5) = &H0                                                         'Write Motion Command Interface
BinaryAxisOne_PosDem(6) = &H2                                                         'Write Motion Command Interface
'BinaryAxisOne_PosDem(7) = &H1F                                                      '個位數 need to change every time
BinaryAxisOne_PosDem(7) = CByte("&H" + "1" + CStr(MyRnd()))
BinaryAxisOne_PosDem(8) = &H1                                                         '011xh:VAI Increment Dem Pos
BinaryAxisOne_PosDem(9) = &H30
BinaryAxisOne_PosDem(10) = &H75
BinaryAxisOne_PosDem(11) = &H0
BinaryAxisOne_PosDem(12) = &H0                               '
BinaryAxisOne_PosDem(13) = &HA0
BinaryAxisOne_PosDem(14) = &H86
BinaryAxisOne_PosDem(15) = &H1
BinaryAxisOne_PosDem(16) = &H0
BinaryAxisOne_PosDem(17) = &HA0
BinaryAxisOne_PosDem(18) = &H86
BinaryAxisOne_PosDem(19) = &H1
BinaryAxisOne_PosDem(20) = &H0
BinaryAxisOne_PosDem(21) = &HA0
BinaryAxisOne_PosDem(22) = &H86
BinaryAxisOne_PosDem(23) = &H1
BinaryAxisOne_PosDem(24) = &H0
BinaryAxisOne_PosDem(25) = &H4
MSComm1.Output = BinaryAxisOne_PosDem
End Sub
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
'                           Negative Demand Position 3 mm
'
'                      MaxVel= 0.1m/s; Acc= 1m/s^2; Dec= 1m/s^2;
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Private Sub AxisOne_NegDemPos_3_Click()
Dim BinaryAxisOne_NegDem(1 To 25) As Byte
BinaryAxisOne_NegDem(1) = &H1
BinaryAxisOne_NegDem(2) = &H13
BinaryAxisOne_NegDem(3) = &H15
BinaryAxisOne_NegDem(4) = &H2
BinaryAxisOne_NegDem(5) = &H0
BinaryAxisOne_NegDem(6) = &H2
'BinaryAxisOne_NegDem(7) = &H1F                                                       '個位數 need to change every time
BinaryAxisOne_NegDem(7) = CByte("&H" + "1" + CStr(MyRnd()))
BinaryAxisOne_NegDem(8) = &H1                                                          '011xh:VAI Increment Dem Pos
BinaryAxisOne_NegDem(9) = &HD0
BinaryAxisOne_NegDem(10) = &H8A
BinaryAxisOne_NegDem(11) = &HFF
BinaryAxisOne_NegDem(12) = &HFF
BinaryAxisOne_NegDem(13) = &HA0
BinaryAxisOne_NegDem(14) = &H86
BinaryAxisOne_NegDem(15) = &H1
BinaryAxisOne_NegDem(16) = &H0
BinaryAxisOne_NegDem(17) = &HA0
BinaryAxisOne_NegDem(18) = &H86
BinaryAxisOne_NegDem(19) = &H1
BinaryAxisOne_NegDem(20) = &H0
BinaryAxisOne_NegDem(21) = &HA0
BinaryAxisOne_NegDem(22) = &H86
BinaryAxisOne_NegDem(23) = &H1
BinaryAxisOne_NegDem(24) = &H0
BinaryAxisOne_NegDem(25) = &H4
MSComm1.Output = BinaryAxisOne_NegDem
End Sub
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
'                                    Homing
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Private Sub AxisOne_Homing_Click()
Dim BinaryAxisOneHome(1 To 9) As Byte
BinaryAxisOneHome(1) = &H1
BinaryAxisOneHome(2) = &H13
BinaryAxisOneHome(3) = &H5
BinaryAxisOneHome(4) = &H2
BinaryAxisOneHome(5) = &H0
BinaryAxisOneHome(6) = &H1
BinaryAxisOneHome(7) = &H3F
BinaryAxisOneHome(8) = &H8
BinaryAxisOneHome(9) = &H4
MSComm1.Output = BinaryAxisOneHome
End Sub
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
'                               Operation Enabled
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Private Sub AxisOne_OperationEnabled_Click()
Dim BinaryAxisOneHomeReset(1 To 9) As Byte
BinaryAxisOneHomeReset(1) = &H1
BinaryAxisOneHomeReset(2) = &H13
BinaryAxisOneHomeReset(3) = &H5
BinaryAxisOneHomeReset(4) = &H2
BinaryAxisOneHomeReset(5) = &H0
BinaryAxisOneHomeReset(6) = &H1
BinaryAxisOneHomeReset(7) = &H3F
BinaryAxisOneHomeReset(8) = &H0
BinaryAxisOneHomeReset(9) = &H4
MSComm1.Output = BinaryAxisOneHomeReset
End Sub
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
'                                    stop MC
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Private Sub AxisOne_StopMC_Click()
Dim BinaryAxisOne_StopMC(1 To 7) As Byte
BinaryAxisOne_StopMC(1) = &H1
BinaryAxisOne_StopMC(2) = &H13
BinaryAxisOne_StopMC(3) = &H3
BinaryAxisOne_StopMC(4) = &H2
BinaryAxisOne_StopMC(5) = &H3
BinaryAxisOne_StopMC(6) = &H6
BinaryAxisOne_StopMC(7) = &H4
MSComm1.Output = BinaryAxisOne_StopMC
End Sub
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
'                                  Start MC
'= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Private Sub AxisOne_StartMC_Click()
Dim BinaryAxisOne_StartMC(1 To 7) As Byte
BinaryAxisOne_StartMC(1) = &H1
BinaryAxisOne_StartMC(2) = &H13
BinaryAxisOne_StartMC(3) = &H3
BinaryAxisOne_StartMC(4) = &H2
BinaryAxisOne_StartMC(5) = &H4
BinaryAxisOne_StartMC(6) = &H6
BinaryAxisOne_StartMC(7) = &H4
MSComm1.Output = BinaryAxisOne_StartMC
End Sub