懂VB的朋友帮个忙看串口问题 急...(100分)

  • 主题发起人 主题发起人 bluedyness
  • 开始时间 开始时间
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 = &quot;0&quot; & ToHex
End Function

Private Function InitComm(ByVal iPort)
On Error GoTo ErrMsg


If MSComm1.PortOpen Then MSComm1.PortOpen = False

MSComm1.Settings = &quot;19200,n,8,1&quot; '可自己修改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 = &quot;E&quot;
End Sub

Private Sub cmdFlashFile_Click()
Dim sFile$, I%

dlgFile.Filter = &quot;*.bin|*.bin&quot;
dlgFile.ShowOpen
sFile = dlgFile.FileName
txtFile = sFile

'sFile = App.Path & &quot;/w0.bin&quot;
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 &quot;文件已读入!长度:&quot; & lFile
End Sub

Private Sub cmdWrite_Click()

iPageMax = lFile / iPageByte
If (lFile Mod iPageByte) = 0 Then iPageMax = iPageMax - 1

MSComm1.Output = &quot;W&quot; '开始DownLoad
iPage = -1
iErr = 0
iWaitChar = 1
dTime = Now
End Sub

Private Sub Form_Load()
If InitComm(1) Then
Msg (&quot;串口打开!&quot;)
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 &quot;>&quot;
MSComm1.Output = &quot;<&quot;
iWaitChar = 3
Msg &quot;开始连接...&quot;

Case &quot;!&quot; '正确
iWaitChar = 1
iPage = iPage + 1
WriteFlash (iPage)
Msg (&quot;成功写到&quot; & iPage & &quot;页,错误次数&quot; & iErr & &quot;,时间&quot; & (Now - dTime))

Case &quot;@&quot; '错误
iWaitChar = 1
iErr = iErr + 1
WriteFlash (iPage) '再写
Msg (&quot;成功写到&quot; & iPage & &quot;页,错误次数&quot; & iErr)

Case &quot;E&quot; '退出boot
Msg &quot;正确退出&quot;

Case Else
If (iWaitChar = 3) Then
ChipInfo (sCmd)
Else
Msg (&quot;错误指令...&quot; & sCmd)
MSComm1.Output = &quot;E&quot;

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 = &quot;0&quot;) Then
sMsg = &quot;Mega8,&quot;
iPageByte = 64
Else
sMsg = &quot;Mega16,&quot;
iPageByte = 256
End If

sTemp = Mid(sInfo, 3, 1)
sTemp = &quot;版本&quot; & Str((Asc(sTemp) - Asc(&quot;f&quot;)) / 10 + 1)
sMsg = sMsg + sTemp
lblChip.Caption = sMsg

Msg (&quot;连接成功,可下载...&quot;)

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
 
[:(]........没看懂
 
检查是否溢出。。。
 
就是溢出阿 但不知道要修改哪里
 
看来是来错地方了!
只好花了一个晚上用delphi重新写了一个!
 

Similar threads

后退
顶部