forked from brianredbeard/knitting_machine
/
FDif.py
executable file
·329 lines (302 loc) · 10.7 KB
/
FDif.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/env python
# Copyright 2012 Steve Conklin
# steve at conklinhouse dot com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import sys
#import os
#import os.path
#import string
import time
import serial
FILTER=''.join([(len(repr(chr(x)))==3) and chr(x) or '.' for x in range(256)])
def dump(src, length=16):
result=[]
for i in xrange(0, len(src), length):
s = src[i:i+length]
hexa = ' '.join(["%02X"%ord(x) for x in s])
printable = s.translate(FILTER)
result.append("%04X %-*s %s\n" % (i, length*3, hexa, printable))
return ''.join(result)
class PDD1():
def __init__(self):
self.verbose = False
self.commport = ''
self.ser = None
return
def __del__(self):
return
def open(self, cport='/dev/ttyUSB0', rate = 9600, par = 'N', stpbts=1, tmout=1, xx=0):
#self.ser = serial.Serial(port = cport, baudrate = rate, parity = par, stopbits = stpbts, timeout = tmout, xonxoff=0)
self.ser = serial.Serial(port = cport, baudrate = rate, parity = par, stopbits = stpbts, timeout = tmout, xonxoff=0, rtscts=1, dsrdtr=0)
if self.ser == None:
print 'Unable to open serial device %s' % cport
raise IOError
self.commtimeout = tmout
# Sometimes on powerup there are some characters in the buffer, purge them
self.dumpchars()
return
def close(self):
if self.ser is not None:
self.ser.close()
self.ser = None
return
def dumpchars(self):
num = 1
while self.ser.inWaiting():
inc = self.ser.read()
if len(inc) != 0:
print 'flushed 0x%02X (%d)' % (ord(inc), num)
num = num + 1
else:
break
return
def getFDCresponse(self, expected):
sch = self.ser.read(expected)
return sch
def isSuccess(self, FDCstatus):
if self.verbose:
print "isSuccess checking:"
print dump(FDCstatus)
if (FDCstatus[0] == '0') and (FDCstatus[1] == '0'):
print " SUCCESS"
print " Physical Sector = %c%c" % (FDCstatus[2], FDCstatus[3])
print " Logical Sector = %c%c%c%c" % (FDCstatus[4], FDCstatus[5],FDCstatus[6], FDCstatus[7])
else:
print " ***** ERROR ***** : %c%c" % (FDCstatus[0], FDCstatus[1])
print " Physical Sector = %c%c" % (FDCstatus[2], FDCstatus[3])
print " Logical Sector = %c%c%c%c" % (FDCstatus[4], FDCstatus[5],FDCstatus[6], FDCstatus[7])
if len(FDCstatus) != 8:
print "Status Bad Len"
return False
for sb in FDCstatus[0:2]:
if sb != '0':
return False
return True
# def readchar(self):
# inc = ''
# while len(inc) == 0:
# inc = self.ser.read()
# return inc
def writebytes(self, bytes):
self.ser.write(bytes)
return
def calcChecksum(self, string):
sum = 0
for schr in string:
sum = sum + ord(schr)
sum = sum % 0x100
sum = sum ^ 0xFF
return chr(sum)
# def __commandResponse(self, command):
# if self.verbose:
# pcmd = command.strip()
# print 'writing command ===> <%s>' % pcmd
# self.dumpchars()
# ds_string = command
# cs = self.calcChecksum(ds_string)
# ds_string = ds_string + cs
# print "cR sending . . ."
# print dump(ds_string)
# self.writebytes(ds_string)
# response = self.readsomechars()
# print 'Command got a response of ', response
# return response
def __FDCcommandResponse(self, command, expected):
if self.verbose:
pcmd = command.strip()
print '-------------------------------------\nwriting FDC command ===> <%s>' % pcmd
self.dumpchars()
self.writebytes(command)
response = self.getFDCresponse(expected)
return response
#
# Begin Op Mode commands
#
def EnterFDCMode(self):
if False:
command = "ZZ" + chr(0x08) + chr(0)
cs = self.calcChecksum(command)
command = command + cs + "\r"
else:
command = "ZZ" + chr(0x08) + chr(0) + chr(0xF7) + "\r"
if self.verbose:
print "Entering FDC Mode, sending:"
print dump(command),
self.writebytes(command)
# There's no response to this command, so allow time to complete
time.sleep(.010)
if self.verbose:
print "Done entering FDC Mode\n"
return
#
# Begin FDC mode commands
#
def FDCChangeMode(self, mode = '1'):
"""
Change the disk drive mode. Default to changing from FDC
emulation mode back to Operational Mode.
"""
command = "M " + mode
result = self.__FDCcommandResponse(command, 8)
if not self.isSuccess(result):
raise IOError
return
def FDCcheckDeviceCondition(self):
"""
Send the 'D' command and return the result
"""
command = "D\r"
result = self.__FDCcommandResponse(command, 8)
if self.verbose:
# third byte:
# 30 - Normal
# 45 - Door open or no disk
# 32 - write protected
print "third byte = 0x%X" % ord(result[2])
return result
def FDCformat(self, sectorSize='5', verify=True):
"""
Format the floppy with the requested
"""
if verify:
command = "F"
else:
command = "G"
command = command + sectorSize + "\r"
result = self.__FDCcommandResponse(command, 8)
if not self.isSuccess(result):
raise IOError
return
def FDCreadIdSection(self, psn = '0'):
"""
Read the ID section of a physical sector, and return
the ID, which should be 12 bytes long
"""
if self.verbose:
print "FDCreadIdSection: Enter"
command = "A " + psn + "\r"
result = self.__FDCcommandResponse(command, 8)
if not self.isSuccess(result):
raise IOError
result = self.__FDCcommandResponse("\r", 12)
if self.verbose:
print "FDCreadIdSection data:"
print dump(result)
print "FDCreadIdSection: Exit"
return result
def FDCreadSector(self, psn = '0', lsn = '1'):
"""
Read the data from a logical sector.
psn is Physical sector number, in the range 0-79
lsn is the logical sector number, in range of 1
to the max for the physical sector size
"""
if self.verbose:
print "FDCreadSector: Enter"
command = "R " + psn + ","+ lsn + "\r"
result = self.__FDCcommandResponse(command, 8)
if not self.isSuccess(result):
raise IOError
if self.verbose:
print "FDCreadSector: Phase two"
result = self.__FDCcommandResponse("\r", 1024)
if self.verbose:
print "FDCreadSector data:"
print dump(result)
print "FDCreadSector: Exit"
return result
def FDCsearchIdSection(self, data, psn = '0'):
"""
Compare the data to the sector ID
psn is Physical sector number, in the range 0-79
Data length must be 12 bytes
"""
if len(data) != 12:
raise ValueError("ID data must be 12 characters long")
if self.verbose:
print "FDCsearchIdSection: Enter"
command = "S" + psn + "\r"
result = self.__FDCcommandResponse(command, 8)
if not self.isSuccess(result):
raise IOError
if self.verbose:
print "FDCsearchIdSection data:"
print dump(data)
result = self.__FDCcommandResponse(data, 12)
if self.verbose:
print "FDCsearchIdSection: Exit"
print "NOT SURE WHAT WE EXPECT HERE"
print "FDCsearchIdSection status is:"
print dump(result)
if not self.isSuccess(result):
raise IOError
return
def FDCwriteIdSection(self, data, psn = '0', verify = True):
"""
Write the data to the sector ID
psn is Physical sector number, in the range 0-79
Data length must be 12 bytes
"""
if len(data) != 12:
raise ValueError("ID data must be 12 characters long")
if verify:
command = "B"
else:
command = "C"
if self.verbose:
print "FDCwriteIdSection: Enter"
command = command + psn + "\r"
result = self.__FDCcommandResponse(command, 8)
if not self.isSuccess(result):
raise IOError
if self.verbose:
print "FDCwriteIdSection data:"
print dump(data)
result = self.__FDCcommandResponse(data, 8)
if self.verbose:
print "FDCwriteIdSection: Exit"
if not self.isSuccess(result):
raise IOError
return
#W Write log sector w/verify
# (two stages - second stage send data to be written)
#X Write log sector w/o vfy
# (two stages - second stage send data to be written)
def FDCwriteSector(self, data, psn = '0', lsn = '1', verify = True):
"""
Write the data to the sector
psn is Physical sector number, in the range 0-79, defaults to 0
lsn is logical sector number, defaults to 1
"""
if verify:
command = "W"
else:
command = "X"
if self.verbose:
print "FDCwriteSector: Enter"
command = command + psn + "," + lsn + "\r"
result = self.__FDCcommandResponse(command, 8)
if not self.isSuccess(result):
raise IOError
if self.verbose:
print "FDCwriteSector data:"
print dump(data)
result = self.__FDCcommandResponse(data, 8)
if self.verbose:
print "FDCwriteSector: Exit"
if not self.isSuccess(result):
raise IOError
return