473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

accessing a device with python

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
Mar 12 '07 #1
6 1926
dshimer
136 Expert 100+
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.
Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2. int      i1;
  3. int      i2;
  4. int      i3;
  5. int      i4;
  6. int      i5;
  7. int      i6;
  8. int      i7;
  9. int      i8;
  10. int      i9;
  11. double   d1, d2, d3;
  12. double   d4, d5, d6;
  13. double   d7;
  14. int      i10;
  15. int      i11;
  16. double   d8;
  17. char     s[260];
  18. } StructName;
I use a format string that looks like
Expand|Select|Wrap|Line Numbers
  1.  format='iiiiiiiiidddddddiid260s'
Then use struct.pack() to convert it to binary which is written to an open file.
Expand|Select|Wrap|Line Numbers
  1.  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)
  2. OpenFile.write(RawData)
  3. OpenFile.flush()
Mar 12 '07 #2
dshimer
136 Expert 100+
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
Expand|Select|Wrap|Line Numbers
  1.    typedef struct {
  2.       char s[81];
  3.       int  i1, i2;
  4.       int  i3, i4;
  5.    } StructName;
  6.  
using
Expand|Select|Wrap|Line Numbers
  1. format='81siiii'
  2. struct.pack(format,sys.argv[1]+'.ext',Int1,Int2,Int3,Int4)
Mar 12 '07 #3
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
Mar 12 '07 #4
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
Mar 12 '07 #5
bartonc
6,596 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2.   enum etype command;
  3.   int command_num;        /* comando n.                               */
  4.   int freq;                      /* frequenza fase rampa costante  */
  5.   unsigned int steps_x;        /* step da fare                                */
  6.   unsigned int steps_y;
  7.   unsigned int steps_z;
  8.   unsigned int steps_a;
  9.   unsigned char dir;              /* byte con le direzioni x y z a         */
  10. } COMMAND_STRUCT;
  11.  
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:
Expand|Select|Wrap|Line Numbers
  1.  
  2. import ctypes
  3. from ctypes import byref
  4. import numpy
  5.  
  6. cbw = ctypes.windll.cbw32 # open CBW32.DLL
  7.  
  8. def CHK(UDStat):
  9.     """raise appropriate exception if error occurred"""
  10.     if UDStat != NOERRORS:
  11.         raise UniversalLibraryError(UDStat)
  12.  
  13.  
  14.  
  15. def cbAInScan(BoardNum, LowChan, HighChan, Count,
  16.               Rate, Gain, ADData,
  17.               Options):
  18.     """Scan range of A/D channels and store samples in an array
  19.  
  20.     Inputs
  21.     ------
  22.  
  23.     BoardNum
  24.     LowChan
  25.     HighChan
  26.     Count
  27.     Rate
  28.     Gain
  29.     ADData -- modified to contain the sampled data
  30.     Options
  31.  
  32.     Outputs
  33.     -------
  34.  
  35.     Rate
  36.  
  37.     """
  38.     Rate = ctypes.c_long(Rate)
  39.     CHK_ARRAY( ADData, Count, numpy.int16 )
  40.     ## here is the library (DLL) call ##
  41.     CHK(cbw.cbAInScan(BoardNum, LowChan, HighChan, Count,
  42.                       byref(Rate), Gain, ADData.ctypes.data, Options))
  43.     return Rate.value
  44.  
  45. class PMD_Model(object):
  46.     def __init__(self, master, BoardNum, Gain):
  47.  
  48.         self.master = master
  49.         self.BoardNum = BoardNum
  50.         self.Gain = Gain
  51.         self.Rate = Rate
  52.         self.error = None
  53.         # this may look better as a module scope variable
  54. ##        self.Options = UL.BACKGROUND + UL.BLOCKIO + UL.CONTINUOUS
  55.         # after coresponding with Robert at Measurement Computing
  56.         self.Options = UL.BACKGROUND + UL.CONTINUOUS
  57.  
  58.         self.ADData = numpy.zeros((Count,), dtype=numpy.int16)
  59.  
  60.  
  61.     def StartScan(self):
  62.         try:
  63.             Rate = UL.cbAInScan(self.BoardNum, LowChan, HighChan, Count,
  64.                                 self.Rate, self.Gain, self.ADData, self.Options)
  65.             self.master.write("Scan Rate = %d, Count = %d" % (Rate, Count))
  66.             self.error = None
  67.         except UL.UniversalLibraryError:
  68.             self.error = self.Errors()
Note that these are snippets and NOT working code!
Mar 13 '07 #6
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
Mar 15 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
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...
1
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...
4
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,...
1
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...
2
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...
3
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...
1
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,...
4
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...
10
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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...
0
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
1
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...
0
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...
0
marktang
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.