Python receives wierd bytes

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello !

 

My program on the GitHub - https://github.com/LightningWalrus/ATmega328-programs/tree/master/Lightmeter

 

I've made a program which consists of two parts. The C part which takes ADC value and sends it to the computer via serial connection. And the python part which receives the value and stores it in the txt file as shown in the file Example one.txt.

 

My problem is when I change the python code to read the txt file for the existing values first and add new values then, it stores values in this weird way as shown in the Example two.txt.

 

How do I make it read the existing values first and add new values so they display correctly like in the Example one.txt ?

Attachment(s): 

This topic has a solution.
Last Edited: Thu. Nov 3, 2016 - 06:52 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
    fw = open('Light level from ' + time.strftime("%d.%m.%Y") + '.txt', 'w')
    fr = open('Light level from ' + time.strftime("%d.%m.%Y") + '.txt', 'r')

Is this one of these "spot the difference" competitions? Surely that is opening the same file on two different file handles? What's more you open those two then later in the code:

    if byteReceive == 4:
        fw = open('Light level from ' + time.strftime("%d.%m.%Y") + '.txt', 'w')

So you are opening the write file again ?!?

 

Do you have an algorithm diagram to suggest what it was you were actually trying to achieve here?

 

Also you appear to be trying to append to a file by first reading in the entire file then tagging the data on the end and writing all that. You sure??

 

EDIT: I cannot help thinking that the open mode you are looking for here is neither 'r' nor 'w' but 'a' (append).

 

EDIT2: as an example:

C:\pytest>type test.txt
hello
cruel
world

C:\pytest>python
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> file = open("test.txt", "a")
>>> file.write("testing")
>>> file.close()
>>> quit()

C:\pytest>type test.txt
hello
cruel
world
testing
C:\pytest>

 

Last Edited: Wed. Nov 2, 2016 - 04:19 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh thank you clawson ! You saved what's left of my hair and my again !

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I wonder where does that space come from ? It's before every value in first example. I've looked at the C code. It doesn't transmit the space first - it transmits it as the last byte. Even if I take it off the code completely it still appears in the text file.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not sure.

 

Reading about pySerial it looks like the return from sp.read(1) will be a "byte" so there will be an implict conversion as this is written to the file. But I don't think it should be adding a leading space.

 

If I use:

C:\pytest>python
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> file = open("foo.txt", "w")
>>> n = b'123'
>>> file.write(n)
>>> file.close()
>>> quit()

C:\pytest>type foo.txt
123
C:\pytest>

No leading space. But this is the joy of Python - you can test things out interactively so either test what you are doing with a few lines like this or step your actual .py in a Python debugger and watch any data involved.

 

 

Last Edited: Fri. Nov 4, 2016 - 10:34 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Fixed it !

 

#Author: LightningWalrus
# ver 1.0

SERIAL_PORT = "COM6" #chage this to your serial port name, mine was COM6
BAUD_RATE = 9600

import serial
import time

sp = serial.Serial(SERIAL_PORT, BAUD_RATE, timeout = 10)
sp.flush()
print "Well met!"

byteReceive = 0

while(1):
    date = time.strftime("%H:%M")
    response = sp.read()
    byteReceive += len(response) 
    txt = open('Light level from ' + time.strftime("%d.%m.%Y") + '.txt', 'a')
    if byteReceive == 4:
        txt.write(response + ' << '+ date + '\n')
        byteReceive = 0
        txt.close()
    elif byteReceive >= 2:
        txt.write(response)
        txt.close()

I just replaced else with elif byteReceive >=2. If this case, the first byte from sp.read() is skipped and only the next 3 bytes are printed. Why the first byte received is a space is still a mystery for me. Now the program prints a clean record like this:

 

075 << 11:31
074 << 11:32