Reputation: 23
I have made a PCB circuit that has a microcontroller (Microchip PIC18F47K40
) that can program a parallel flash memory (Microchip SST39SF040
).
The microcontroller communicates to my PC (Windows 10) via Modbus RTU
over a USB to UART adaptor.
I use five Modbus registers...
Control, Address0, Address1, Address2, Data
Control = read, write, erase
Address0,1,2 = Address bus
Data = Data bus
Using Baseblock program I have verified the microcontroller is working fine, and in Python I can send individual commands ok.
My goal is to be able to read in a binary file and send to the microcontroller to program the flash memory chip.
This is the code I have so far: -
#!/usr/bin/env python3
import minimalmodbus
instrument = minimalmodbus.Instrument('COM8', 1)
instrument.serial.baudrate = 9600
instrument.close_port_after_each_call = True
addressCount = 0
with open("rom.bin", "rb") as f:
while (dataByte := f.read(1)):
addressBytes = ( (addressCount).to_bytes(3, byteorder='little') )
addressCount += 1
instrument.write_register(0, 0x3, 0)
instrument.write_register(1, addressBytes[0], 0)
instrument.write_register(2, addressBytes[1], 0)
instrument.write_register(3, addressBytes[2], 0)
#instrument.write_register(4, dataByte.hex(), 0)
print(dataByte.hex())
The line that is causing me problems is: -
instrument.write_register(4, dataByte.hex(), 0)
The error message: -
Exception has occurred: TypeError
The input value must be numerical. Given: '85'
File "X:\Code\Python\1-modbus_master_flas_read_write_erase\#4 - numpy.py", line 17, in <module>
instrument.write_register(4, dataByte.hex(), 0)
When I check the type for dataByte.hex()
it comes back as a str
...
So, my question, how do I convert the str
to an int
?
Upvotes: 1
Views: 755
Reputation: 1874
You can use int(dataByte.hex())
databyte = b'101010'
databyte = databyte.hex()
print(type(databyte)) # <class 'str'>
databyte = int(databyte,16)
print(type(databyte)) # <class 'int'>
Upvotes: 1