I have a motherboard with a kinda of jamma connector, model is axiomtek GMB121.
They provide library for GPIO for windows as dll. The win32 application example works fine.
I'm trying to comunicate with this ddl using python.
I wrote so far, coping from the C code example and converting in python:
from ctypes import *
ERR_Success = 0
ERR_Error = -1
ERR_NotExist = -2
ERR_Opened = -3
ERR_NotOpened = -4
debounce=1
mydll = windll.LoadLibrary("C:\\Users\\test\\Documents\\TestPy\\AGP1_MFC64.dll")
iRet=mydll.AXGMB_Nvram_Open()
if (iRet != ERR_Success and iRet != ERR_Opened):
print "error"
iRet = mydll.AXGMB_DIO_SetDebounceTime(debounce)
if (iRet == ERR_Success):
print "AXGMB_DIO_SetDebounceTime Function success"
else:
print "AXGMB_DIO_SetDebounceTime Function failure"
mydll.AXGMB_DIO_Close()
But what I got is:
iRet=mydll.AXGMB_Nvram_Open()
WindowsError: [Error -532459699] Windows Error 0xE0434F4D
And I don't know how to proceed, any advice?
Related
I've been trying to get this module working but for some reason it looks like it doesn't know its own classes
This is my code:
import pysimpledmx
mydmx = pysimpledmx.DMXConnection(3)
This is the code I need to get from PySimpleDMX:
class DMXConnection(object):
def __init__(self, comport = None):
'''
On Windows, the only argument is the port number. On *nix, it's the path to the serial device.
For example:
DMXConnection(4) # Windows
DMXConnection('/dev/tty2') # Linux
DMXConnection("/dev/ttyUSB0") # Linux
'''
self.dmx_frame = [0] * DMX_SIZE
try:
self.com = serial.Serial(comport, baudrate = COM_BAUD, timeout = COM_TIMEOUT)
except:
com_name = 'COM%s' % (comport + 1) if type(comport) == int else comport
print "Could not open device %s. Quitting application." % com_name
sys.exit(0)
print "Opened %s." % (self.com.portstr)
The error I get when i want to use the DMXConnection:
AttributeError: module 'pysimpledmx' has no attribute 'DMXConnection'
But as you can see, DMXConnection is an attribute of pysimpledmx.
I've tried to reinstall the module with PIP, but that didn't work.
That's an abandoned Python 2 package, either install it and use it with Python 2 version or copy the module file in your project and try to debug it.
I'm using python to communicate with Oscilloscope and Waveform generator via usbmtc and sometimes my usb ports are changing.
So I wrote a function to detect current device:
But if I don't have device usbtmc1 connected, then I have the following error:
Is there are any options to ignore the error?
Use the built-in try to ignore errors in Python.
Combining with logging, you can log the error on screen or to a file.
import logging
for x in range(0, 3):
dev= '/dev/usbtmc' + str(x)
try:
currentUsb = usb.tmc(dev)
currentUsb.write("*IDN?")
name = currentUsb.read(300)
if name.find('DSO') >-1:
scope= usb.tmc(dev)
print 'scope '
elif name.find('33621A') >-1:
waveform = usb.tmc(dev)
print 'waveform'
except OSError:
logging.info('Something is wrong')
pass
I tried upload scenario with keyboard press simulation through ctypes package in python for selenium webdriver. It is working fine with my local machine installed with windows 8.1.
But when i run the same code in my development server which is a linux box, that will call a remote machine of windows 7 OS, i got error like windll not found in this part of my code
def SendInput(*inputs):
nInputs = len(inputs)
LPINPUT = INPUT * nInputs
pInputs = LPINPUT(*inputs)
cbSize = ctypes.c_int(ctypes.sizeof(INPUT))
return ctypes.windll.user32.SendInput(nInputs, pInputs, cbSize)
So I did change my code to a if else statement which prompts if the OS is windows got to above snippet of code else go to below snippet of code,
cdll.LoadLibrary("libc.so.6")
Xtst = cdll("libXtst.so.6")
Xlib = cdll("libX11.so.6")
dpy = Xtst.XOpenDisplay(None)
def SendInput(txt):
for c in txt:
sym = Xlib.XStringToKeysym(c)
code = Xlib.XKeysymToKeycode(dpy, sym)
Xtst.XTestFakeKeyEvent(dpy, code, True, 0)
Xtst.XTestFakeKeyEvent(dpy, code, False, 0)
Xlib.XFlush(dpy)
But after adding this i am getting error in my linux box like
TypeError: 'LibraryLoader' object is not callable.
I did search for resources over internet, but i was not able to get them. Can someone help me to get it through.
I am trying to use Microsoft.Office.Interop.Excel from a Python script:
import msvcrt
import clr
clr.AddReference("Microsoft.Office.Interop.Excel")
import Microsoft.Office.Interop.Excel as Excel
excel = Excel.ApplicationClass()
excel.Visible = True # makes the Excel application visible to the user - will use this as True for debug
excel.DisplayAlerts = False # turns off Excel alerts since I don't have a handler
print ("Excel: " + str(type(excel)))
print ("Workbooks: " + str(type(excel.Workbooks)))
print ("Workbooks count: " + str(excel.Workbooks.Count))
#wb = excel.Workbooks.Open(r'C:\Projects\Experiments\Python\ExcelInterop\Test.xlsx')
print ("Press any key")
msvcrt.getch()
Here is the output:
C:\Projects\Experiments\Python\ExcelInterop>exceltest.py
Excel: <class 'Microsoft.Office.Interop.Excel.ApplicationClass'>
Workbooks: <class 'System.__ComObject'>
Traceback (most recent call last):
File "C:\Projects\Experiments\Python\ExcelInterop\exceltest.py", line 12, in <module>
print ("Workbooks count: " + str(excel.Workbooks.Count))
AttributeError: '__ComObject' object has no attribute 'Count'
I am running in an admin cmd prompt on Windows 10
I have tried python 2.7 and 3.5 (using py -3 exceltest.py).
Microsoft.Office.Interop.Excel is version 15.0.4420.1017 (Office 2013)
I created a similar .NET console app, which worked fine.
I used ILDASM to check references from Microsoft.Office.Interop.Excel, and then gacutil -l to double-check that all referenced assemblies are in the GAC.
Just in case, I copied office.dll, stdole.dll and Microsoft.Vbe.Interop.dll into the folder where the Python script is running. These are the assemblies added to the .NET console application build folder if I don't embed the interop types for Microsoft.Office.Interop.Excel.
I have .NET for Python installed for 2.7 and 3.5 (from https://pypi.python.org/pypi/pythonnet)
Thanks for reading.
You can only get to COM objects from pythonnet using reflection currently:
clr.AddReference("System.Reflection")
from System.Reflection import BindingFlags
excel.Workbooks.GetType().InvokeMember("Count",
BindingFlags.GetProperty |
BindingFlags.InvokeMethod,
None, excel.Workbooks, None)
Here is a wrapper function:
https://gist.github.com/denfromufa/ec559b5af41060c5ac318f7f59d8b415#file-excel_interop_vsto-ipynb
I am trying to find out if a given executable (or library) is compiled for 32-bits or 64-bits from Python. I am running Vista 64-bits and would like to determine if a certain application in a directory is compiled for 32-bits or 64-bits.
Is there a simple way to do this using only the standard Python libraries (currently using 2.5.4)?
The Windows API for this is GetBinaryType. You can call this from Python using pywin32:
import win32file
type=GetBinaryType("myfile.exe")
if type==win32file.SCS_32BIT_BINARY:
print "32 bit"
# And so on
If you want to do this without pywin32, you'll have to read the PE header yourself. Here's an example in C#, and here's a quick port to Python:
import struct
IMAGE_FILE_MACHINE_I386=332
IMAGE_FILE_MACHINE_IA64=512
IMAGE_FILE_MACHINE_AMD64=34404
f=open("c:\windows\explorer.exe", "rb")
s=f.read(2)
if s!="MZ":
print "Not an EXE file"
else:
f.seek(60)
s=f.read(4)
header_offset=struct.unpack("<L", s)[0]
f.seek(header_offset+4)
s=f.read(2)
machine=struct.unpack("<H", s)[0]
if machine==IMAGE_FILE_MACHINE_I386:
print "IA-32 (32-bit x86)"
elif machine==IMAGE_FILE_MACHINE_IA64:
print "IA-64 (Itanium)"
elif machine==IMAGE_FILE_MACHINE_AMD64:
print "AMD64 (64-bit x86)"
else:
print "Unknown architecture"
f.close()
If you're running Python 2.5 or later on Windows, you could also use the Windows API without pywin32 by using ctypes.
from ctypes import windll, POINTER
from ctypes.wintypes import LPWSTR, DWORD, BOOL
SCS_32BIT_BINARY = 0 # A 32-bit Windows-based application
SCS_64BIT_BINARY = 6 # A 64-bit Windows-based application
SCS_DOS_BINARY = 1 # An MS-DOS-based application
SCS_OS216_BINARY = 5 # A 16-bit OS/2-based application
SCS_PIF_BINARY = 3 # A PIF file that executes an MS-DOS-based application
SCS_POSIX_BINARY = 4 # A POSIX-based application
SCS_WOW_BINARY = 2 # A 16-bit Windows-based application
_GetBinaryType = windll.kernel32.GetBinaryTypeW
_GetBinaryType.argtypes = (LPWSTR, POINTER(DWORD))
_GetBinaryType.restype = BOOL
def GetBinaryType(filepath):
res = DWORD()
handle_nonzero_success(_GetBinaryType(filepath, res))
return res
Then use GetBinaryType just like you would with win32file.GetBinaryType.
Note, you would have to implement handle_nonzero_success, which basically throws an exception if the return value is 0.
I've edited Martin B's answer to work with Python 3, added with statements and ARM/ARM64 support:
import struct
IMAGE_FILE_MACHINE_I386 = 332
IMAGE_FILE_MACHINE_IA64 = 512
IMAGE_FILE_MACHINE_AMD64 = 34404
IMAGE_FILE_MACHINE_ARM = 452
IMAGE_FILE_MACHINE_AARCH64 = 43620
with open('foo.exe', 'rb') as f:
s = f.read(2)
if s != b'MZ':
print('Not an EXE file')
else:
f.seek(60)
s = f.read(4)
header_offset = struct.unpack('<L', s)[0]
f.seek(header_offset + 4)
s = f.read(2)
machine = struct.unpack('<H', s)[0]
if machine == IMAGE_FILE_MACHINE_I386:
print('IA-32 (32-bit x86)')
elif machine == IMAGE_FILE_MACHINE_IA64:
print('IA-64 (Itanium)')
elif machine == IMAGE_FILE_MACHINE_AMD64:
print('AMD64 (64-bit x86)')
elif machine == IMAGE_FILE_MACHINE_ARM:
print('ARM eabi (32-bit)')
elif machine == IMAGE_FILE_MACHINE_AARCH64:
print('AArch64 (ARM-64, 64-bit)')
else:
print(f'Unknown architecture {machine}')
I was able to use Martin B's answer successfully in a Python 3.5 program after making this adjustment:
s=f.read(2).decode(encoding="utf-8", errors="strict")
Originally it worked just fine with my program in Python 2.7, but after making other necessary changes, I discovered I was getting b'MZ', and decoding it appears to fix this.
Using Python 3.7, 32 bit on 64 bit Win 7, the first code fragment in the top answer doesn't run for me. It fails because GetBinaryType is an unknown symbol. Solution is to use win32file.GetBinaryType.
Also running it on a .pyd file doesn't work, even if it is renamed to a .dll. See next:
import shutil
import win32file
from pathlib import Path
myDir = Path("C:\\Users\\rdboylan\\AppData\\Roaming\\Python\\Python37\\site-packages\\pythonwin")
for fn in ("Pythonwin.exe", "win32ui.pyd"):
print(fn, end=": ")
myf = myDir / fn
if myf.suffix == ".pyd":
mytemp = myf.with_suffix(".dll")
if mytemp.exists():
raise "Can not create temporary dll since {} exists".format(mytemp)
shutil.copyfile(myf, mytemp)
type = win32file.GetBinaryType(str(mytemp))
mytemp.unlink()
else:
type=win32file.GetBinaryType(str(myf))
if type==win32file.SCS_32BIT_BINARY:
print("32 bit")
else:
print("Something else")
# And so on
Results in
Pythonwin.exe: 32 bit
win32ui.pyd: Traceback (most recent call last):
File "C:/Users/rdboylan/Documents/Wk devel/bitness.py", line 14, in <module>
type = win32file.GetBinaryType(str(mytemp))
pywintypes.error: (193, 'GetBinaryType', '%1 is not a valid Win32 application.')