2

I need some help with data reading. In my situation, I have an Arduino Uno connected via USB to the RPi. The Arduino is sending data to the RPi with Serial.write(). The Arduino outputs 128 bytes each read out period.

Part of the Arduino code which produces the output looks like this:

void loop()
{
  unsigned char ADC_value;
  unsigned char first_byte = 0;

  if(conv_complete)
  {
    if(first_sample)
    {
      first_sample = 0;
      Serial.write(first_byte);
    }
    else
    {
      ADC_value = ADCH;
      Serial.write(ADC_value);
    }
    conv_complete = 0;
  }
}

Currently I have my Python code like this:

import time, serial
import numpy as np
ser = serial.Serial('/dev/ttyACM0',115200)
start = time.time()
PERIOD_OF_TIME = 300
reading = ser.read()
f = open('data.txt','a')
while reading is not None:
    f.write(reading)
    reading = ser.read()
if time.time()>start+PERIOD_OF_TIME:break

I tried using ser.read() but the output I got are a bunch of symbols. If I modify the code in Arduino to Serial.println() I can read the data with ser.readline() on the RPi side but the data obtained doesn't have 128 samples in a period (I only got around 40 samples in a period). I think this could be due to the Serial.println() command which converted 3 bytes into an integer value and these are not usable for me.

I understand there is a difference with Serial.println and Serial.write. Serial.write writes binary data to the serial port and Serial.print prints data to the serial port as human-readable ASCII text.

I want to convert the received data to uint8 format by adding another line of python code like this:

x = np.fromfile(f,dtype=np.uint8)

So that I would be able to see values (0-255). I'm still having trouble with this. Any advise? Any help is much appreciated. :)

  • Hello, and welcome to the RPi SE! Could you post more of your code - how are you opening the ports, what are your baudrate settings, ...? I guess you are using pySerial, right? – LuWi May 1 '14 at 10:50
  • 1
    Hi there, I had made appropriate edits to my post. Sorry for the lack of details. – wengzhe May 4 '14 at 11:17
1

If you want to read binary data you need to do a bit more planning (or specify in more detail what you are sending).

serial.readline() reads data until a newline.

serial.read(n) reads n bytes (unless you have set a timeout).

So you could use serial.read(4) if you were sending bytes in groups of 4. You need to synchronise the reading so you start at the group.

EDIT __________

The data is already bytes!!

If all you want to do is display the bytes (in hex) read try the following (Note I am not a Python expert - this is much easier in c)

#!/usr/bin/python
import serial
ser = serial.Serial('/dev/ttyACM0',115200)
read_byte = ser.read()
while read_byte is not None:
    read_byte = ser.read()
    print '%x' % ord(read_byte)
  • Hi there, I apologise for the extra posts. I had cleared the previous posts and will keep to this post. I had made appropriate edits to this post by adding more details to it. – wengzhe May 4 '14 at 11:22
1

Assuming you are using pySerial.

You need to set a timeout when you instantiate the serial class.

See https://pyserial.readthedocs.io/en/latest/pyserial_api.html for details.

Without a timeout readline will block until a newline is read.

Your program logic will have to deal with readline returning with a timeout.

  • Thank you for the link and explanation. I will proceed to carry out further modifications and tests. – wengzhe May 4 '14 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.