Skip to content

Struggling Toward A Loader

February 6, 2017

As of end of day today I’m struggling toward a working xmodem loader.  I’ve cobbled up a python script that does a basic xmodem send with 32 byte blocks and i have a dumb C program that will mock up enough of the protocol to keep it happy.

//ssxload super simple xmodem loader
//loads a single 32 byte block with checksum from a custom host program
#include <nstdlib.h>
#include <olduino.h>
#include <cpu1802spd4port7.h>
#include "xloader.h"
unsigned char buff[64];

void main(){
	unsigned char ch='?';
	unsigned int nch=0,i=0;
	putch(NAK); //send NAK to start
	while(nch<36){
		if (seravail()){
			ch=getch();
			buff[nch++]=ch;
		}
	}
	putch(ACK);
	delay(1000);//delay to skip the EOT
	putch(ACK); //ack it anyway
	delay(10000);//let python clear out
	dump(buff,36);
}
#include <nstdlib.c>
#include <olduino.c>
from __future__ import print_function
import sys
import logging
logging.basicConfig()
import serial
try:
    from cStringIO import StringIO
except:
    from StringIO import StringIO
from time import sleep
import os
if len(sys.argv)>1:
    filename=sys.argv[1]
else:
    filename="test32.txt"
fileSize=os.path.getsize(filename)
print ("File Size is",fileSize)

def xmodem_send(serial, file):
	t=0
#	t, anim ='|/-\\'
	while 1:
	    if serial.read(1) != 'N':
	        t = t + 1
	        print ('.')
	        if t == 60 : return False
	    else:
	        break

	p = 1
	s = file.read(32)
	while s:
	    s = s + '\xFF'*(32 - len(s))
	    chk = 0
	    for c in s:
	        chk+=ord(c)
	    while 1:
	        serial.write('S') #SOH)
	        serial.write(chr(p))
	        serial.write(chr(255 - p))
	        serial.write(s)
	        serial.write(chr(chk%256))
	        serial.flush()

	        answer = serial.read(1)
	        if  answer == 'N': continue
	        if  answer == 'K': break
	        return False
	    s = file.read(32)
	    p = (p + 1)%256
	    print ('.')
	serial.write('T')
	return True

#Main program starts here - define the serial port, set RTS off, then open it
#open the file to be loaded
stream = open(filename,'rb')

port = serial.Serial(parity=serial.PARITY_NONE,
                     bytesize=serial.EIGHTBITS,
                     stopbits=serial.STOPBITS_ONE,timeout=1,xonxoff=0,
                     rtscts=0,dsrdtr=0,baudrate=57600)
port.rts=False
port.port='COM3'
port.open()
port.rts=True
sleep(0.001)
port.rts=False
port.flushInput()
#transfer the file
result=xmodem_send(port, stream)

stream.close()
port.close()

if result:
    print ("\ntransfer successful")
    sleep(.25)
else:
    print ("\ntransfer unsuccessful")
    x=raw_input("press enter to continue...");


I always want the last word so that wordpress doesn’t eat my code!

Thanks to nono240 for an answer in http://stackoverflow.com/questions/358471/xmodem-for-python
 

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: