API

Tpdd

class Tpdd.Tpdd(comport)[source]

The Tpdd class is used for interfacing to a Tandy Portable Disk Drive (TPDD) or TPDD 2.

Parameters:comport – The com port that the TPDD is connected to.
checksum(data)[source]

Performs a checksum calculation on the block of data passed into the method.

Parameters:data – The block of data to be checksummed.
Returns:A 1 byte checksum.
delete_file(filename)[source]

Deletes a file on the TPDD disk.

Parameters:filename – The name of the file to delete.
Returns:A return code item (tuple) consisting of:
  • The return code (0x00 to 0xff)
  • The return code text.
file_close()[source]

Closes the file on a TPDD disk.

Returns:A return code item (tuple) consisting of:
  • The return code (0x00 to 0xff)
  • The return code text.
file_exists(filename)[source]

Checks the directory of the disk to see if the file is present. The filename parameter is checked by parse_filename() for correctness prior to searching for the file on the disk.

Parameters:filename – The filename to check.
Returns:True if file found on the TPDD otherwise False
file_read(echo=False)[source]

Reads the contents of a previously opened file.

Parameters:echo – (optional) Set to True to print the file to stdout during the read. Default is False.
Returns:A return code item (tuple) consisting of:
  • The return code (0x00 to 0xff)
  • The file contents, or the return code text in the case of an error.
file_read_open()[source]

Opens a file on the TPDD disk for reading. This method expects that the filename to read has been parsed using parse_filename().

Returns:A return code item (tuple) consisting of:
  • The return code (0x00 to 0xff)
  • The return code text.
file_write(data, echo=False)[source]

Writes data to a previously opened file.

Parameters:
  • data – The data to write to the file.
  • echo – (optional) Set to True to print the file to stdout during the save. Default is False.
Returns:

A return code item (tuple) consisting of:

  • The return code (0x00 to 0xff)
  • The return code text.

file_write_open()[source]

Opens a file for writing on the TPDD disk. This method expects that the filename to write to has been parsed using parse_filename().

Returns:A return code item (tuple) consisting of:
  • The return code (0x00 to 0xff)
  • The return code text.
format_disk()[source]

Formats the disk and reports any errors.

Returns:A return code item (tuple) consisting of:
  • The return code (0x00 to 0xff)
  • The return code text.
get_bank()[source]

Returns the current bank number as a character of either ‘0’ or ‘1’.

Returns:‘0’ for bank 0 and ‘1’ for bank ‘1’.
get_dir(size=True)[source]

Checks TPDD status and if ok issues the ‘dir’ command to the disk drive

Parameters:size – (Optional) Used to include file sizes and bytes free in listing. Default is True.
Returns:A return code item (tuple) consisting of:
  • The return code (0x00 to 0xff)
  • The directory contents as a list if successful or the return code error text.
get_status()[source]

Issues the status command to the disk drive.

Returns:A return code item (tuple) consisting of:
  • The return code (0x00 to 0xff)
  • The return code text.
load_file(filename, echo=False)[source]

Loads (reads) the contents of a file from the TPDD disk.

Parameters:
  • filename – The file on the disk to read.
  • echo – (optional) Set to True to print the file to stdout during the read. Default is False.
Returns:

A return code item (tuple) consisting of:

  • The return code (0x00 to 0xff)
  • The file contents if successful or the return code error text.

parse_dir(data, size=True)[source]

Takes a raw line from the results of a ‘dir’ command and reformats it into a readable text format

Parameters:
  • data – The raw data as read from the com port following a ‘dir’ command.
  • size – (Optional) Used to include file sizes and bytes free in listing. Default is True.
Returns:

A directory entry in readable text format

  • An exit code of either Tpdd.OK or Tpdd.DONE

parse_filename(filename)[source]

Checks the filename and pads spaces where required. i.e., ‘0:file.do’ will be returned as ‘0:file .do’ If the checks are ok, the Tpdd object updates its bank and filename to match the parsed filename.

Parameters:filename – The filename to check.
Returns:A return code item (tuple) consisting of:
  • The return code (0x00 to 0xff)
  • The filename formatted correctly for the TPDD or an error meesage or the error text in the case of an invalid filename.
parse_return_code(data)[source]

Parses return code data from the TPDD in the form of (hex) 12:01:xx:yy where xx is the return code, and yy is the checksum. Note: Does not check the checksum.

Parameters:data – The raw data as read from the com port following a ‘status’ command.
Returns:A return code item (tuple) consisting of:
  • The return code (0x00 to 0xff)
  • The return code text.
read_bytes(num_bytes=None, timeout=1)[source]

Reads a byte stream (if any) from the TPDD.

Parameters:
  • num_bytes – (optional) The number of bytes to read from the com port before returning. The default is no bytes specified resulting in relying on the timeout to complete the read.
  • timeout – (optional) The time period in seconds to wait before returning with no bytes read. The default is the Tpdd.DEFAULT_COM_TIMEOUT.
Returns:

The raw data as read from the com port (if any).

rename_file(old_filename, new_filename)[source]

Renames a file on the disk.

Parameters:
  • old_filename – The current name of the file.
  • new_filename – The new name for the file.
Returns:

A return code item (tuple) consisting of:

  • The return code (0x00 to 0xff)
  • The return code text.

return_code_item(code)[source]

Given a return code number, return the number and associated message for the return code. If return code not found, set the message to indicate an invalid return code.

Parameters:code – A return code number to be looked up in the return code dictionary.
Returns:A return code item (tuple) consisting of:
  • The return code (0x00 to 0xff)
  • The return code text.
save_file(filename, data, echo=False)[source]

Saves (writes) the data passed in to a file on the TPDD disk.

Parameters:
  • filename – The file on the disk to write to.
  • data – The data to write to the file.
  • echo – (optional) Set to True to print the file to stdout during the save. Default is False.
Returns:

A return code item (tuple) consisting of:

  • The return code (0x00 to 0xff)
  • The return code text.

set_bank(bank)[source]

Sets the bank number for file dir/file operations. If setting of the bank number is unsuccessful, the bank number remains unchanged.

Parameters:bank – The bank number expressed as a character of either ‘0’ or ‘1’.
Returns:True if setting of the bank number was successful, otherwise False.
write_bytes(bytes)[source]

Writes a byte stream to the TPDD.

Parameters:bytes – The data to be written to the serial port.
Returns:A return code item (tuple) consisting of:
  • The return code (0x00 to 0xff)
  • The return code text.
write_command(command_type)[source]

Assembles the byte stream for a command to the TPDD to pass on to the write_bytes() method. The method prepends the TPDD command bytes, calculates the checksum and appends the checksum.

Parameters:command_type – The hex bytes for the command i.e., Tpdd.CMD_STATUS
Returns:The response from the drive as a byte stream (if any).