1

I'm trying to call a COM Method using win32com Python (v3.3) library. This method accepts an address and a data to write to that address.

SWDIOW(IN addr, IN data)

Problem is: accepted size for data is 32bits. When I call this method with such parameters there is no problem:

swdiow(0x400c0000, 0x00000012)

But when data is too big (actually greater than 2^(-31)-1), such as;

swdiow(0x400c0000, 0xF3804770)

This happens:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\WinPython\python-3.3.2.amd64\lib\site-packages\win32com\gen_py\97FF1B53-6477-4BD1-8A29-ADDB58E862E5x0x16x0.py", line 1123, in swdiow
    , data)
OverflowError: Python int too large to convert to C long

I think this problem happens because python takes 0xF3804770 as a signed integer. This value requires 32 bits to be represented as unsigned. To make it signed, Python adds one more sign bit, and size of 0xF3804770 becomes 33 bits. Because of that sign bit, it cannot convert to C long type which I believe the size is 32 bits.

I've found a workaround to this that will convert number to a negative integer when it's too big.

>> int.from_bytes((0xF3804770).to_bytes(4,'big',signed=False),'big', signed=True)
-209696912

Maybe this is the best thing I could do, but I wonder if there is a more elegant solution to this?

By the way, source of my 'data' is a text file that contains 4 bytes HEX values such as this;

D0010880
D1FD1E40
F3EF4770
431103C2
...
4
  • If you have control over the COM interface, have you tried declaring its parameter unsigned? Commented Dec 12, 2013 at 13:11
  • No, I can't do that. Actually documentation doesn't say anything about signed/unsigned. That's the function prototype given in the documentation: SWDIOW(IN addr, IN data) Commented Dec 12, 2013 at 13:15
  • Well, there's this: stackoverflow.com/questions/16452232/… Maybe the COM method will accept it as unsigned, or maybe there's a "clean" way to convert to negative based on this... Commented Dec 12, 2013 at 13:27
  • This : ctypes.c_int32(ctypes.c_uint32(num).value).value , works for converting to negative and it's more clear in code. Thanks. Commented Dec 12, 2013 at 14:17

0

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.