Xmodem 演示
set last 0
while { 1 } {
incr BlockNumber
set InverseBlockNumber [expr 255 - $BlockNumber]
set databyte [string range $data 0 127]
set datasize [string length $databyte]
set data [string range $data 128 end]
if { $datasize < 128 } {
set last 1
for { set i 1 } { $i <= [expr 128 - $datasize]} { incr i } {
append databyte $CTRLZ
}
}
set checksum [::crc::xmodem -format %04X $databyte]
set packet "$SOH[binary format H2 [format %02x $BlockNumber]][binary format H2 [format %02x $InverseBlockNumber]]$databyte[binary format H4 $checksum]"
chan puts -nonewline $comport $packet
after 500
set resp [read $comport]
if { [string equal $resp $ACK] } {
if { $last } {
chan puts -nonewline $comport $EOT
break
}
if { $BlockNumber == 255 } {
set BlockNumber 0
}
continue
} elseif { [string equal $resp $NAK] } {
chan puts -nonewline $comport $packet
after 500
set resp1 [read $comport]
if { [string equal $resp $ACK] } {
if { $last } {
chan puts -nonewline $comport $EOT
break
}
if { $BlockNumber == 255 } {
set BlockNumber 0
}
continue
} elseif { [string equal $resp $NAK] } {
close $comport
return 0
} elseif { [string equal $resp $CAN] } {
close $comport
return 0
}
} elseif { [string equal $resp $CAN] } {
close $comport
return 0
} else {
close $comport
puts error
return 0
}
}
while { 1 } {
incr BlockNumber
set InverseBlockNumber [expr 255 - $BlockNumber]
set databyte [string range $data 0 127]
set datasize [string length $databyte]
set data [string range $data 128 end]
if { $datasize < 128 } {
set last 1
for { set i 1 } { $i <= [expr 128 - $datasize]} { incr i } {
append databyte $CTRLZ
}
}
set checksum [::crc::xmodem -format %04X $databyte]
set packet "$SOH[binary format H2 [format %02x $BlockNumber]][binary format H2 [format %02x $InverseBlockNumber]]$databyte[binary format H4 $checksum]"
chan puts -nonewline $comport $packet
after 500
set resp [read $comport]
if { [string equal $resp $ACK] } {
if { $last } {
chan puts -nonewline $comport $EOT
break
}
if { $BlockNumber == 255 } {
set BlockNumber 0
}
continue
} elseif { [string equal $resp $NAK] } {
chan puts -nonewline $comport $packet
after 500
set resp1 [read $comport]
if { [string equal $resp $ACK] } {
if { $last } {
chan puts -nonewline $comport $EOT
break
}
if { $BlockNumber == 255 } {
set BlockNumber 0
}
continue
} elseif { [string equal $resp $NAK] } {
close $comport
return 0
} elseif { [string equal $resp $CAN] } {
close $comport
return 0
}
} elseif { [string equal $resp $CAN] } {
close $comport
return 0
} else {
close $comport
puts error
return 0
}
}
留言
張貼留言