ferg
Posts: 32
Joined: Sun Jan 31, 2016 2:42 am

How to read (rb) a *.txt file and o/p depend on 1 or 0?

Sun Jun 19, 2016 6:14 am

Hello,

Can I read an arbitrary text file using the read (rb) command and get the same code to send a different 32 bit output depending on whether it is a 1 or 0?

I imagine the front end will be opening and reading the file, then sending the bit string to a decision loop whereby it is checked for a 1 or 0 value and the loop commands either 32 bit (1's) or 32 bit (0's) depending on that value. These 32 bits are sent to a GPIO to output and transmit.

If this approach is sound, where could I find examples of code to put this together?

Cheers, F

pcmanbob
Posts: 6619
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How to read (rb) a *.txt file and o/p depend on 1 or 0?

Mon Jun 20, 2016 10:16 pm

Hi.

first are you sure the file will contain true binary 1 or 0 and it is not just a number 1 or 0 as i think these would produce different results depending on how you read the file.

the file read and decision make bit would look some think like this if its just numbers in the file ( code pulled from another post I made and not tested )

Code: Select all

current_status = open('status.txt', 'r')
current_status_result = current_status.read()
current_status.close()

if current_status_result == 0:
   
   print " send 32 bit(0) in here"
   
else:
  
   print " send 32 bit(1) in here"
it may still work the same for binary file if you change "r" to "rb"
you need to specify the file name you cant use *.txt as you stated in your question.

if you run in to problems or need help please ask.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

ferg
Posts: 32
Joined: Sun Jan 31, 2016 2:42 am

Re: How to read (rb) a *.txt file and o/p depend on 1 or 0?

Wed Jun 22, 2016 10:02 am

Thanks for your response!!

I was hoping that if I were to open a text or word file with the read binary (rb) access mode, then it would read the file and store it in a temporary memory location as a whole lot of bits. These bits could then be passed through the loop to create the next action depending on the bit type.

Was I hoping for too much??!!? I thought that all files were stored as binary data anyway.

If this isn't the case, what sort of conversion code would I need to look into?

Thanks again.
F

User avatar
B.Goode
Posts: 8225
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to read (rb) a *.txt file and o/p depend on 1 or 0?

Wed Jun 22, 2016 10:35 am

ferg wrote:I thought that all files were stored as binary data anyway.
They are!

For background information about file handling with Python, an internet search tool using the string "python read write file"
returns useful hits including:

https://docs.python.org/3/tutorial/inpu ... ting-files

This comparison of ways of looking at the content of a file might get you started...

Code: Select all

pi@rpi2b ~ $ cat hw.txt
Hello World!
pi@rpi2b ~ $ od -bc hw.txt
0000000 110 145 154 154 157 040 127 157 162 154 144 041 012
          H   e   l   l   o       W   o   r   l   d   !  \n
0000015

maurice1
Posts: 37
Joined: Tue Mar 05, 2013 8:55 am
Location: Dublin

Re: How to read (rb) a *.txt file and o/p depend on 1 or 0?

Sat Jun 25, 2016 7:05 pm

pythons linecache is a good way to read character/s from a text file eg.

X = int (linecache.getline('/var/www/html/Aritech.txt', 1))


Use
linecache.clearcache() when you want to clear it

ferg
Posts: 32
Joined: Sun Jan 31, 2016 2:42 am

Re: How to read (rb) a *.txt file and o/p depend on 1 or 0?

Wed Jun 29, 2016 10:12 am

I've got some basic code happening but I believe that I still have another conversion of the data to go before I can use it. I can get it to pump out a screen of crazy symbols and also a column of byte values (ie 0-255) depending on coding approach, but not binary values.

Code: Select all

#open/read the image
with open("logo.jpg", "rb") as f:
    data = f.read()
    #process the data file for 1s or 0s
    while data:
        if f== 0:
            print "send 0x00 to GPIO"
        else:
            print "send 1x11 to GPIO"
f.close()
Any pointers would be great!!
F

ferg
Posts: 32
Joined: Sun Jan 31, 2016 2:42 am

Re: How to read (rb) a *.txt file and o/p depend on 1 or 0?

Wed Jun 29, 2016 10:58 am

a little epiphany (discovery) since posting;

Code: Select all

with open("logo.jpg", "rb") as f:
    for line in f:
        for byte in line:
            i = ord(byte)
            bin = "{:08b}".format(i)
            print(bin)
This gives me a list of bytes that I can convert to bits.

My next question is; would it be better to convert the list into a long string and pass through the while loop, or to pass each byte (in bits) through the while loop?

Am I going the long way round on this one???
F

User avatar
B.Goode
Posts: 8225
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to read (rb) a *.txt file and o/p depend on 1 or 0?

Wed Jun 29, 2016 11:20 am

ferg wrote: Am I going the long way round on this one???
You seem to be using an image file (*.jpg) as a source of a pseudo-random sequence of bytes from which you will extract 0 or 1 bits? [The specification has changed, because you originally referred to a *.txt file.]

If that's what you are doing, why not simply use a random number library directly from python?

https://docs.python.org/3/library/random.html

ferg
Posts: 32
Joined: Sun Jan 31, 2016 2:42 am

Re: How to read (rb) a *.txt file and o/p depend on 1 or 0?

Wed Jun 29, 2016 11:34 am

Hi,
I read more about text files and images (regarding bit level), found advice that jpg's are simpler to convert, so I thought I'd begin there instead. I'll move to text files next once I've got this working.

I want to break down the image to bit level, send those bits to a GPIO as serial data, transmit them somewhere, receive them somewhere else and recode them into the original image.

Just trying to eat the elephant one mouthful at a time....

ghans
Posts: 7871
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: How to read (rb) a *.txt file and o/p depend on 1 or 0?

Wed Jun 29, 2016 12:18 pm

What kind of communication ? Point-to-point or infrastructure mode ? Simplex or Half-Duplex transmission ? Wired or Wireless ? What bitrate ? Which range ?

See also :

http://xyproblem.info/

What do you want to actually do , what is your final goal ?

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

User avatar
dasmanul
Posts: 502
Joined: Wed Sep 30, 2015 10:20 am
Location: Frankfurt, Germany

Re: How to read (rb) a *.txt file and o/p depend on 1 or 0?

Wed Jun 29, 2016 12:24 pm

If you're treating the file as raw binary, it doesn't really matter what kind of file it is - that's basically what "raw" means in that context from my understanding.

That's also why your approach with "for line in file" won't work since it will treat bytes used for line separation differently from other bytes. You could try something like the following:

Code: Select all

with open("filename","rb") as f:
  byte = f.read(1)
  while len(byte) > 0:
    bits = [ bit == "1" for bit in "{:08b}".format(ord(byte)) ]
    for bit in bits:
      if bit:
        # send 1 via GPIO
      else:
        # send 0 via GPIO
    byte = f.read(1)
I'm sure there's more efficient ways to do it but this should at least work.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: How to read (rb) a *.txt file and o/p depend on 1 or 0?

Wed Jun 29, 2016 3:39 pm

Very minor point but I'd expect python to treat an empty string/byte as False so you could just do "while byte:" in that example.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

ferg
Posts: 32
Joined: Sun Jan 31, 2016 2:42 am

Re: How to read (rb) a *.txt file and o/p depend on 1 or 0?

Thu Jun 30, 2016 10:48 am

Thanks @Dasmanul, I'll try that code to see if it runs better.

@Ghans, At this point in time I'm just trying to get the 'file' into bits and ready to apply to a loop condition. After I've done that I want to reverse the file-to-bits code and make sure the bits-to-file process works too. This is the immediate goal.

The next goal is to take these bits and use them for serial (SPI) comms between GPIO's of separate RPi's. The transmission part with be via addressable LED's, which I already have working as a TX and RX pair. So that's where I'm heading....

Cheers, F

scotty101
Posts: 3670
Joined: Fri Jun 08, 2012 6:03 pm

Re: How to read (rb) a *.txt file and o/p depend on 1 or 0?

Thu Jun 30, 2016 11:04 am

Why do you need to split it in to bits?

The SPI interface will take bytes and serially shift them out bit by bit for you.

The only reason you'd need to split it to bytes is if you wanted to bit bang your own SPI interface on the GPIO pins.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

ferg
Posts: 32
Joined: Sun Jan 31, 2016 2:42 am

Re: How to read (rb) a *.txt file and o/p depend on 1 or 0?

Thu Jun 30, 2016 11:56 am

@Scotty, I'm using addressable LED's and they use 32 bit signals. So I need my code to take each file bit and o/p those 32 bits to GPIO (MOSI) for the LED's. It's not an elegant set-up, but something I hope to build on later.

Ok, building on Dasmanul's example, my next step is to reverse the process for breaking the file into bits. Doing it this way, I can at least see what damage (if any) the coding does to the original image.

Return to “Python”