R
R
Rudtoha2017-03-06 02:13:25
Python
Rudtoha, 2017-03-06 02:13:25

How to solve python type conversion error?

I work with CAN adapter directly via COM port (I use CANard)

import canard, serial
from canard import can
from canard.hw import cantact

f = can.Frame(41)
f.dlc = 8
f.data = [32,23,4,5,6,7,8,9]

dev = cantact.CantactDev('COM4')
dev.set_bitrate(125000)
dev.start()
dev.send(f)
Msg = dev.recv()
dev.stop()

Sending a message works correctly, but it's a problem with receiving: ValueError: invalid literal for int() with base 16: ''
The piece of code that the interpreter complains about is in Canard, which is strange. There he is:
rx_str = ""
        while rx_str == "" or rx_str[-1] != '\r':
            rx_str = rx_str + self.ser.read().decode('UTF-8', 'ignore')

        # parse the id, create a frame
        frame_id = int(rx_str[1:4], 16)                       #строка с ошибкой
        frame = can.Frame(frame_id)

I would be very grateful if you could help
PS realterm shows the incoming message

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey S., 2017-03-06
@Rudtoha

And what is wrong ?
You are being told that "" (nothing) is not a valid hexadecimal number

D
Dimonchik, 2017-03-06
@dimonchik2013

if rx_str:
or
rx_str is not None:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question