Arpitthumar
Posts: 32
Joined: Sat Jun 29, 2019 5:39 am

ValueError: chr() arg not in range(0x110000)

Mon Jul 08, 2019 11:34 am

I am using python 3.5 and i am getting error ValueError: chr() arg not in range(0x110000)in my code while i was using python 2.7 i was getting ValueError: chr() arg not in range(256). at if temp == chr(ord(rbuff)+1) or temp == chr(ord(rbuff)-1) or temp == chr(ord(rbuff)+2) or temp == chr(ord(rbuff)-2) or temp == chr(ord(rbuff)):this line.If possible provide me a solution for both python version.

Code: Select all

import threading
import queue
import serial
import time 
import RPi.GPIO as GPIO
count_enable=0
count=0
#i=0
flag=0
transmit_flag=0
rbuff=list(range(100))
temp=0
#zero_detect=1
print("staring program")
ser=serial.Serial('/dev/ttyAMA0',921600,timeout=1)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)

events_queue = queue.Queue()

start = time.time()

def thread_read():
    global count_enable, flag,temp, count
    while True:
        #if transmit_flag==0:
        rbuff=ser.read()
        #for i in range(len(rbuff)):
            #ser.write(rbuff[i])
            #if i >= 100:
            #transmit_flag=1
        if count_enable == 1:
            count+=1
            #print(count)
        if rbuff == chr(0x02) and flag==0: # and zero_detect==1: 
            count_enable=1
            #ser.write(rbuff)
            #print (count)
            flag=1
        if rbuff == chr(0x01) and flag==1  and count==1:
            #ser.write(rbuff)
            #print(count)
            flag=2
        if rbuff == chr(0x04) and flag==2 and count==2:
            #ser.write(rbuff)
            #print(count)
            flag=3
        if rbuff == chr(0x03) and flag==3 and count==3:
            #ser.write(rbuff)
            #print(count)
            flag=4

        if flag==4 and count==28:
            ser.write(rbuff)
            #print(count)
            count=0
            flag=0
        if temp == chr(ord(rbuff)+1) or temp == chr(ord(rbuff)-1) or temp == chr(ord(rbuff)+2) or temp == chr(ord(rbuff)-2) or temp == chr(ord(rbuff)):
            GPIO.output(12,GPIO.LOW)
            #ser.write(ord(buff))
        else:
            GPIO.output(12,GPIO.HIGH)
            temp=rbuff
           # ser.write(ord(buff))

        if count == 1 and rbuff != chr(0x01):
            count_enable=0
            count=0
            flag=0


def thread_check():
    global start
    while True:
        if time.time() - start >= 1:
            start = time.time()
#            print (GPIO.input(12))

read_thread = threading.Thread(target=thread_read)
read_thread.start()

check_thread = threading.Thread(target=thread_check)
check_thread.start()

event=threading.Event()
try:
    event.wait()
except KeyboardInterrupt:
    pass



DirkS
Posts: 10018
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: ValueError: chr() arg not in range(0x110000)

Mon Jul 08, 2019 5:36 pm

Please post the full error msg instead of posting your interpretation...
And use a code block so that it's readable

User avatar
Paeryn
Posts: 2753
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: ValueError: chr() arg not in range(0x110000)

Mon Jul 08, 2019 6:02 pm

ord() will return the unicode code point of the character passed in. In your if statement you have the following

Code: Select all

chr(ord(rbuff)+1)
the problem is, if rbuff contains the very last unicode character then ord(rbuff) will return 0x10ffff, which you proceed to add 1 to and try to convert that back into a character, but chr(0x110000) is invalid because unicode values are 0 to 0x10ffff only.

This is the same problem you saw with Python2, but there characters are single bytes which can have values 0 to 255 rather than Python3's unicode charaters which can have values 0 to 0x10ffff. The moment you tried to get chr(256) it gave you the error because chr() will only accept values from 0 to 255.

You would also have a problem if rbuff has the first valid character chr(0) as later in that if statement you try to do chr(ord(rbuff)-1) and chr(-1) is invalid. Also you try with +2 and -2 so will fail if rbuff is chr(1) or chr(0x10fffe).

Note: 0x10ffff in decimal is 1,112,063.
She who travels light — forgot something.

ghp
Posts: 1421
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: ValueError: chr() arg not in range(0x110000)

Mon Jul 08, 2019 6:21 pm

The python serial got an upgrade between python2 and python3: the return in python3 is a bytes like object (was character in python2).
So if you want to send characters or string over serial in p3, then you need to encode. But if you want to handle bytes, then things get more simple.
So rbuff=ser.read() will return an array, get a byte with rbuff[0].

Code: Select all

import serial
ser = serial.Serial('COM6', 9600)
while True:
    r = ser.read()
	print(r, r[0])

Output is
b'\x00' 0
b'\x01' 1
b'\x02' 2
b'\x03' 3
b'\x04' 4
b'\x05' 5
b'\x06' 6
b'\x07' 7
b'\x08' 8
b'\t' 9
b'\n' 10
b'\x0b' 11
b'\x0c' 12
b'\r' 13
b'\x0e' 14

The arduino test code is:

Code: Select all

void setup() {
  Serial.begin(9600);
}

void loop() {
  for ( int i = 0; i < 256; i ++ )
  {  Serial.write(i);
     delay(100);
  }
}

Arpitthumar
Posts: 32
Joined: Sat Jun 29, 2019 5:39 am

Re: ValueError: chr() arg not in range(0x110000)

Tue Jul 09, 2019 6:18 am

DirkS wrote:
Mon Jul 08, 2019 5:36 pm
Please post the full error msg instead of posting your interpretation...
And use a code block so that it's readable
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "iwrserial3.py", line 59, in thread_read
if temp == chr(ord(rbuff)+1) or temp == chr(ord(rbuff)-1) or temp == chr(ord(rbuff)+2) or temp == chr(ord(rbuff)-2) or temp == chr(ord(rbuff)):
ValueError: chr() arg not in range(0x110000)




While I am using timer code in my code this error is coming but when i am using python 2.7 and no timer code in my code. It is working perfectly fine.

ghp
Posts: 1421
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: ValueError: chr() arg not in range(0x110000)

Tue Jul 09, 2019 4:51 pm

I would propose to stay with python3.
Change places like
if rbuff == chr(0x02) and flag==0: # and zero_detect==1:
into
if rbuff[0] == 0x02 and flag==0: # and zero_detect==1:

and then of course
if temp == chr(ord(rbuff)+1) or temp == chr(ord(rbuff)-1) or temp == chr(ord(rbuff)+2) or temp == chr(ord(rbuff)-2) or temp == chr(ord(rbuff)):
into
if rbuff[0] -2 <= temp[0] <= rbuff[0]+2:

This handles rbuff and temp as byte array.

Arpitthumar
Posts: 32
Joined: Sat Jun 29, 2019 5:39 am

Re: ValueError: chr() arg not in range(0x110000)

Thu Jul 11, 2019 10:54 am

I am getting this error while updating code like you have said.


Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "iwrserial2.py", line 53, in thread_read
if rbuff[0]-2 <= temp <= rbuff[0]+2:
TypeError: unorderable types: int() <= bytes()

User avatar
Paeryn
Posts: 2753
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: ValueError: chr() arg not in range(0x110000)

Thu Jul 11, 2019 12:13 pm

Arpitthumar wrote:
Thu Jul 11, 2019 10:54 am
I am getting this error while updating code like you have said.

Code: Select all

    if rbuff[0]-2 <= temp <= rbuff[0]+2:
TypeError: unorderable types: int() <= bytes()
You haven't written what ghp suggested, you are trying to compare an element of a bytes to a bytes, you need to compare against the first element of temp like ghp said

Code: Select all

 
    if rbuff[0]-2 <= temp[0] <= rbuff[0]+2:
She who travels light — forgot something.

Arpitthumar
Posts: 32
Joined: Sat Jun 29, 2019 5:39 am

Re: ValueError: chr() arg not in range(0x110000)

Fri Jul 12, 2019 5:28 am

Hi Paeryn,

Thanks.The problem has been solved.

Return to “Python”