Using matplotlib

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

I decided to learn and use matplotlib. Here is the first question:

 

I get the following string from serialPort.readline():

b'\x01 \x01 \x01 \x03\xff \x03\xff\r\n'

How should I convert it to a list of numbers like this: [1, 1, 1, 1023, 1023]?

Slow and Steady!

Last Edited: Fri. Feb 12, 2021 - 10:16 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simple and off the wall, effectively infinite in duration and resolves down to one sample, but not real time: grab your data, save as 16-bit words (add zeros to pad the 10-bit word) and open as a raw file in Audacity.

 

Which is a music player, I know. But you can zoom from the whole file to one pixel and you can zoom in the Y direction too. And it has no problem with files a couple of gig or more.

 

Neil

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

That's a byte array.  What are you expecting to see?  That looks like garbage.

In python 3 you can convert a bytearray to a unicode string using decode if you know the encoding:

>>> b'\x68\x65\x6c\x6c\x6f'.decode("utf-8") 
'hello'
>>> 

 

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

You've already been pointed at example which do this - have you looked at them?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MattRW wrote:

What are you expecting to see?  That looks like garbage.

MCU sends these bytes:

0x01(S1)-0x20(space)-0x01(S2)-0x20(space)-0x01(S3)-0x20(space)-0x03-0xff(ADC0)-0x20(space)-0x03-0xff(ADC1)

I want to separate these values by spaces (I think split method does it) and convert them to a number list: [1, 1, 1, 1023, 1023] (3ffH = 1023D)

I'm using python 3.

 

awneil wrote:

You've already been pointed at example which do this - have you looked at them?

I wish I could look at them because their title seems to be exactly what I need. But I can't open those webpages. Looks like region IP block. sad

Slow and Steady!

Last Edited: Sat. Feb 13, 2021 - 05:44 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
>>> bs = b'\x01 \x01 \x01 \x03\xff \x03\xff\r\n'
>>> bs[0]
1
>>> bs[2]
1
>>> bs[4]
1
>>> 256*bs[6]+bs[7]
1023
>>> 

 

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

This is nothing to do with MathPlotLib - this is just Python.

 

perhaps you should sign-up for this webinar, coming up on 24 Feb:

 

http://www.doulos.com/partner-webinars/python-coding-guidelines-and-idioms

 

via: https://www.toradex.com/webinars

 

EDIT

 

On-demand: https://www.toradex.com/webinars/doulos-python-in-one-hour (from September 2020)

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Mon. Feb 15, 2021 - 02:42 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

pajuhesh80 wrote:

How should I convert it to a list of numbers like this: [1, 1, 1, 1023, 1023]?

With great difficulty. Do yourself a favour and ditch that ambiguous crock of s**t  format you have for incoming data.

 

Had your initial Bytearray been:

 

b'\x00\x01\x00\x01\x00\x01\x03\xff\x03\xff\r\n'

Things would have been so much easier:

>>> ba = b'\x00\x01\x00\x01\x00\x01\x03\xff\x03\xff\r\n'
>>> bl = [int.from_bytes(ba[i:(i+2)], 'big') for i in range(0, len(ba), 2) ]
>>> bl
[1, 1, 1, 1023, 1023, 3338]

Dealing with superfluous conversion of the trailing '\r\n' is left as an exercise for the reader.

 

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

awneil wrote:
This is nothing to do with MathPlotLib - this is just Python.

It's because the original message was in another topic and then it was moved to a new one (this one).

Slow and Steady!

Last Edited: Mon. Feb 15, 2021 - 03:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

pajuhesh80 wrote:
It's because the original message was in another topic and then it was moved to a new one (this one).

but you said:

pajuhesh80 wrote:
I decided to learn and use matplotlib. Here is the first question:

 

 

Anyhow, You can edit the title

 

EDIT

 

add quote

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Mon. Feb 15, 2021 - 04:27 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...