I am using the Raspberry Pi with the socketcan implementation. Everything is working fine with fix values. To get my simulation more realistic I would like to generate random CAN messages to i.e. simulate various speeds due to traffic on the highway.

After importing all the necessary libaries and bringing my network up my code looks like this:


I get the following error: ValueError: string must be of size 1. Has anyone got an idea how I can get random hex values without running over that error?

New contributor
Philipp is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

put on hold as off-topic by Dmitry Grigoryev, Ingo, Milliways, Darth Vader Feb 13 at 15:59

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be specific to the Raspberry Pi within the scope defined in the help center." – Dmitry Grigoryev, Ingo, Milliways, Darth Vader
If this question can be reworded to fit the rules in the help center, please edit the question.

  • This is a Python programming question which is not related to RPi. It seems that you're trying to assign a string value to a single byte. – Dmitry Grigoryev Feb 11 at 9:33
  • Welcome to Raspberry Pi :-) Please don't use images of text, instead paste the text direct into the question. And please take the short Tour and visit the Help Center to get an idea how things work here. – Ingo Feb 11 at 21:39

From the second line, data is an array of integers.
randomspeed is a string of length 2, containing a hex value, which you want to insert into an int array...
This would only work with a length 1 string interpreted as char.
Remove the hex() function, to insert the random number as integer value, and it should work.

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