Hi everybody,
first of all I'd like to say that I am a beginner with python so please be patience. I googled a lot before posting the following question.
I am trying to port a small C code of mine which "talks" to a real-time kernel module to python.
I need an easy way to set up a graphic interface therefore I am using tkinter.
I need to send a C struct to /dev/rtf0:
typedef struct {
enum etype command;
int command_num; /* comando n. */
int freq; /* frequenza fase rampa costante */
unsigned int steps_x; /* step da fare */
unsigned int steps_y;
unsigned int steps_z;
unsigned int steps_a;
unsigned char dir; /* byte con le direzioni x y z a */
} COMMAND_STRUCT;
Could anyone please provide a way or a link to somewhere where I could find some example code for doing such thing (ie defining a python equivalent for struct)?
I would also be grateful for links to sites with practical example of tkinter classes use.
Thanks a lot for any help,
Pier
6 1926
This just hints around the edges of what you are asking, but I work with a program that relies heavily on Cstructures for it's data types. I have examples of reading and writing those structures which I need to obfuscate because I didn't develop them and don't own them, but for example given the following generic structure whose variable names have been change to protect the innocent. - typedef struct {
-
int i1;
-
int i2;
-
int i3;
-
int i4;
-
int i5;
-
int i6;
-
int i7;
-
int i8;
-
int i9;
-
double d1, d2, d3;
-
double d4, d5, d6;
-
double d7;
-
int i10;
-
int i11;
-
double d8;
-
char s[260];
-
} StructName;
I use a format string that looks like - format='iiiiiiiiidddddddiid260s'
Then use struct.pack() to convert it to binary which is written to an open file. - RawData=struct.pack(format,self.__i1,self.__i2,self.__i3,self.__i4,self.__i5,self.__i6,self.__i7,self.__i8,self.__i9,self.__d1,self.__d2,self.__d3,self.__d4,self.__d5,self.__d6,self.__d7,self.__i10,self.__i11,self.__d8,self.__s)
-
OpenFile.write(RawData)
-
OpenFile.flush()
I should also probably mention that the whole "self.__" set of characters is because those parameters are in a object of the class defined to hold them. I have done the exact same thing without writing a class but using the same method. For example in one program I grab the filename of a file specified on the command line and pass it along with 4 integers for a structure that is defined as - typedef struct {
-
char s[81];
-
int i1, i2;
-
int i3, i4;
-
} StructName;
-
using - format='81siiii'
-
struct.pack(format,sys.argv[1]+'.ext',Int1,Int2,Int3,Int4)
dshimer
thank you very much.
I'll try and see if I can make something with all the infos you kindly provided.
I'll pop in again if I get to somewhere.
Regards,
Pier
No dice,
I think I am hopeless with this language. I can't see how to make the entry widget work.
I am not able to experiment the struct stuff yet as I cannot get data read and updated from the entry widget. Nonetheless I have found some good examples on how to access date in an entry widget and treat them as strings as well as transform them in integers (or e.g. floats) using the int method.
Any link to a basic tutorials with practical examples would be greatly appreciated.
For those of good will here below is the link to the c... code I have written so far.
Thanks again for any hint.
Regards,
Pier
http://www.pastebin.ca/392463
Hi everybody,
first of all I'd like to say that I am a beginner with python so please be patience. I googled a lot before posting the following question.
I am trying to port a small C code of mine which "talks" to a real-time kernel module to python.
I need an easy way to set up a graphic interface therefore I am using tkinter.
I need to send a C struct to /dev/rtf0: -
typedef struct {
-
enum etype command;
-
int command_num; /* comando n. */
-
int freq; /* frequenza fase rampa costante */
-
unsigned int steps_x; /* step da fare */
-
unsigned int steps_y;
-
unsigned int steps_z;
-
unsigned int steps_a;
-
unsigned char dir; /* byte con le direzioni x y z a */
-
} COMMAND_STRUCT;
-
Could anyone please provide a way or a link to somewhere where I could find some example code for doing such thing (ie defining a python equivalent for struct)?
I would also be grateful for links to sites with practical example of tkinter classes use.
Thanks a lot for any help,
Pier
You can use the ctypes library to treat a dll just like any other module and conver py type to c types:
For example, for a C library function that returns an error code: -
-
import ctypes
-
from ctypes import byref
-
import numpy
-
-
cbw = ctypes.windll.cbw32 # open CBW32.DLL
-
-
def CHK(UDStat):
-
"""raise appropriate exception if error occurred"""
-
if UDStat != NOERRORS:
-
raise UniversalLibraryError(UDStat)
-
-
-
-
def cbAInScan(BoardNum, LowChan, HighChan, Count,
-
Rate, Gain, ADData,
-
Options):
-
"""Scan range of A/D channels and store samples in an array
-
-
Inputs
-
------
-
-
BoardNum
-
LowChan
-
HighChan
-
Count
-
Rate
-
Gain
-
ADData -- modified to contain the sampled data
-
Options
-
-
Outputs
-
-------
-
-
Rate
-
-
"""
-
Rate = ctypes.c_long(Rate)
-
CHK_ARRAY( ADData, Count, numpy.int16 )
-
## here is the library (DLL) call ##
-
CHK(cbw.cbAInScan(BoardNum, LowChan, HighChan, Count,
-
byref(Rate), Gain, ADData.ctypes.data, Options))
-
return Rate.value
-
-
class PMD_Model(object):
-
def __init__(self, master, BoardNum, Gain):
-
-
self.master = master
-
self.BoardNum = BoardNum
-
self.Gain = Gain
-
self.Rate = Rate
-
self.error = None
-
# this may look better as a module scope variable
-
## self.Options = UL.BACKGROUND + UL.BLOCKIO + UL.CONTINUOUS
-
# after coresponding with Robert at Measurement Computing
-
self.Options = UL.BACKGROUND + UL.CONTINUOUS
-
-
self.ADData = numpy.zeros((Count,), dtype=numpy.int16)
-
-
-
def StartScan(self):
-
try:
-
Rate = UL.cbAInScan(self.BoardNum, LowChan, HighChan, Count,
-
self.Rate, self.Gain, self.ADData, self.Options)
-
self.master.write("Scan Rate = %d, Count = %d" % (Rate, Count))
-
self.error = None
-
except UL.UniversalLibraryError:
-
self.error = self.Errors()
Note that these are snippets and NOT working code!
bartonc:
thanks for the reply. Alas I get the:
ImportError: No module named ctypes
message when I try and import ctypes module.
I noticed references to win.dll.... As I am on a linux box could this be the problem?
dshimer:
the module that is supposed to receive the struct packed is whether deaf to python data or nothing is dispatched. I am not sure though about the format structure I provided to the struct.pack method.
http://www.pastebin.ca/396387
Thanks a lot for any help,
Pier
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Fazer |
last post by:
Hello,
I have a MP3 and I want to access the songs in them. I was wondering
if Python could help me do that. I understand that this can be done
using Linux by mountig it as a file system, but...
|
by: Mccormick.Johnw |
last post by:
Hello, ... I am John McCormick (Systems Programmer ) and I am currently
working on a python program which will connect (user) specified inputs
and connect them to (user) selected outputs (like...
|
by: Bradley Grant |
last post by:
Does anyone out there know of where I could find some C#
example programs for accessing USB Devices. There just does
not seem to be much out there on this subject. Even after
doing a Google,...
|
by: Johhny |
last post by:
Hello,
I am currently looking to write a utility in python that will monitor
the statis of a RAID card within linux. The card in Question is the LSI
SAS1064 as the tools provided by the vendor...
|
by: Jimmy Reds |
last post by:
Hi,
I have a blood glucose meter (a Lifescan OneTouch Ultra in case anyone
was wondering) which I connect to my PC using a USB cable and I would
like to have a go at accessing the data on this...
|
by: jodansmif |
last post by:
I've been teaching myself Python as part of my senior design project at
university.
The obstacle our group currently faces is communicating with a
microcontroller (ACS USB Servo II) that appears...
|
by: walterbyrd |
last post by:
I want my python app to read a file from a pocketpc mobile device, if
possible.
Assume I am running windows-xp, and activesync 3.8. Assume I have
"exported" the file.
As I understand it,...
|
by: Phoe6 |
last post by:
Hi all,
I am trying to disable the NIC card (and other cards) enabled in my
machine to test diagnostics on that card.
I am trying to disable it programmatic using python. I checked python
wmi and...
|
by: blaine |
last post by:
Hey everyone,
So I've got a quick query for advice.
We have an embedded device in which we are displaying to an LCD
device that sits at /dev/screen. This device is not readily available
all...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |