I am trying to make call for a callback function in python.
I have a dll present at a path say 'dllpath'.
This dll have a callback function stated below:
Function prototype:
ULONG SetByteTotalsCallback(tFNByteTotals pCallback,BYTE interval);
Parameter discription:
tFNByteTotals pCallback: mode-IN, Callback function pointer
BYTE interval:mode-IN, Interval in seconds
Callback Prototype:
void ByteTotalsCallback(ULONGLONG txTotalBytes, ULONGLONG rxTotalBytes );
I want to call the function SetByteTotalsCallback and I want to print the values of txTotalBytes and rxTotalBytes.
I tried with following code:
from ctypes import *
filepath = r"<path to dll>"
gdll = WinDLL(filepath)
tx = c_longlong
rx = c_longlong
pCallback = CFUNCTYPE(tx, rx)
def ByteTotalsCallback(t, r):
try:
print 'Printing tx and rx: '
#print 'Transmitted bytes: ',t[0]
#print 'Received bytes: ',r[0]
#return 0
except:
print 'Error...'
byteTotal_func =pCallback(ByteTotalsCallback)
SetByteTotalsCallback = gdll.SetByteTotalsCallback
try:
print 'Return of SetByteTotals: ',SetByteTotalsCallback(pCallback(ByteTotalsCallback), c_byte(128))
except:
print 'Error found: '
After executing the above code, I observe that the function ByteTotalsCallback is not getting called but SetByteTotalsCallback(pCallback(ByteTotalsCallback), c_byte(128))
got called and returned back successfully.
Can someone help me?
Thanks in advance.
Regards,
Geet
Related
I am using Python to build a script for a program to be ran by Artisan-Scope roasting software. The program already works with my device (the Phidgets 1045_1B) but I need to do more filtering on the temperature readings. I would like the program to sample at 32ms and organize those 30 samples per second in ascending order. I would then like the lowest 10 samples to be average and returned to Artisan software to be graphed.
This is what I have so far but I need help figuring out how to organize the samples and average them before giving one temperature reading to Artisan.
import sys
import time
import traceback
from Phidget22.Devices.TemperatureSensor import *
from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from Phidget22.Net import *
try:
from PhidgetHelperFunctions import *
except ImportError:
sys.stderr.write("\nCould not find PhidgetHelperFunctions. Either add PhdiegtHelperFunctions.py to your project folder "
"or remove the import from your project.")
sys.stderr.write("\nPress ENTER to end program.")
readin = sys.stdin.readline()
sys.exit()
def onAttachHandler(self):
ph = self
try:
#If you are unsure how to use more than one Phidget channel with this event, we recommend going to
#www.phidgets.com/docs/Using_Multiple_Phidgets for information
print("\nAttach Event:")
channelClassName = ph.getChannelClassName()
serialNumber = ph.getDeviceSerialNumber()
channel = ph.getChannel()
ph.setDataInterval(32)
ph.setTemperatureChangeTrigger(0)
except PhidgetException as e:
print("\nError in Attach Event:")
DisplayError(e)
traceback.print_exc()
return
def onDetachHandler(self):
ph = self
try:
except PhidgetException as e:
print("\nError in Detach Event:")
DisplayError(e)
traceback.print_exc()
return
def onErrorHandler(self, errorCode, errorString):
sys.stderr.write("[Phidget Error Event] -> " + errorString + " (" + str(errorCode) + ")\n")
"""
* Outputs the TemperatureSensor's most recently reported temperature.
* Fired when a TemperatureSensor channel with onTemperatureChangeHandler registered meets DataInterval and ChangeTrigger criteria
*
* #param self The TemperatureSensor channel that fired the TemperatureChange event
* #param temperature The reported temperature from the TemperatureSensor channel
"""
def onTemperatureChangeHandler(self, temperature):
#If you are unsure how to use more than one Phidget channel with this event, we recommend going to
#www.phidgets.com/docs/Using_Multiple_Phidgets for information
print("[Temperature Event] -> Temperature: " + str(temperature))
"""
* Prints descriptions of how events related to this class work
"""
def PrintEventDescriptions():
print("\n--------------------\n"
"\n | Temperature change events will call their associated function every time new temperature data is received from the device.\n"
" | The rate of these events can be set by adjusting the DataInterval for the channel.\n"
" | Press ENTER once you have read this message.")
readin = sys.stdin.readline(1)
print("\n--------------------")
"""
* Creates, configures, and opens a TemperatureSensor channel.
* Displays Temperature events for 10 seconds
* Closes out TemperatureSensor channel
*
* #return 0 if the program exits successfully, 1 if it exits with errors.
"""
def main():
try:
ch = TemperatureSensor()
ch.setOnAttachHandler(onAttachHandler)
ch.setDeviceSerialNumber(424909)
ch.setChannel(0)
ch.openWaitForAttachment(5000)
ch.setTemperatureChangeTrigger(0)
ch.setOnDetachHandler(onDetachHandler)
ch.setOnErrorHandler(onErrorHandler)
#This call may be harmlessly removed
PrintEventDescriptions()
ch.setOnTemperatureChangeHandler(onTemperatureChangeHandler)
try:
ch.openWaitForAttachment(5000)
except PhidgetException as e:
PrintOpenErrorMessage(e, ch)
raise EndProgramSignal("Program Terminated: Open Failed")
time.sleep(1)
return 0
except PhidgetException as e:
sys.stderr.write("\nExiting with error(s)...")
DisplayError(e)
traceback.print_exc()
print("Cleaning up...")
ch.close()
return 1
except EndProgramSignal as e:
print(e)
print("Cleaning up...")
ch.close()
return 1
except RuntimeError as e:
sys.stderr.write("Runtime Error: \n\t" + e)
traceback.print_exc()
return 1
finally:
print("Press ENTER to end program.")
readin = sys.stdin.readline()
main()
The first thing that you need is some kind of buffer to hold recorded values until you have enough of them to process them. You can for instance use a python list::
# in onAttachHandler:
# init buffer
global buffer
buffer = []
In onTemperatureChangeHandler, store the values in the buffer. Once the buffer is full, calculate your average, then pass that value on.
# in onTEmperatureChangeHandler
global buffer
buffer.append(temperature)
if len(buffer) > 30:
buffer.sort()
mean_temperature = sum(buffer[:10]) / 10.0
buffer = []
# Do something with mean_temperature here
That said, global variables as used here are considered bad style for good reasons. The code should be improved by defining a class, which has the buffer and all the handlers as attributes. There are plenty of Python tutorials about this.
I want to 'pull' some values from my Arduino in kivy on Raspberry, which is connected via a wireless NRF24 module. I am using this library with a python wrapper
In pure Python the code works well and now I want to integrate it in Kivy.
For that I made two functions inside zimmerwetter.py:
One for setting up the radio device and returns the radio object (should be running once the application starts):
def radiosetup():
radio = RF24(RPI_BPLUS_GPIO_J8_22, RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ)
# doing setup stuff...
return radio
and another function which send an request to the Arduino which delivers some enviroment date (temperatur, humidity etc.).
def getenviroment(self,radio):
millis = lambda: int(round(time.time() * 1000))
# send command
send_payload = 'getdata'
# First, stop listening so we can talk.
radio.stopListening()
# Take the time, and send it. This will block until complete
print 'Now sending length ', len(send_payload), ' ... ',
radio.write(send_payload[:len(send_payload)])
a = datetime.datetime.now()
# Now, continue listening
radio.startListening()
# Wait here until we get a response, or timeout
started_waiting_at = millis()
timeout = False
while (not radio.available()) and (not timeout):
if (millis() - started_waiting_at) > 1000:
timeout = True
# Describe the results
if timeout:
b = datetime.datetime.now()
# print(b - a)
print 'failed, response timed out.'
else:
# Grab the response, compare, and send to debugging spew
length = radio.getDynamicPayloadSize()
receive_payload = []
receive_payload = radio.read(length)
print 'got response size=', length
print struct.unpack("bbbbhbbbb", ''.join(chr(c) for c in receive_payload))
b = datetime.datetime.now()
print(b - a)
return receive_payload
The getenviroment function should be called later every x seconds from the kivy app, the partial function is used as suggested in the kivy clock module
from zimmerwetter import *
class PyowmApp(App):
def build(self):
radio = radiosetup()
Clock.schedule_interval(partial(getenviroment,radio), 10)
The Error is :
File "/home/pi/pyscripts/pyowm/zimmerwetter.py", line 83, in getenviroment
radio.stopListening()
AttributeError: 'float' object has no attribute 'stopListening'
I am wondering why a float object is returned, when I print the radio object with help(radio), it returns class RF24(Boost.Python.instance) and the function stoplistening() exists.
The function called by Clock.schedule_interval will receive dt as an argument after the ones passed through partial. The signature for your function is getenviroment(self,radio), so radio will be assigned to self and dt will be assigned to radio.
Instead, use lambda:
Clock.schedule_once(lambda dt: self.getenviroment(radio), 10)
I've found it out by myself, changing the schedule statement to
Clock.schedule_interval(partial(getenviroment,radio=radio), 10)
did the trick.
I have a script that calls a list of linux guests I am trying to tidy up. Here is the code:
#!/usr/bin/python
guests = ['guest1','guest2','guest3','guest*']
def serverCheck(guestList)
for g in guestList:
server = AdminControl.completeObjectName('cell=tstenvironment,node=guest1,name=uatenvironment,type=Server,*')
try:
status = AdminControl.getAttribute(server, 'state')
print g + status
except:
print "Error %s is down." % g
serverCheck(guests)
The problem lies in this line:
server = AdminControl.completeObjectName('cell=Afcutst,node=%s,name=afcuuat1,type=Server,*') % g
How do I use my list to populate the node variable while still being able to pass the info within the parentheses to the AdminControl function?
The argument string itself is the argument to the % operator, not the return value of the function call.
server = AdminControl.completeObjectName(
'cell=Afcutst,node=%s,name=afcuuat1,type=Server,*' % (g,)
)
Peeking into the crystal ball, Python 3.6 will allow you to write
server = AdminControl.completeObjectName(
f'cell=Afcutst,node={g},name=afcuuat1,type=Server,*'
)
embedding the variable directly into a special format string literal.
can you try like this
AdminControl.completeObjectName('cell=tstenvironment,node=%s,name=uatenvironment,type=Server,*'%g)
For more readability I would suggest this and also using the same way to format strings from variables (here I chose str.format)
guests = ['guest1','guest2','guest3','guest*']
def serverCheck(guestList)
name_tpl = 'cell=tstenvironment,node={},name=uatenvironment,type=Server,*'
for g in guestList:
obj_name = name_tpl.format(g)
server = AdminControl.completeObjectName(obj_name)
try:
status = AdminControl.getAttribute(server, 'state')
print '{}: {}'.format(g, status)
except:
print 'Error {} is down'.format(g)
serverCheck(guests)
I am trying to have the bus loads in PSS/E to change by using python program. So I am trying to write a script in python where I could change loads to different values between two buses in PSS/E.
You can use API routine called "LOAD_CHNG_4" (search for this routin in API.pdf documentation). This routine belongs to the set of load data specification functions. It can be used to modify the data of an existing load in the working case.
Look at chapter II of the PSSE python API. It completely covers changing power flow data. Load data can be referenced with the bus number and load ID. You can change all the parameters you see in the nameplate when you manually enter in the load data itself. Use this function below to change the load:
ierr = psspy.load_chang(bus_number, load_id, intgar,realar)
See page 728 of the API manual for more info.
I wrote this class to handle loads in PSSE...It originally sent with SQL alchemy so it has some extras which I removed:
class Load():
def __init__(self,busnumber,loadID):
# Initialize the Branch Class Instance Variables to a Default 0; Except for the From,To and Circuit ID
self.bus = Bus(busnumber)
self.busI = busnumber
self.loadID = loadID
self.status = 0
self.Pload = 0.0
self.Qload = 0.0
self.errorList = []
self.init()
def init(self):
# Setup the load from the case
# Check to see if bus exists
busOk = self.bus.init()
if not busOk[0]:
return (False,self.bus.number,"The bus number %d is invalid or does not exist..." % self.bus.number)
if psspy.loddt2(self.bus.number,self.loadID,'TOTAL','ACT')[0] != 0:
return (False,False)
Scomplex = self.check(psspy.loddt2(self.bus.number,self.loadID,'TOTAL','ACT'),'loddt2','ACT') # Grab the S vector/phasor from the power flow case for the load specified at the BUS number
self.Pload = Scomplex.real
self.Qload = Scomplex.imag
self.status = self.check(psspy.lodint(self.bus.number,self.loadID,'STATUS'),'lodint','STATUS') # Grab the Status of the Load
return (True,self.bus.number)
def check(self,tuple,funName,subFun):
# Define Error Messages that should be accesable by the end-user and developer
loddt2 = {
0:'No error; P and Q or CMPVAL returned',
1:'Bus not found; P and Q or CMPVAL unchanged.',
2:'Load not found; P and Q or CMPVAL unchanged.',
3:'Bus type code is not 1, 2 or 3; P and Q or CMPVAL returned.',
4:'Load out-of-service; P and Q or CMPVAL returned.',
5:'Invalid value of STRING1 or STRING2; P and Q or CMPVAL unchanged.'
}
lodint = {
0:'No error; IVAL returned.',
1:'Bus not found; IVAL unchanged.',
2:'Load not found; IVAL unchanged.',
3:'Bus type code is not 1, 2 or 3; IVAL returned.',
4:'Load out-of-service; IVAL returned.',
5:'Invalid value of STRING; IVAL unchanged.'
}
funDic = {
'loddt2':loddt2,
'lodint':lodint
}
# Retrieve the Right Error Message
list = funDic[funName]
msg = list[tuple[0]]
if tuple[0] > 0:
logging.debug("Function Name: %s Sub Function Name: %s Error Message: %s"%(funName,subFun,msg))
return tuple[1]
def setLoad(self,loadP):
self.Pload = loadP
def updateCase(self):
# Setup Defaults
_i = psspy.getdefaultint()
_f = psspy.getdefaultreal()
cDef = psspy.getdefaultchar()
psspy.load_data_3(self.bus.number,self.loadID,[self.status,_i,_i,_i,_i],[self.Pload, self.Qload,_f,_f,_f,_f])
# To String Method
def __repr__(self):
return "%d %s %d %d" %(self.bus.number,self.loadID,self.Pload,self.Qload)
# printf Statement that Dumps information to CMD line...Homeage to C and C++
def printf(self):
print "\n Bus: %d \n Load ID: %s \n MW Value: %d \n MVAR Value: %d \n" % (self.bus.number,self.loadID,self.Pload,self.Qload)
If you call the function named, "updateCase" it will take what ever values are stored in the load object and refresh the PSSE case.
http://www.whit.com.au/blog/2011/07/run-psse-from-python-and-not-other-way/
I found this blog post about how to run Python command from PSSE. I don't think that your question has anything to do with ASP.NET though.
This is a simple example of something I'm trying to get working before tackling an actual useful problem. The C code:
typedef struct {
uint32_t seconds;
uint32_t nanoseconds;
} geoTime;
int myTest(geoTime *myTime){
printf("Time: %d %d\n", myTime->seconds, myTime->nanoseconds);
myTime->seconds = myTime->nanoseconds;
geoTime T = {314, 159};
printf("MyTime: %d %d retValue: %d %d\n", myTime->seconds, myTime->nanoseconds, T.seconds, T.nanoseconds);
return 314;
}
The Python code:
import ctypes
import time
import math
lib_astro = ctypes.CDLL("libastroC.so")
class geoTime(ctypes.Structure):
_fields_ = [("seconds", ctypes.c_uint),
("nanoseconds", ctypes.c_uint)]
now = time.time()
print "Python Now: ", now
now_geoTime = geoTime()
now_geoTime.seconds = ctypes.c_uint(int((math.floor(now))))
now_geoTime.nanoseconds = ctypes.c_uint(int(math.floor(math.modf(now)[0] * 1000000000)))
print "Python geoTime now:", now_geoTime.seconds, now_geoTime.nanoseconds
lib_astro.myTest.argtypes = [ctypes.POINTER(geoTime)]
lib_astro.myTest.restype = geoTime
print "************* ENTERING C ********************"
test = lib_astro.myTest(ctypes.byref(now_geoTime))
print "************* EXITING C **********************"
print "Modified now_geoTime: ",now_geoTime.seconds, now_geoTime.nanoseconds
print "test: ",test
Output:
Python Now: 1336401085.43
Python geoTime now: 1336401085 432585000
************* ENTERING C ********************
Time: 1336401085 432585000
MyTime: 432585000 432585000 retValue: 314 159
************* EXITING C **********************
Modified now_geoTime: 432585000 432585000
test: 314
The above code works exactly as I would expect, my pointer goes in and is modified and I get my integer back. The problem happens when I try to create a geoTime structure in C and return that back to Python.
Added/modified code in C:
geoTime patTest(geoTime *myTime){
printf("Time: %d %d\n", myTime->seconds, myTime->nanoseconds);
myTime->seconds = myTime->nanoseconds;
geoTime T = {314, 159};
printf("MyTime: %d %d retValue: %d %d\n", myTime->seconds, myTime->nanoseconds, T.seconds, T.nanoseconds);
return T;
}
Modified Python code:
lib_astro.patTest.argtypes = [ctypes.POINTER(geoTime)]
lib_astro.patTest.restype = geoTime
print "************* ENTERING C ********************"
test = lib_astro.patTest(ctypes.byref(now_geoTime))
print "************* EXITING C **********************"
print "Modified now_geoTime: ",now_geoTime.seconds, now_geoTime.nanoseconds
print "Type of test: ",test
print "Information in test: ", test.seconds, test.nanoseconds
Once I change my code like that the C code gets nonsense into myTime instead of the information from Python and the return value gets placed into now_geoTime instead of test. Any ideas on what might be going wrong? It looks like the python code isn't doing something the way I expect because the C code seems to be working correctly with the values that get passed in.
Output from the last example:
Python Now: 1336404920.77
Python geoTime now: 1336404920 773674011
************* ENTERING C ********************
Time: 90500 -17037640
MyTime: -17037640 -17037640 retValue: 314 159
************* EXITING C **********************
Modified now_geoTime: 314 159
Type of test: <__main__.geoTime object at 0x82bedf4>
Information in test: 137096800 134497384
Any ideas would be greatly appreciated, I've been trying to get this to work for quite a while now. Thanks in advance!
I switched to 64 bit python/64 bit dlls and the problem went away. When I'm feeling a little more inspired I might try to isolate it down to the compiler, the os or python but for now I'm going to run with it.