Porting example code to python 3

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
anpa_4738671
Level 1
Level 1
First like received

I am running into an issue when trying to run PSoC4/SWD/Python_Ex/Python_Ex.py with python 3.

The issue is in the PSoC4_EraseAll()

  #original code  hResult = pp.PSoC4_WriteProtection(buffer(data1), buffer(data2))

hResult = pp.PSoC4_WriteProtection(memoryview(data1), memoryview(data2))  # port to python 3

From what I googled, memoryview is the replacement for buffer in python 3 but this change produces a windows exception.

  hResult = pp.PSoC4_WriteProtection(memoryview(data1), memoryview(data2))

  File "<COMObject PSoCProgrammerCOM.PSoCProgrammerCOM_Object>", line 2, in PSoC4_WriteProtection

  File "C:\Program Files (x86)\Python38-32\lib\site-packages\win32com\client\dynamic.py", line 287, in _ApplyTypes_

    result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -1114130), None)

I am guessing memoryview is not a drop in replacement?

0 Likes
1 Solution

I have it working now. This is what I had to do.

First, I was using python 3 32-bit. I switched to 64-bit. I am not sure if the 32-bit was the main problem but I think it might be. I haven't gone back to test it.

Second, change to this,

  # Move to OPEN from PROTECTED. It automatically erases Flash and its Protection bits.

        flashProt = []  # do not care in PROTECTED mode

        chipProt = []

        for i in range(0, 1):

            chipProt.append(CHIP_PROT_OPEN)

        data1 = bytearray(flashProt)

        data2 = bytearray(chipProt)

        hResult = pp.PSoC4_WriteProtection(data1, data2)

Unfortunately, I cannot explain why all the others fail, but with this, I can program when the protection bits are set.

View solution in original post

7 Replies
lock attach
Attachments are accessible only for community members.
DheerajK_81
Moderator
Moderator
Moderator
First comment on KBA First comment on blog 5 questions asked

I have updated the example code to work with Python 3.7. Please try the attached code and let me know if it works. You will need to change the hex file based on the device you are using.

Regards,

Dheeraj

0 Likes

Unfortunately, same result.

  hResult = pp.PSoC4_WriteProtection(data1, data2)

  File "<COMObject PSoCProgrammerCOM.PSoCProgrammerCOM_Object>", line 2, in PSoC4_WriteProtection

  File "C:\Program Files (x86)\Python38-32\lib\site-packages\win32com\client\dynamic.py", line 287, in _ApplyTypes_

    result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)

pywintypes.com_error: (-2147417851, 'The server threw an exception.', None, None)

0 Likes

Have you specified the full path to the hex file? Ensure there is access to the hex file and there are no restrictions on reading it. Run it on a drive that does not require admin access etc. Try saving all these files in a folder right below the drive folder in case this issue is being caused because of a long path and checking if it works.

Regards,

Dheeraj

0 Likes

Yes. Everything works when the protection bits are not set. It also works fine with the original script in python 2. And the error is only for this call which has nothing to do with the hex file, correct?

The difference in the files is that the keyword buffer() was removed, so now you are passing the array directly, which seems weird if that would work. Maybe python 3 knows how to convert an array to a raw pointer?  Have you verified this change works? You have to have the protection bit set for this piece of code to even run.

0 Likes

The error does not have anything to do with the hex file itself. I am not able to reproduce this error, so I'm not sure what's causing this. Some of the website sources suggested checking for long path, unicode characters in the file, file permissions, etc. but didn't find anything definitive.

I have the chip protection and the flash protection bits set. I don't see any problems. The write protection API works for me.

You don't need to use buffer or memoryview if the data array is not so large. So, it will work even without it.  I tried using both of them and it still works.

The error you see seems to be related to something else and not specific to the script itself. Can you give more details on your OS version, the path where the script and hex file are present, and anything else that is unique to your environment?

Regards,

Dheeraj

0 Likes

pywin32==227

pywin32-ctypes==0.2.0

The only change I need to make to the script is

SetProtocolConnector(1)  # 10-pin connector

and the hex file name.

Using the PSoC programmer, everything works.

I have two hex files, called protected.hex and notprotected.hex

C:\Users\tpalazzolo\Desktop\notprotected.hex

C:\Users\tpalazzolo\Desktop\protected.hex

Starting with notprotected.hex loaded.

I can successfully load notprotected.hex, with the script.

Then I can successfully load protected.hex also with the script.

Once protected.hex is loaded and I try to program either hex file, with the script, the error happens.

Traceback (most recent call last):

  File "Python3_Ex.py", line 492, in <module>

    hr = Execute()

  File "Python3_Ex.py", line 478, in Execute

    hr = ProgramAll()

  File "Python3_Ex.py", line 341, in ProgramAll

    hr = PSoC4_EraseAll()

  File "Python3_Ex.py", line 208, in PSoC4_EraseAll

    hResult = pp.PSoC4_WriteProtection(data1, data2)

  File "<COMObject PSoCProgrammerCOM.PSoCProgrammerCOM_Object>", line 2, in PSoC4_WriteProtection

  File "C:\Program Files (x86)\Python38-32\lib\site-packages\win32com\client\dynamic.py", line 287, in _ApplyTypes_

    result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)

pywintypes.com_error: (-2147417851, 'The server threw an exception.', None, None)

The only difference is that it runs the command,

hResult = pp.PSoC4_WriteProtection(data1, data2)

which is the cause of the error.

Just to reiterate, when I run the original script with python 2 on the same system, it all works.

If there is any other information I can provide, please let me know.

0 Likes

I have it working now. This is what I had to do.

First, I was using python 3 32-bit. I switched to 64-bit. I am not sure if the 32-bit was the main problem but I think it might be. I haven't gone back to test it.

Second, change to this,

  # Move to OPEN from PROTECTED. It automatically erases Flash and its Protection bits.

        flashProt = []  # do not care in PROTECTED mode

        chipProt = []

        for i in range(0, 1):

            chipProt.append(CHIP_PROT_OPEN)

        data1 = bytearray(flashProt)

        data2 = bytearray(chipProt)

        hResult = pp.PSoC4_WriteProtection(data1, data2)

Unfortunately, I cannot explain why all the others fail, but with this, I can program when the protection bits are set.