On 08/28/2012 04:24 PM, Adam Barta wrote:
Could you share a code snippet?
Here's the code.
If you cat the file to hd does it read correctly even with the zero filesize?
I'll have to make a small mod before trying that.  I'll let you know.

Tom




On Wed, Aug 29, 2012 at 1:19 AM, Tom Kuiper <kui...@jpl.nasa.gov <mailto:kui...@jpl.nasa.gov>> wrote:

    On 08/28/2012 04:11 PM, Adam Barta wrote:
    is it possible that ipython is not actually closing the file but
    keeping the file descriptor open behind your back, if so then
    seeking to 0 might solve it?
    Clever idea! but, alas, Python is cleverer than that.  I put a
    seek(0) after (re)opening the file and before reading.  Still get
    0 bytes back.

    I'm not having much luck in finding a way to change the file
    size.  There is a 'truncate' method that will do it but Linux will
    zero fill an extended file.

    Cheers

    Tom




    On Wed, Aug 29, 2012 at 1:00 AM, Tom Kuiper <kui...@jpl.nasa.gov
    <mailto:kui...@jpl.nasa.gov>> wrote:

        On 08/28/2012 03:54 PM, David MacMahon wrote:

            On Aug 28, 2012, at 3:39 PM, Tom Kuiper wrote:

                I don't know how to get Python to read more than teh
                nominal file size if it is supposed to look like a file.

            If you want to read the register value a second time, you
            need to seek to the beginning of the file first, then
            read four bytes.  You should be able to repeat the
            seek/read pattern as many times as you want.

        I close the file after I write to it and open it again for
        the read.  After I write to the file, "ls -l" gives a size of
        zero instead of 4.

            If that's not what's confusing you, can you please
            clarify what is?

        I am not trying to mix reads and writes on an open file.  I
        know about seek and tell if that were what I wanted to do.

        Tom




-- *Adam Barta*
    c: +27 72 105 8611 <tel:%2B27%2072%20105%208611>
    e: a...@ska.ac.za <mailto:a...@ska.ac.za>
    w: www.ska.ac.za <http://www.ska.ac.za>




-- I or me?http://www.oxforddictionaries.com/page/145




--
*Adam Barta*
c: +27 72 105 8611
e: a...@ska.ac.za <mailto:a...@ska.ac.za>
w: www.ska.ac.za <http://www.ska.ac.za>




--
I or me? http://www.oxforddictionaries.com/page/145


# -*- coding: utf-8 -*-
from glob import glob
from os.path import basename
import shlex
from subprocess import PIPE, Popen
from struct import pack,unpack
import io

roach_file_system = "/home/kuiper/mnt/kuiper-localhost:60001/"

def roach_command(command):
  prefix = "/usr/bin/ssh -p 60002 root@localhost "
  try:
    p = Popen(prefix+command, stdout=PIPE, shell=True)
  except OSError, details:
    print "Popen failed with OS error\n",details
    return
  try:
    response = p.stdout.readlines()
    return response
  except Exception:
    raise Exception

def get_task_details(task):
  response = roach_command("ps -ef | grep "+task)
  if len(response) == 1:
    result = {}
    items = response[0].split()
    result['owner'] = items[0]
    result['taskPID'] = items[1]
    result['parentPID'] = items[2]
    result['command'] = items[-1]
    return result
  else:
    print "Too many lines in response"
    for line in response:
      print line
    raise RunTimeError

def show_registers(registers):
  keys = registers.keys()
  keys.sort()
  for register in keys:
    print("%20s  %5d  %s" %
      (register,registers[register],read_register_chars(register)))

def get_register_size(roach_register_path):
  file_listing = roach_command("ls -l "+roach_register_path)
  register_size = {}
  for line in file_listing[1:]:
    parts = line.strip().split()
    name = parts[-1]
    register_size[name] = int(parts[4])
  return register_size

def read_register(register):
  reg_fd = io.open(local_register_path+register,
                mode='rb',
                buffering=0)
  reg_fd.seek(0)
  bytes = buffer(reg_fd.read(4) )
  print "read_register got %s bytes" % len(bytes)
  reg_fd.close()
  return bytes

def write_register(register,uint):
  bytes = pack("<I",uint)
  print "write_register input: %s bytes: %s" % (len(bytes),buffer(bytes))
  reg_fd = io.open(local_register_path+register,
                mode='wb',
                buffering=0)
  try:
    written = reg_fd.write(buffer(bytes))
    print "write_register wrote",written,"bytes"
    print "file pointer is now at",reg_fd.tell()
  except Exception:
    raise Exception, "writing word failed"
  else:
    print "Closing register"
    reg_fd.close()
    return True

def read_register_uint(register):
  bytes = read_register(register)
  print "read_register_uint got %s bytes" % len(bytes),
  result = unpack("<I",bytes)
  return result[0]

def read_register_int(register):
  bytes = read_register(register)
  result = unpack("<i",bytes)
  return result[0]

def read_register_chars(register):
  bytes = read_register(register)
  result = unpack("<4c",bytes)
  return result

def read_register_bytes(register):
  bytes = read_register(register)
  result = unpack("<4b",bytes)
  return result

def read_register_ubytes(register):
  bytes = read_register(register)
  result = unpack("<4B",bytes)
  return result

def bit_value(register,bit):
  """
  bit 0 is the least significant bit
  """
  return bool(register & 2**bit)

def set_bit(register,bit):
  current_value = read_register_uint(register)
  print "set_bit initial register:",bin(current_value)
  new_value = current_value | 2**bit
  print "set_bit revised register:",bin(new_value)
  if write_register(register,new_value):
    return True
  else:
    raise RunTimeException,"setting bit failed"

def clr_bit(register,bit):
  current_value = read_register_uint(register)
  new_value = ~(~current_value | 2**bit)
  new_value = current_value | 2**bit
  if write_register(register,new_value):
    return True
  else:
    raise RunTimeException,"clearing bit failed"
  
def get_bram(device):
  """
  set 'trig_adc0' bit 0 to high and toggle adcsnap_ctrl bit 0.
  The capture buffer is called 'adcsnap_bram'.
  """
  
if __name__ == "__main__":
  task_details = get_task_details("dtospec")

  roach_register_path = "/proc/"+task_details['taskPID']+"/hw/ioreg/"
  local_register_path = roach_file_system + roach_register_path[1:]

  registers = glob(local_register_path+"*")
  registers.sort()

  register_size = get_register_size(roach_register_path)
  show_registers(register_size)

print "Board ID:          ", read_register_ubytes('sys_board_id')
print "10Gbe dest'n:      ", read_register_bytes('dest_ip')
print "ADC0 trigger:      ", read_register_uint('trig_adc0')
print "sum sq ADC samples:", read_register_int('adc_sum_sq0')
#print read_register_uint('acc_cnt')
set_bit('sys_scratchpad',1)
print read_register_uint('sys_scratchpad')
#clr_bit('trig_adc0',0)
#print "ADC0 trigger:", read_register_uint('trig_adc0')

Reply via email to