B
bluedyness
Unregistered / Unconfirmed
GUEST, unregistred user!
这个程序是用来通过串口向单片机写入文件的,现在的问题是当文件的页数超过128的话就提示溢出,我不太懂VB,所以请会VB的朋友帮忙看在哪里修改一下!
有点急。。。部分代码如下:
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim FlashBuf() As Byte
Dim lFile As Long
Dim iWaitChar%, iPage%, iErr%, iPageByte%, iPageMax%
Dim dTime As Double
Private Sub Msg(ByVal sMsg$)
lblMsg.Caption = sMsg
End Sub
Private Function ToHex(ByVal data As Byte) As String
ToHex = Hex(data)
If (data < 16) Then ToHex = "0" & ToHex
End Function
Private Function InitComm(ByVal iPort)
On Error GoTo ErrMsg
If MSComm1.PortOpen Then MSComm1.PortOpen = False
MSComm1.Settings = "19200,n,8,1" '可自己修改Baud
MSComm1.RThreshold = 1
'MSComm1.InputMode = comInputModeBinary
MSComm1.CommPort = iPort
MSComm1.PortOpen = True
InitComm = True
Exit Function
ErrMsg:
MsgBox Err.Description
InitComm = False
End Function
Private Sub cmdExit_Click()
MSComm1.Output = "E"
End Sub
Private Sub cmdFlashFile_Click()
Dim sFile$, I%
dlgFile.Filter = "*.bin|*.bin"
dlgFile.ShowOpen
sFile = dlgFile.FileName
txtFile = sFile
'sFile = App.Path & "/w0.bin"
Open sFile For Binary Access Read As #1
lFile = LOF(1)
ReDim FlashBuf(lFile)
Get #1, , FlashBuf
Close #1
'For I = 0 To 3
' Debug.Print ToHex(FlashBuf(I))
'Next I
Msg "文件已读入!长度:" & lFile
End Sub
Private Sub cmdWrite_Click()
iPageMax = lFile / iPageByte
If (lFile Mod iPageByte) = 0 Then iPageMax = iPageMax - 1
MSComm1.Output = "W" '开始DownLoad
iPage = -1
iErr = 0
iWaitChar = 1
dTime = Now
End Sub
Private Sub Form_Load()
If InitComm(1) Then
Msg ("串口打开!"
End If
iWaitChar = 1
iPageByte = 64
End Sub
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive '接收到数据
If (MSComm1.InBufferCount >= iWaitChar) Then '已经有足够数据
RecvCmd (MSComm1.Input) '处理单片机的应答
End If
End Select
End Sub
Private Sub RecvCmd(ByVal sCmd$)
Select Case sCmd
Case ">"
MSComm1.Output = "<"
iWaitChar = 3
Msg "开始连接..."
Case "!" '正确
iWaitChar = 1
iPage = iPage + 1
WriteFlash (iPage)
Msg ("成功写到" & iPage & "页,错误次数" & iErr & ",时间" & (Now - dTime))
Case "@" '错误
iWaitChar = 1
iErr = iErr + 1
WriteFlash (iPage) '再写
Msg ("成功写到" & iPage & "页,错误次数" & iErr)
Case "E" '退出boot
Msg "正确退出"
Case Else
If (iWaitChar = 3) Then
ChipInfo (sCmd)
Else
Msg ("错误指令..." & sCmd)
MSComm1.Output = "E"
End If
iWaitChar = 1
End Select
End Sub
Private Sub ChipInfo(ByVal sInfo$)
Dim sTemp$, sMsg$
On Error Resume Next
sTemp = Left(sInfo, 1)
If (sTemp = "0" Then
sMsg = "Mega8,"
iPageByte = 64
Else
sMsg = "Mega16,"
iPageByte = 256
End If
sTemp = Mid(sInfo, 3, 1)
sTemp = "版本" & Str((Asc(sTemp) - Asc("f") / 10 + 1)
sMsg = sMsg + sTemp
lblChip.Caption = sMsg
Msg ("连接成功,可下载..."
End Sub
Private Sub WriteFlash(ByVal iPage%)
Dim Crc%, iTemp As Byte, iStart As Long, I%
Dim DD() As Byte
If iPage < 0 Then Exit Sub
ReDim DD(1)
If (iPage > iPageMax) Then
DD(0) = 255
DD(1) = 255
MSComm1.Output = DD()
Exit Sub
Else
iTemp = iPage / 256 '先高位
DD(0) = iTemp
iTemp = iPage Mod 256
DD(1) = iTemp
MSComm1.Output = DD() '输出Page编号
Crc = DD(0) + DD(1) '考虑Page的效验
If Crc > 255 Then Crc = Crc - 256
End If
Sleep (5)
ReDim DD(iPageByte)
iStart = iPage * iPageByte *******这里就出错*******************************
For I = 0 To iPageByte - 1
If (iStart + I) < lFile Then
iTemp = FlashBuf(iStart + I)
Else
iTemp = 255
End If
DD(I) = iTemp
Crc = Crc + iTemp
If Crc > 255 Then Crc = Crc - 256
Next I
DD(I) = Crc
MSComm1.Output = DD()
End Sub
有点急。。。部分代码如下:
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim FlashBuf() As Byte
Dim lFile As Long
Dim iWaitChar%, iPage%, iErr%, iPageByte%, iPageMax%
Dim dTime As Double
Private Sub Msg(ByVal sMsg$)
lblMsg.Caption = sMsg
End Sub
Private Function ToHex(ByVal data As Byte) As String
ToHex = Hex(data)
If (data < 16) Then ToHex = "0" & ToHex
End Function
Private Function InitComm(ByVal iPort)
On Error GoTo ErrMsg
If MSComm1.PortOpen Then MSComm1.PortOpen = False
MSComm1.Settings = "19200,n,8,1" '可自己修改Baud
MSComm1.RThreshold = 1
'MSComm1.InputMode = comInputModeBinary
MSComm1.CommPort = iPort
MSComm1.PortOpen = True
InitComm = True
Exit Function
ErrMsg:
MsgBox Err.Description
InitComm = False
End Function
Private Sub cmdExit_Click()
MSComm1.Output = "E"
End Sub
Private Sub cmdFlashFile_Click()
Dim sFile$, I%
dlgFile.Filter = "*.bin|*.bin"
dlgFile.ShowOpen
sFile = dlgFile.FileName
txtFile = sFile
'sFile = App.Path & "/w0.bin"
Open sFile For Binary Access Read As #1
lFile = LOF(1)
ReDim FlashBuf(lFile)
Get #1, , FlashBuf
Close #1
'For I = 0 To 3
' Debug.Print ToHex(FlashBuf(I))
'Next I
Msg "文件已读入!长度:" & lFile
End Sub
Private Sub cmdWrite_Click()
iPageMax = lFile / iPageByte
If (lFile Mod iPageByte) = 0 Then iPageMax = iPageMax - 1
MSComm1.Output = "W" '开始DownLoad
iPage = -1
iErr = 0
iWaitChar = 1
dTime = Now
End Sub
Private Sub Form_Load()
If InitComm(1) Then
Msg ("串口打开!"
End If
iWaitChar = 1
iPageByte = 64
End Sub
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive '接收到数据
If (MSComm1.InBufferCount >= iWaitChar) Then '已经有足够数据
RecvCmd (MSComm1.Input) '处理单片机的应答
End If
End Select
End Sub
Private Sub RecvCmd(ByVal sCmd$)
Select Case sCmd
Case ">"
MSComm1.Output = "<"
iWaitChar = 3
Msg "开始连接..."
Case "!" '正确
iWaitChar = 1
iPage = iPage + 1
WriteFlash (iPage)
Msg ("成功写到" & iPage & "页,错误次数" & iErr & ",时间" & (Now - dTime))
Case "@" '错误
iWaitChar = 1
iErr = iErr + 1
WriteFlash (iPage) '再写
Msg ("成功写到" & iPage & "页,错误次数" & iErr)
Case "E" '退出boot
Msg "正确退出"
Case Else
If (iWaitChar = 3) Then
ChipInfo (sCmd)
Else
Msg ("错误指令..." & sCmd)
MSComm1.Output = "E"
End If
iWaitChar = 1
End Select
End Sub
Private Sub ChipInfo(ByVal sInfo$)
Dim sTemp$, sMsg$
On Error Resume Next
sTemp = Left(sInfo, 1)
If (sTemp = "0" Then
sMsg = "Mega8,"
iPageByte = 64
Else
sMsg = "Mega16,"
iPageByte = 256
End If
sTemp = Mid(sInfo, 3, 1)
sTemp = "版本" & Str((Asc(sTemp) - Asc("f") / 10 + 1)
sMsg = sMsg + sTemp
lblChip.Caption = sMsg
Msg ("连接成功,可下载..."
End Sub
Private Sub WriteFlash(ByVal iPage%)
Dim Crc%, iTemp As Byte, iStart As Long, I%
Dim DD() As Byte
If iPage < 0 Then Exit Sub
ReDim DD(1)
If (iPage > iPageMax) Then
DD(0) = 255
DD(1) = 255
MSComm1.Output = DD()
Exit Sub
Else
iTemp = iPage / 256 '先高位
DD(0) = iTemp
iTemp = iPage Mod 256
DD(1) = iTemp
MSComm1.Output = DD() '输出Page编号
Crc = DD(0) + DD(1) '考虑Page的效验
If Crc > 255 Then Crc = Crc - 256
End If
Sleep (5)
ReDim DD(iPageByte)
iStart = iPage * iPageByte *******这里就出错*******************************
For I = 0 To iPageByte - 1
If (iStart + I) < lFile Then
iTemp = FlashBuf(iStart + I)
Else
iTemp = 255
End If
DD(I) = iTemp
Crc = Crc + iTemp
If Crc > 255 Then Crc = Crc - 256
Next I
DD(I) = Crc
MSComm1.Output = DD()
End Sub