Put Serial data to list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Put Serial data to list (/thread-29354.html) |
Put Serial data to list - PA3040 - Aug-29-2020 The value send from arduino is 10x12 the data coming to the python slell is b'10x12\r\n' Dear I need to decode the carriage return from the data coming from Arduino Serial port . So I decode it Therefore I used arduino = serial.Serial('COM15',9600) arduino_data = arduino.readline() print(arduino_data) // the value is from arduino_data b'10x12\r\n'// the result is showing in python shellafter decode the arduino_data values = str(arduino_data[0:len(arduino_data)].decode("utf-8")) print(values) 10x12 / the result is showing in the python shell// The print of "value" is 10x12 after decode that is what I need //Then I split it by x to found 10 and 12 because x is my data separator list_values = values.split('x') print(list_values)After split the list_value is ['10', '12\r\n'] It is come back \r\n to the second value '12\r\n'] Please advice --------------------------- full code ist_values = [] value = [] import serial arduino = serial.Serial('COM15',9600) arduino_data = arduino.readline() print(arduino_data) values = str(arduino_data[0:len(arduino_data)].decode("utf-8")) print(values) list_values = values.split('x') print(list_values) value_1 = (list_values[0:2]) value_2 = (list_values[0:1]) print(value_1) print(value_2) RE: Put Serial data to list - DeaD_EyE - Aug-29-2020 This line is horrible: values = str(arduino_data[0:len(arduino_data)].decode("utf-8"))It should be replaced by this: values = arduino_data.decode() # utf8 is implicit the default arduino = serial.Serial('COM15',9600) arduino_data = arduino.readline() # arduino_data reads and writes bytes values = arduino_data.split(b"x") # split at "x", but x must be bytes, if # you work with bytes int_values = list(map(int, values))
By the way, the example allows more than 2 values. If you want to be very strict, you can limit the amount of split: "10x10x10".split("x", maxsplit=1) and you can do it also from the right side and count from there: "10x10x10".rsplit("x", maxsplit=1). Of couse int or float will fail, if you put "10x10" into it, it will raise a ValueError . Just try it with some examples in the python repl.If only two factors are needed, you can replace the map function with: value1 = int(arduino_data[0]) value2 = int(arduino_data[1]) # arduino_data[1] == "10\r\n" # there is still the carriage return and the newline # but they are not required to strip awayThen you can do math with your values. RE: Put Serial data to list - PA3040 - Aug-30-2020 Dear DeaD Grate thanks for the detailed reply My posted code with this line values = str(arduino_data[0:len(arduino_data)].decode("utf-8"))I just add -2 to the (arduino_data)-2 now it is working values = str(arduino_data[0:len(arduino_data)-2].decode("utf-8"))But I know that the way you have point out is the accurate way RE: Put Serial data to list - deanhystad - Aug-30-2020 Serial.readline waits for a newline character ('\n'). Since you did not specify a timeout value for serial.open, and your program does not wait forever, it must be receiving a newline and that is why there is a newline at the end of you string/bytes/whatever. I am not sure why you are also getting the carriage return ('\r'). It must be part of the message that was sent or an artifact of how the message was sent. Use strip() to remove trailing blank space characters like carriage return and new line. arduino_data = arduino.readline().strip() RE: Put Serial data to list - PA3040 - Aug-31-2020 (Aug-30-2020, 02:43 PM)deanhystad Wrote: Serial.readline waits for a newline character ('\n'). Since you did not specify a timeout value for serial.open, and your program does not wait forever, it must be receiving a newline and that is why there is a newline at the end of you string/bytes/whatever. I am not sure why you are also getting the carriage return ('\r'). It must be part of the message that was sent or an artifact of how the message was sent. Dear deanhystad Thanks for the repy It is very clear me what you have point out This is the arduino code int x = 10; int temp = 10; int hum = 12; void setup() { Serial.begin(9600); } void loop() { temp++; hum++; delay(10); Serial.print(temp); Serial.print("x"); Serial.println(hum); delay(2000); }in above code Serial.println(hum);code ( Line number 14 )have println ln is carriage return but the first code Serial.print(temp);( Line number 12 )do not have carriage return but it is showing in the output Please advice About timeout I will check and update here yes your point out is very accurate and technical I change the code as follows arduino = serial.Serial('COM15',9600 , timeout = 1)Arduino code also change by removing carriage return void loop() { temp++; hum++; delay(10); Serial.print(temp); Serial.print("x"); Serial.print(hum); delay(2000); }Now it is working Grate thanks RE: Put Serial data to list - deanhystad - Sep-01-2020 From the Serial.println documentation Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or '\r')and a newline character (ASCII 10, or '\n'). This command takes the same forms as Serial.print(). You get the \r\n from the println. Read the manual. When you have a problem it should be the first place you look. It would have saved you a week or more. |