?? frmmain.frm
字號:
LastLine = Me.RTBCmdLine.SelStart + 3
End If
strTemp = Mid(Me.RTBCmdLine.Text, LastLine, 1)
If curInsPos >= LastLine Then
WhetherInLastLine = True
frmMain.RTBCmdLine.SelStart = curInsPos
Else
WhetherInLastLine = False
frmMain.RTBCmdLine.SelStart = curInsPos
End If
End Function
Private Sub Form_Unload(Cancel As Integer)
Comm.PortOpen = False
End Sub
Private Sub LstCode_DblClick()
Dim Index As Integer
blSingleStep = True
Index = Me.LstCode.ListIndex
RunCommand (Me.LstCode.List(Index))
End Sub
Private Sub MNU_About_Click()
frmAbout.Show 1, Me
End Sub
Private Sub MNU_Config_Code_Click()
frmControlCode.Show 1, frmMain
End Sub
Private Sub MNU_Config_Port_Click()
frmCfgSPort.Show 1, frmMain
End Sub
Private Sub MNU_Edit_Clear_Click()
frmMain.RTBCmdLine.Text = strTipChar
frmMain.RTBCmdLine.SelStart = Len(Me.RTBCmdLine.Text) '將插入點移動到末尾
End Sub
Private Sub MNU_Edit_Edit_Click()
frmMain.RTBCode.Visible = True
frmMain.LstCode.Visible = False
frmMain.cmdEdit.Enabled = False
frmMain.cmdImport.Enabled = True
frmMain.MNU_Run_Run.Enabled = False
End Sub
Private Sub MNU_Edit_EditDone_Click()
frmMain.MNU_Run_Run.Enabled = True
MoveRtoL
frmMain.RTBCode.Visible = False
frmMain.LstCode.Visible = True
frmMain.cmdImport.Enabled = False
frmMain.cmdEdit.Enabled = True
End Sub
Private Sub MNU_File_Close_Click()
If strFileName <> "" Then '不是空串說明已經打開了文件
frmMain.RTBCode.FileName = ""
strFileName = ""
frmMain.RTBCode.Text = ""
frmMain.MNU_Run_Run.Enabled = False
frmMain.MNU_File_Close.Enabled = False
'清空列表框
frmMain.LstCode.Clear
End If
End Sub
Private Sub MNU_File_Exit_Click()
Unload frmMain
End Sub
Private Sub MoveRtoL() 'move command from richtextbox to listbox
Dim curInsPos As Integer
Dim LineStart, LineEnd As Integer
Dim Command As String
If frmMain.RTBCode.Text = "" Then
MsgBox "缺少命令!", vbOKOnly, "錯誤警告!"
frmMain.MNU_Run_Run.Enabled = False
Exit Sub
End If
curInsPos = frmMain.RTBCode.SelStart
frmMain.RTBCode.SelStart = 1
Command = ""
frmMain.LstCode.Clear
Do While (StrComp(Command, "完成") <> 0) And (LineEnd < Len(Me.RTBCode.Text))
LineStart = Me.RTBCode.SelStart
frmMain.RTBCode.UpTo Chr(13), True, False
LineEnd = Me.RTBCode.SelStart
frmMain.RTBCode.SelStart = Me.RTBCode.SelStart + 3
Command = Mid(Me.RTBCode.Text, LineStart, LineEnd - LineStart + 1)
frmMain.LstCode.AddItem Command
Loop
'如果最后一條命令不是end,則加入end命令到列表框
If StrComp(Command, "完成") <> 0 Then '此時command的內容是最后一條命令
Me.LstCode.AddItem "完成"
End If
frmMain.RTBCode.SelStart = curInsPos
End Sub
Private Sub MNU_File_Open_Click()
On Error GoTo HandleErr
frmMain.CommonDialog1.Filter = "Text (*.txt)|*.txt|All files (*.*)|*.*"
frmMain.CommonDialog1.ShowOpen
If frmMain.CommonDialog1.FileName <> "" Then
strFileName = Me.CommonDialog1.FileName
frmMain.RTBCode.LoadFile strFileName, rtfText
'成功打開了文件
frmMain.MNU_File_Close.Enabled = True
frmMain.cmdImport.Enabled = True
frmMain.cmdEdit.Enabled = False
frmMain.RTBCode.Visible = True
frmMain.LstCode.Visible = False
frmMain.MNU_File_Save.Enabled = True
Else
MsgBox "cann't Open file without a filename.", vbOKOnly, "Error"
End If
Exit Sub
HandleErr:
MsgBox (Err.Description)
End Sub
Private Sub MNU_File_Save_Click()
On Error GoTo HandleError
frmMain.CommonDialog1.Filter = "Text (*.txt)|*.txt|All files (*.*)|*.*"
frmMain.CommonDialog1.FileName = strFileName
frmMain.CommonDialog1.ShowSave
If Me.CommonDialog1.FileName <> "" Then
strFileName = Me.CommonDialog1.FileName
frmMain.RTBCode.SaveFile Me.CommonDialog1.FileName, rtfText
'成功打開了文件
frmMain.MNU_File_Close.Enabled = True
Else
MsgBox "Cann't save file without a filename."
End If
Exit Sub
HandleError:
MsgBox (Err.Description)
End Sub
Private Sub MNU_Run_Run_Click()
blSingleStep = False
Me.Timer.Interval = 1
Me.Timer.Enabled = True
nCodeIndex = -1 '從第一條指令開始執行
End Sub
Private Sub RTBCmdLine_Keydown(KeyCode As Integer, Shift As Integer)
Dim strTemp As String
Dim curInsPos As Integer
'///////////////////////////////////////////////////////////////////////////////////
'////////////////////對于不同的輸入作相應地處理///////////////////////
'////////////////////////////////////////////////////////////////////////////////////
'如果不是光標移動字符,而且光標不再最后一行,則將插入點置于行尾
If KeyCode <> vbKeyEnd And KeyCode <> vbKeyHome And _
KeyCode <> vbKeyLeft And KeyCode <> vbKeyRight And _
KeyCode <> vbKeyDown And KeyCode <> vbKeyUp And WhetherInLastLine = False Then
Me.RTBCmdLine.SelStart = Len(Me.RTBCmdLine.Text)
End If
If KeyCode = vbKeyLeft Then
'如果插入點前的字符是提示符,則不能左移
If RTBCmdLine.SelStart <> 0 Then
strTemp = Mid(Me.RTBCmdLine.Text, RTBCmdLine.SelStart, 1)
If (StrComp(strTemp, strTipChar) = 0) And (WhetherInLastLine = True) Then
If Me.RTBCmdLine.SelStart = Len(Me.RTBCmdLine.Text) Then
curInsPos = RTBCmdLine.SelStart
Me.RTBCmdLine.Text = Me.RTBCmdLine.Text & " "
Me.RTBCmdLine.SelStart = curInsPos + 1
Else
Me.RTBCmdLine.SelStart = Me.RTBCmdLine.SelStart + 1
End If
End If
End If
End If
If KeyCode = vbKeyDown Then
'下一行是最后一行,則不能移動到提示符前
End If
If KeyCode = vbKeyBack Then
'如果插入點前的字符是提示符,則不能刪除
strTemp = Mid(Me.RTBCmdLine.Text, Me.RTBCmdLine.SelStart, 1)
If (StrComp(strTemp, strTipChar) <> 0) And WhetherInLastLine Then
frmMain.RTBCmdLine.Locked = False
Else
frmMain.RTBCmdLine.Locked = True
End If
Else
frmMain.RTBCmdLine.Locked = False '其他的字符則解除鎖定
End If
'////////////////////////////////////////////////////////////////////
'//////////////////按下Enter鍵后執行命令//////////////////////
'////////////////////////////////////////////////////////////////////
If KeyCode = vbKeyReturn Then
Dim LastLine As Integer 'start Point of Last Line
Dim LenLastLine As Integer 'Length of Last Line
frmMain.RTBCmdLine.SelStart = Len(Me.RTBCmdLine.Text)
frmMain.RTBCmdLine.UpTo Chr(13), False, False
If Me.RTBCmdLine.SelStart = 0 Then '如果只有一行,則不用加3
LastLine = Me.RTBCmdLine.SelStart + 1
Else
LastLine = Me.RTBCmdLine.SelStart + 3 'lastLine為“?”起始的位置
End If
LenLastLine = Len(Me.RTBCmdLine.Text) - LastLine
CurCommand = Mid(Me.RTBCmdLine.Text, LastLine + 1, LenLastLine)
'**********************
RunCommand (CurCommand)
'**********************
frmMain.RTBCmdLine.Text = Me.RTBCmdLine.Text & strTipChar
frmMain.RTBCmdLine.SelStart = Len(Me.RTBCmdLine.Text) - 1
End If
'///////////////////////////////////////////////////////////////////////
End Sub
'/////////////////////////////////////////////////////////////////////////////
'/////////////////////執行命令/////////////////////////////
Private Function FindBlank(Code As String) As Integer '找到第一個空格的位置
Dim i As Integer
Dim aChar As String
For i = 1 To Len(Code)
aChar = Mid(Code, i, 1)
If StrComp(aChar, " ") = 0 Then
FindBlank = i
Exit Function
End If
Next i
FindBlank = 0 '沒有空格
End Function
Private Sub DisplayInfor(infor) '在Command Line RichTextBox中顯示信息
frmMain.RTBCmdLine.Text = Me.RTBCmdLine.Text & Chr(13) & Chr(10) & infor & _
Chr(13) & Chr(10) & strTipChar
frmMain.RTBCmdLine.SelStart = Len(Me.RTBCmdLine.Text)
End Sub
Private Function DecToHex(nNum As Integer) As String
Dim str1, str2 As String
Dim num1, num2 As Integer
num1 = nNum \ 16 '除數
If num1 >= 10 Then
str1 = Chr(65 + num1 - 10) '顯示A TO F
Else
str1 = Str(num1)
End If
num2 = nNum Mod 16 '余數
If num2 >= 10 Then
str2 = Chr(65 + num2 - 10) '顯示A TO F
Else
str2 = Str(num2)
End If
DecToHex = "0x" & Right(str1, 1) & Right(str2, 1)
End Function
Private Sub RunCommand(Code As String)
Dim i As Integer
Dim DelayTime As Integer '延時
Dim strDelay As String
Dim Msg As String
Dim Index As Integer '機器碼的索引
Dim BlankPos As Integer
Dim Command As String
Dim strPara As String '去掉命令代碼以后的參數字符串
Dim Para As Integer
'進行命令到機器碼的翻譯
Code = Trim(Code) '去掉空格
BlankPos = FindBlank(Code)
If BlankPos <> 0 Then
Command = Mid(Code, 1, BlankPos - 1)
Else
Command = Code
End If
Index = 0
For i = 1 To 13 '用戶書寫的命令是否在13條命令之中
If StrComp(DecodeMap(i).strCode, Command) = 0 Then
Index = i
End If
Next i
'對非法的命令進行提示
If Index = 0 Then
Msg = "非法指令!"
DisplayInfor Msg
blSingleStep = True '轉化為單步執行
frmMain.Timer.Enabled = False '關閉時鐘
Exit Sub
End If
'對命令碼的合法性進行校驗
If BlankPos = 0 Then
'所有的命令都需要參數,所以blankPos都不能為0
Msg = "命令需要參數"
DisplayInfor Msg
blSingleStep = True '轉化為單步執行
frmMain.Timer.Enabled = False '關閉時鐘
Exit Sub
End If
strPara = Trim(Mid(Code, BlankPos + 1, Len(Code) - BlankPos)) '得到去掉命令碼的字符串
BlankPos = FindBlank(strPara) '如果有兩個參數則blankPos不為零
If DecodeMap(Index).blNeedPara Then '如果需要參數,則讀取參數和延時
If BlankPos <> 0 Then
Para = Val(Mid(strPara, 1, BlankPos - 1))
If Para < DecodeMap(Index).fParaLower Or Para > DecodeMap(Index).fParaUpper Then
Msg = "Parameters out of range!"
DisplayInfor Msg
blSingleStep = True '轉化為單步執行
frmMain.Timer.Enabled = False '關閉時鐘
Exit Sub
End If
strDelay = Mid(strPara, BlankPos + 1, Len(strPara) - BlankPos)
DelayTime = Val(Mid(strPara, BlankPos + 1, Len(strPara) - BlankPos))
Else
Msg = "命令需要控制參數和延時."
DisplayInfor Msg
blSingleStep = True '轉化為單步執行
frmMain.Timer.Enabled = False '關閉時鐘
Exit Sub
End If
Else
'不需要參數則直接讀取延時
nCmdPara = 0 '不需要參數時,將參數設置成0
If BlankPos = 0 Then
DelayTime = Val(strPara)
Else
Msg = "參數過多!"
DisplayInfor Msg
blSingleStep = True '轉化為單步執行
frmMain.Timer.Enabled = False '關閉時鐘
Exit Sub
End If
End If
'執行命令,收發機器碼
'給全程變量賦值
nDelayTime = DelayTime
nCmdIndex = Index
If DecodeMap(Index).blNeedPara Then
nCmdPara = Para
End If
'加入延時
'//////////向單片機發送代碼//////////
'首先處理命令碼
tmpCmdCode = Trim(strStepCode)
tmpCmdCode = Right(tmpCmdCode, 2)
Command_Buf(0) = "&H" + tmpCmdCode
TXData(0) = Val(Command_Buf(0))
tmpCmdCode = Trim(DecodeMap(Index).strMachineCode)
tmpCmdCode = Right(tmpCmdCode, 2)
Command_Buf(1) = "&H" + tmpCmdCode
TXData(1) = Val(Command_Buf(1))
tmpCmdCode = DecToHex(nCmdPara)
tmpCmdCode = Right(tmpCmdCode, 2)
Command_Buf(2) = "&H" + tmpCmdCode
TXData(2) = Val(Command_Buf(2))
'先判斷是否單字節發送測試
Dim BB As Integer
BB = &HBB
If BB = TXData(1) Then
Dim SingleByte(0) As Byte
SingleByte(0) = TXData(2)
Comm.Output = SingleByte
Else
Comm.Output = TXData
End If
tmRX.Enabled = False
Msg = strStepCode & Chr(13) & Chr(10) & DecodeMap(nCmdIndex).strMachineCode & Chr(13) & Chr(10) & DecToHex(nCmdPara) _
& Chr(13) & Chr(10) & Str(nDelayTime)
DisplayInfor Msg
'連續執行才開啟時鐘
If blSingleStep = False Then
Me.Timer.Enabled = False '時鐘置零
Me.Timer.Interval = nDelayTime '設置時鐘響應時間
Me.Timer.Enabled = True '啟動時鐘
End If
End Sub
Private Sub Timer_Timer()
Dim Msg As String
'if blGetResponds and blCorrect then
nCodeIndex = nCodeIndex + 1
'else
'重新發送命令
'重新發送命令的次數
'endif
'高亮顯示當前執行的命令
frmMain.LstCode.ListIndex = nCodeIndex
If nCodeIndex < Me.LstCode.ListCount - 1 Then
RunCommand (Me.LstCode.List(nCodeIndex))
'Me.Timer.Enabled = False
Else
Msg = "命令發送完成!"
DisplayInfor Msg
blSingleStep = True '轉化為單步執行
frmMain.Timer.Enabled = False
End If
End Sub
Private Sub tmRX_Timer()
If RXCorrectFlag Then
frmMain.tmRX.Enabled = False
RXCorrectFlag = False
RepeatCnt = 0
Else
'重發命令
frmMain.tmRX.Enabled = False
nCodeIndex = nCodeIndex - 1
RunCommand (Me.LstCode.List(nCodeIndex))
frmMain.tmRX.Enabled = True
' RepeatCnt = RepeatCnt + 1
If RepeatCnt > 5 Then
tmRX.Enabled = False
RepeatCnt = 0
Else: RepeatCnt = RepeatCnt + 1
End If
End If
End Sub
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -