1

I downloaded some code from Github for interfacing with a lab equipment:

readBuffer=ctypes.create_string_buffer(1024)
# some C functions modifying readBuffer using ctypes
deviceInfoList=readBuffer.value.split(',')

The last line shows an error: TypeError: a bytes-like object is required, not 'str'

print(type(readBuffer.value)) shows <class 'bytes'>, and print(readBuffer.value) shows b'0,NEWPORT 1936-R v1.2.2 04/06/12 SN22955\r;'.

According to the SDK documentation file, readBuffer is 'a character buffer with the following format: <DeviceID1>,<DeviceDescription1>;<DeviceID2>,<DeviceDescription2>;<DeviceIDX>,<DeviceDescriptionX>. The last line of the code wants to extract the device ID (which is 0).

Split should be a string method if I'm not mistaken, readBuffer.value looks like a byte, while the error complains that it is string not byte. What has gone wrong here?

1 Answer 1

2

Both string and bytes have a split method, that requires an argument of the same type. ',' is not a bytes object - hence the complaint. You want

deviceInfoList=readBuffer.value.split(b',')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.