View previous topic :: View next topic |
Author |
Message |
PeterPiper
Joined: 23 Mar 2006 Posts: 66
|
Posted: Mon Feb 26, 2007 4:28 pm Post subject: Xmodem? |
|
|
Does anyone have the code for implementing the transmit xmodem protocol ? |
|
Back to top |
|
|
amirf
Joined: 25 Aug 2009 Posts: 154
|
Posted: Fri Apr 16, 2010 3:33 am Post subject: |
|
|
anyone ? |
|
Back to top |
|
|
amirf
Joined: 25 Aug 2009 Posts: 154
|
Posted: Fri Apr 16, 2010 3:42 am Post subject: |
|
|
this is bootloader pc side for pic micro , anyone can porting for avr using
xmodem protocol ...javascript:emoticon('')
Dim UpdateCancelled As Boolean
Dim XON_Received As Boolean
Dim XOFF_Received As Boolean
Dim ACK_Received As Boolean
Dim ComPorts(1 To 16) As Boolean
Private Declare Function GetTickCount Lib "kernel32" () As Long ' used in the slowdown routine
Private Sub BtnExit_Click()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
Unload Me
End Sub
Private Sub Form_Load()
Dim PortSpeed
Dim COMPORT
Text1.Text = ""
Call Populate_Commports
Call Populate_PortSpeed
COMPORT = GetSetting("KMP Engineering", "AutoSaveSettings", "Com_Port", "")
If COMPORT <> "" Then
MSComm1.CommPort = COMPORT
End If
CmbCommSelect.Text = "COM" & MSComm1.CommPort
MSComm1.InputLen = 1
MSComm1.RThreshold = 1
PortSpeed = GetSetting("KMP Engineering", "AutoSaveSettings", "PortSpeed", "")
If PortSpeed <> "" Then
Call DisplaySpeed(PortSpeed)
'CMBPortSpeed.Text = PortSpeed & "bps"
MSComm1.Settings = PortSpeed
Else
CMBPortSpeed.Text = "600bps"
MSComm1.Settings = "600,n,8,1"
End If
End Sub
Private Sub DisplaySpeed(speed)
Select Case speed
Case "600,n,8,1"
CMBPortSpeed.Text = "600bps"
Case "1200,n,8,1"
CMBPortSpeed.Text = "1200bps"
Case "2400,n,8,1"
CMBPortSpeed.Text = "2400bps"
Case "4800,n,8,1"
CMBPortSpeed.Text = "4800bps"
Case "9600,n,8,1"
CMBPortSpeed.Text = "9600bps"
Case "19200,n,8,1"
CMBPortSpeed.Text = "19200bps"
Case "38400,n,8,1"
CMBPortSpeed.Text = "38400bps"
Case "57600,n,8,1"
CMBPortSpeed.Text = "57600bps"
Case "115200,n,8,1"
CMBPortSpeed.Text = "115200bps"
End Select
End Sub
Private Sub Populate_PortSpeed()
' adding valid com port speeds to the lineup
CMBPortSpeed.AddItem "600bps"
CMBPortSpeed.AddItem "1200bps"
CMBPortSpeed.AddItem "2400bps"
CMBPortSpeed.AddItem "4800bps"
CMBPortSpeed.AddItem "9600bps"
CMBPortSpeed.AddItem "19200bps"
CMBPortSpeed.AddItem "38400bps"
CMBPortSpeed.AddItem "57600bps"
CMBPortSpeed.AddItem "115200bps"
End Sub
Private Sub Populate_Commports()
Dim i As Integer
On Error Resume Next
CmbCommSelect.Clear
For i = 1 To 8
MSComm1.CommPort = i
MSComm1.PortOpen = True
If Err Then
ComPorts(i) = False
Debug.Print "Commport " & i & " Not Available "
Err = 0
Else
ComPorts(i) = True
Debug.Print "Commport " & i & " Available "
CmbCommSelect.AddItem "COM" & i
MSComm1.PortOpen = False
End If
If (MSComm1.PortOpen = True) Then MSComm1.PortOpen = False
Next i
End Sub
Private Sub CmbCommSelect_click()
On Error GoTo Errorhandler
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
Select Case CmbCommSelect.Text
Case "COM1"
MSComm1.CommPort = 1
Case "COM2"
MSComm1.CommPort = 2
Case "COM3"
MSComm1.CommPort = 3
Case "COM4"
MSComm1.CommPort = 4
Case "COM5"
MSComm1.CommPort = 5
Case "COM6"
MSComm1.CommPort = 6
Case "COM7"
MSComm1.CommPort = 7
Case "COM8"
MSComm1.CommPort = 8
Case Else
MSComm1.PortOpen = False
End Select
SaveSetting "KMP Engineering", "AutoSaveSettings", "Com_Port", MSComm1.CommPort
MSComm1.PortOpen = True
Exit Sub
Errorhandler:
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
MsgBox ("Port is in use, or not present, Please select another one")
SaveSetting "KMP Engineering", "AutoSaveSettings", "Com_Port", ""
MSComm1.PortOpen = True
Exit Sub
End Sub
Private Sub CMBPortSpeed_click()
On Error GoTo Errorhandler
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
Select Case CMBPortSpeed.Text
Case "600bps"
MSComm1.Settings = "600,n,8,1"
Case "1200bps"
MSComm1.Settings = "1200,n,8,1"
Case "2400bps"
MSComm1.Settings = "2400,n,8,1"
Case "4800bps"
MSComm1.Settings = "4800,n,8,1"
Case "9600bps"
MSComm1.Settings = "9600,n,8,1"
Case "19200bps"
MSComm1.Settings = "19200,n,8,1"
Case "38400bps"
MSComm1.Settings = "38400,n,8,1"
Case "57600bps"
MSComm1.Settings = "57600,n,8,1"
Case "115200bps"
MSComm1.Settings = "115200,n,8,1"
Case Else
MSComm1.PortOpen = False
End Select
SaveSetting "KMP Engineering", "AutoSaveSettings", "PortSpeed", MSComm1.Settings
MSComm1.PortOpen = True
Exit Sub
Errorhandler:
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
MsgBox ("Port is in use, or not present, Please select another one")
SaveSetting "KMP Engineering", "AutoSaveSettings", "Com_Port", ""
MSComm1.PortOpen = True
Exit Sub
End Sub
Private Sub CMD_Flash_Cancel_Click()
UpdateCancelled = True
End Sub
Private Sub CMD_Flash_OK_Click()
If txt_Filename.Text <> "" Then
Call OpenFile
End If
End Sub
Private Sub Cmd_Openfile_Click()
CommonDialog1.Filter = "Data Files (*.HEX)|*.HEX|"
CommonDialog1.FilterIndex = 1 ' Specify default filter.
CommonDialog1.ShowOpen ' Display the Open dialog box.
txt_Filename.Text = CommonDialog1.FileName ' Call the open file procedure.
frmFlash.Caption = "PIC Flasher - " & txt_Filename.Text
End Sub
Private Sub OpenFile()
'On Error GoTo Errorhandler
Dim fnum As Integer
Dim file_name As String
Dim file_record As String * 1
Dim MyString As String
Dim lSizeCount As Long
Dim lFileLen As Long
lSizeCount = 0
ProgressBar1.Max = 100
file_name = txt_Filename.Text ' Get the file name.
fnum = FreeFile ' Open the file.
Open file_name For Input As #fnum
lFileLen = LOF(fnum) ' get the length of the file
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
CmbCommSelect.Enabled = False
CMBPortSpeed.Enabled = False
UpdateCancelled = False
' my PIC when receiving STX,R, sets a flag in the eeprom and resets the CPU.
' The Bootloader then sees the flag is set and enters bootloading mode
MSComm1.Output = Chr$(2) & "R" 'Resets CPU and puts it in bootload mode
SlowDown (500) 'This allows the PIC to be rebooted and ready for action
Do Until EOF(fnum)
If UpdateCancelled = True Then
Call Close_Files
Exit Sub
End If
XON_Received = False
ACK_Received = False
Line Input #fnum, MyString
Text1.Text = MyString
MSComm1.Output = MyString & Chr$(13)
Counter = Timer + 2 ' 2 second timeout
lSizeCount = lSizeCount + Len(MyString) + 2 ' count the number of characters processed
Do While ACK_Received = False ' wait for the ACK from the PIC
If Int(100 * (lSizeCount / lFileLen)) = 100 Then ' The bootloader does not send an ACK after the last line
MsgBox "Flash Complete.", vbInformation
Call Close_Files
Exit Sub
End If
If Timer > Counter Then
Call Close_Files
MsgBox "Timeout!, Writing PIC Please check the cable and try again", vbCritical, "Timeout"
Exit Sub
End If
DoEvents
Loop
ProgressBar1.Value = Int(lSizeCount * 100 / lFileLen) ' set the progressbar
Label2.Caption = Int(100 * (lSizeCount / lFileLen)) & "% Done"
frmFlash.Caption = "PIC Flasher - " & txt_Filename.Text & " - " & Int(100 * (lSizeCount / lFileLen)) & "%"
Counter = Timer + 2 ' 2 second timeout
DoEvents
Loop
MSComm1.PortOpen = False
Exit Sub
Errorhandler: ' if any unspecified error has occured, show in messagebox
MsgBox "OpenFile " & Err.Description & vbCrLf & "Error Number: " & Err.Number, vbExclamation, "Please email detectag with the error number kasper@detectag.com"
Exit Sub
End Sub
Private Sub Close_Files()
Close #fnum ' close the file
ProgressBar1.Value = 0
Label2.Caption = ""
frmFlash.Caption = "PIC Flasher - " & txt_Filename.Text
Text1.Text = ""
CmbCommSelect.Enabled = True
CMBPortSpeed.Enabled = True
CmbCommSelect.Enabled = True
' If MSComm1.PortOpen = True Then
' MSComm1.PortOpen = False
' End If
End Sub
Private Sub MSComm1_OnComm()
On Error GoTo Errorhandler
Select Case MSComm1.CommEvent
Case comEventBreak ' A Break was received.
Case comEventCDTO ' CD (RLSD) Timeout.
Case comEventCTSTO ' CTS Timeout.
Case comEventDSRTO ' DSR Timeout.
Case comEventFrame ' Framing Error.
Case comEventOverrun ' Data Lost.
Case comEventRxOver ' Receive buffer overflow.
Case comEventRxParity ' Parity Error.
Case comEventTxFull ' Transmit buffer full.
Case comEventDCB ' Unexpected error retrieving DCB]
Case comEvCD ' Change in the CD line.
Case comEvCTS ' Change in the CTS line.
Case comEvCTS
Case comEvDSR ' Change in the DSR line.
Case comEvRing ' Change in the Ring Indicator.
Case comEvReceive ' Received RThreshold # of chars.
Call ReceiveHandeler
Case comEvSend ' There are SThreshold number of
' characters in the transmit buffer.
Case comEvEOF ' An EOF character was found in the
' input stream.
Case comCTSTO 'CTS timeout
Case comRTSTO
End Select
Exit Sub
Errorhandler:
MsgBox "MSComm1_OnComm()!" & Err.Description & vbCrLf & "Error Number: " & Err.Number, vbExclamation, "Please email detectag with the error number kasper@detectag.com"
End Sub
Private Sub ReceiveHandeler()
Dim RX
RX = MSComm1.Input
'Debug.Print RX
Select Case RX
Case Chr$(2) 'STX
' Debug.Print "STX"
Case Chr$(10)
Case Chr$(17)
XON_Received = True
Debug.Print "XON"
Case Chr$(19)
XOFF_Received = True
Debug.Print "XOFF"
Case Chr$(6)
ACK_Received = True
Debug.Print "ACK"
Case Else
End Select
End Sub
Public Sub SlowDown(MilliSeconds As Long)
Dim lngTickStore As Long
lngTickStore = GetTickCount()
Do While lngTickStore + MilliSeconds > GetTickCount()
DoEvents
Loop
End Sub
|
|
Back to top |
|
|
Luciano
Joined: 29 Nov 2004 Posts: 3149 Location: Italy
|
Posted: Sat Apr 17, 2010 8:46 am Post subject: |
|
|
Hi,
This section of the forum is intended to post (share) your working BASCOM-AVR code.
Please post questions like that in the forum section "Various".
Best regards,
Luciano |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|