C/C++ in python einbinden

Python in C/C++ embedden, C-Module, ctypes, Cython, SWIG, SIP etc sind hier richtig.
Antworten
Begin
User
Beiträge: 17
Registriert: Donnerstag 26. Mai 2011, 09:32

Hallo,

ich habe mir eine Relais Karte gekauft.
Quancom USBOPTOREL8
Ich habe hier einen Beispielcode in c.
Jetzt möchte ich diesen Code in Python einbinden.
Wie kann ich das machen ?
Durch Cpython oder Ctypes ?
Oder soll ich durch Ctypes nur die Bibliothek einbinden und den Rest in python schreiben.
Vieleicht kennt wer Tutorials wie ich das am einfachsten machen kann.
Vielen Dank
Beispielcode:

Code: Alles auswählen

#include "stdafx.h"
#include "windows.h"
#include "qlib.h"
#include <conio.h>
#include <stdio.h>


// usbmodul.cpp : Sample project for the USBOPTOREL16 and USBOPTOIO Modules
//
// Author: Michael Reimer, QUANCOM Informationssysteme GmbH, Germany
//
// Website: http://www.quancom.de
// Product: 
// USB OPTO I/O Module http://www.quancom.de/qprod01/eng/pb/usbopto16io.htm
// USB OPTO Relay Module http://www.quancom.de/qprod01/deu/pb/usboptorel16.htm
// Information: 
// 
// To use the QLIB Commands in your source, do the following:
//
// (1) Add statement #include "qlib.h" to your source file.
// (2) Add in the Dialog Menu->Project->Settings->C/C++->Preprocessor 
// "$(QLIB_INC)" to the additional include directories entry.
// (3) Add in the Dialog Menu->Project->Settings->Linker->General
// "$(QLIB_LIB)\qlib32.lib" to the additional library and object 
// modules directories entry.                       

int main(int argc, char* argv[])
{
	
	ULONG handle;
	char ch;

	//
	// This sample works for USBOPTOREL16 and USBOPTOIO16 Modules
	// 

	// 
	// Open the USB Module
	// 
	
	handle = QAPIExtOpenCard(USBOPTOREL16,0);

	if ( handle == 0 ) 
		{
		
			handle = QAPIExtOpenCard(USBOPTO16IO,0);
		}

	// 
	// If there are no modules terminate application 
	// 	
	
	if ( handle == 0 )
		{
			MessageBox(NULL, "No USB Modules found!","Error", MB_OK);
			return FALSE;
		}

	// Ok, we found a QUANCOM USB Module

	
	// ---------------------------------------------------------------------------
	// PART 1: Setting the outputs 
	//
	// The following constants can be used to program the outputs: 
	// ---------------------------------------------------------------------------

	#define OUT1	0x1
	#define OUT2	0x2
	#define OUT3	0x4
	#define OUT4	0x8
	#define OUT5	0x10
	#define OUT6	0x20
	#define OUT7	0x40
	#define OUT8	0x80
	#define OUT9	0x100
	#define OUT10	0x200
	#define OUT11	0x400
	#define OUT12	0x800
	#define OUT13	0x1000
	#define OUT14	0x2000
	#define OUT15	0x4000
	#define OUT16	0x8000

	ULONG lines = 0;

	//
	// Reset all lines to "Low"
	//	

	printf("Reset all lines to 'Low' ( Press return to continue ):\n");

	QAPIExtWriteDO16(handle, 0, lines, 0);

	ch = getchar();
	
	//
	// Set the outputs OUT4,OUT10 and OUT15 to "High" ( 16-Bit )
	//	

	printf("Set OUT4,OUT10 and OUT15 to 'High' ( Press return to continue ):\n");

	lines = OUT4 + OUT10 + OUT15;

	QAPIExtWriteDO16(handle, 0, lines, 0);

	ch = getchar();

	//
	// Set the output OUT1, OUT4,OUT10 and OUT15 to "High" ( 16-Bit )
	//	

	printf("Set OUT1, OUT4,OUT10 and OUT15 to 'High' ( Press return to continue ):\n");

	lines = OUT1 + OUT4 + OUT10 + OUT15;

	QAPIExtWriteDO16(handle, 0, lines, 0);

	ch = getchar();

	//
	// Reset line OUT10 to "Low" 
	//	

	printf("Reset line OUT10 to 'Low' ( Press return to continue ):\n");

	QAPIExtWriteDO1(handle, 10 - 1, FALSE, 0);

	ch = getchar();

	//
	// Set line OUT5 to "High" 
	//	

	printf("Set line OUT5 to 'High' ( Press return to continue ):\n");

	QAPIExtWriteDO1(handle, 5 - 1, TRUE, 0);

	ch = getchar();

	//
	// Reset all lines to "Low"
	//	

	printf("Reset all to 'Low' ( Press return to continue ):\n");

	lines = 0;

	QAPIExtWriteDO16(handle, 0, lines, 0);

	ch = getchar();


	// ---------------------------------------------------------------------------
	// PART 2: Reading the inputs ( and detect changed inputs )
	//
	// This part reads the state of the input lines. 	 
	//
	// The following constants can be used to program the inputs: 
	// ---------------------------------------------------------------------------

	int i;


	while (!kbhit())
		{
				
			// read the current state from the inputs		
			
			lines = QAPIExtReadDI16(handle, 0, 0);
			printf("\n--------------------------------------------------------------------------\n");
			printf("Current input states\n");
			printf("IN1  IN2  IN3  IN4  IN5  IN6  IN7  IN8  IN9  IN10 IN11 IN12 IN13 IN14 IN15\n");
			
			for (i=0;i<15;i++)
				{
					if ( lines & 1<<i)
						{
							printf(" 1   ");
						}
					else
						{
							printf(" 0   ");
						}
				}

			// read the state change ff from the input lines

			lines = QAPIExtSpecial(handle, JOB_READ_IN_FFS, 0, 0);

			printf("\n--------------------------------------------------------------------------");
			printf("\nInput change detection 1 = Input has changed its state since last reading\n");
			printf("IN1  IN2  IN3  IN4  IN5  IN6  IN7  IN8  IN9  IN10 IN11 IN12 IN13 IN14 IN15\n");
			for (i=0;i<15;i++)
				{
					if ( lines & 1<<i)
						{
							printf(" 1   ");
						}
					else
						{
							printf(" 0   ");
						}
				}

			printf("\n--------------------------------------------------------------------------\n");
			printf("Press return to stop reading the inputs every 5 seconds ...\n");

			for (int j=0;(j<10) && !kbhit();j++)
				{
					Sleep(500);
				}

		}

		ch = getchar();

		// ---------------------------------------------------------------------------
		// PART 3: Enabling and disabling the timeout control
		//
		// The timeout control is a security feature. If the application
		// crashes or fails to set the outputs in a predefined time
		// the module resets the outputs to 'low'. This is to prevent that
		// connected devices ( i.e. motors, heaters, lamps, ... )
		// continue to run when the software has lost control.
		// ---------------------------------------------------------------------------

		printf("Testing timeout function ...\n");

		printf("Set OUT1,OUT2,OUT5,OUT10 and OUT15 to 'high'\n");

		// writing to OUTx pins

		QAPIExtWriteDO16(handle, 0, OUT1+OUT2+OUT5+OUT10+OUT15, 0);

		printf("Setting timout time to 22.4s\n");

		// first disable timeout

		QAPIExtSpecial(handle, JOB_DISABLE_TIMEOUT, 0, 0);

		// now, set timeout time

		lines = QAPIExtSpecial(handle, JOB_SET_WATCHDOG_TIME, 6, 0);

		printf("Enabling the timout control.\n");

		// reset timeout status bit, this may be set by previous testing

		QAPIExtSpecial(handle, JOB_RESET_TIMEOUT_STATUS, 0, 0);

		// enable timeout control

		QAPIExtSpecial(handle, JOB_ENABLE_TIMEOUT, 0, 0);

		// this part waits for 40s without any write to the usb module
		// after 22.4 seconds the hardware watchdog on the board 
		// sets all outputs to 'low'

		printf("Now we wait for 40 seconds. This simulates a crash !\n");

		for (int j=0;j<40;j++)
			{
				Sleep(1000);
				printf(".");
			}

		printf("\nAll OUT Pins should be reset now ! Press a key to continue !");

		ch = getchar();

		ULONG status = QAPIExtSpecial(handle, JOB_READ_TIMEOUT_STATUS, 0, 0);

		if ( status ) 	
			printf("Timeout flag was set\n");
		else
			printf("No timeout flag set\n");

		// first disable timeout

		QAPIExtSpecial(handle, JOB_DISABLE_TIMEOUT, 0, 0);
		
		// reset timeout status bit

		QAPIExtSpecial(handle, JOB_RESET_TIMEOUT_STATUS, 0, 0);

		printf("\nReady ! Press a key to continue !");

		ch = getchar();

		QAPIExtCloseCard(handle);


		return 0;
}
Zuletzt geändert von Anonymous am Montag 1. August 2011, 09:48, insgesamt 1-mal geändert.
Grund: Quelltexttyp in C geändert.
BlackJack

@Begin: CPython ist die übliche Bezeichnung der in C geschriebenen Python-Implementierung, die man bei python.org bekommt. Du meintest vielleicht Cython — eine Python-Untermenge, die mit dem gleichnamigen Programm in C-Quelltext übersetzt werden kann, der sich zu einem Modul für CPython übersetzen lässt.

Ich würde die DLL (ich nehme mal an es gibt eine) mit `ctypes` ansprechen. Ein kleines Tutorial für `ctypes` gibt es in der Python-Dokumentation. Man muss für so eine Aufgabe C und Python können.

Erster Schritt wäre es die ganzen Funktionen aus der DLL in Python verfügbar zu machen. Im zweiten Schritt sollte man dann schauen, dass man eine „pythonische” API damit erstellt.
Begin
User
Beiträge: 17
Registriert: Donnerstag 26. Mai 2011, 09:32

Danke erstmal für die Antwort.
Leider habe ich keine DLL Datei.
Was würdest du dann machen ?
deets

@Begin

Du hast keine DLL? Und wozu sind dann die qlib32.lib-Kommentare?

Irgendwo muss der Objektcode stehen. Und das wird schon ne DLL sein, schliesslich kann man das auch in LabVIEW benutzen, und das kann nicht statisch linken (was sonst noch eine Moeglichkeit waere)
Begin
User
Beiträge: 17
Registriert: Donnerstag 26. Mai 2011, 09:32

Ja die Lib Datei habe ich. Aber das ist ja nur die Bibliothek.
Wie kann ich den C code miteinbinden?
deets

Du bist sicher, dass du keine DLL hast? Ich habe schon versucht die Bibliothek mal runterzuladen, aber das ist eine EXE, und ich hab' nur Linux hier. Ich bin mir *ziemlich* sicher, dass da ne DLL rumfliegt. Wenn wirklich nicht, dann hilft vielleicht das hier

http://www.openwebspider.org/documentat ... r-windows/

Man kann wohl aus einer statischen lib-Datei eine DLL machen.
Begin
User
Beiträge: 17
Registriert: Donnerstag 26. Mai 2011, 09:32

Kann du bitte von Anfang an erklären ?
Ich glaube du meinst ich soll die Bibliothek, als QLIB32.lib in eine .dll umwandeln.
Dann kann mit Ctypes das ganze in python einbinden oder ?
Und den code den ich habe in python umwandeln.
Ist das ganze so weit richtig ?
BlackJack

@Begin: Du solltest erst einmal sicherstellen, dass Du die DLL nicht doch hast. Ich habe das vorhin mal ausprobiert und da wurde eine Qlib32.dll installiert. Irgendwo unter ``C:\windows\system\`` war das glaube ich. Sorry, hab es wieder gelöscht.
deets

@Begin

Wenn BlackJack recht hat (wovon man ueblicherweise ausgehen kann, er hat ne bessere Quote als der Papst), dann beantwortet sich der Rest deiner Frage mit: ja, dann kannst du den Code mit ctypes nachbilden.
Begin
User
Beiträge: 17
Registriert: Donnerstag 26. Mai 2011, 09:32

Huhu hab sie gefunden.
Sie ist unter Windows system32 gespeichert.
Dann kann ich die Bibliothek jetzt mit ctypes einbinden.
Den Rest schreibe ich per python oder ?
Oder kann man den C code auch irgendwie einbinden ?
Und ja anscheinend hat blackjack immer recht ;)
deets

Wozu willst du denn den C-Code irgendwie einbinden? Das ist doch einfach nur Beispiel-Code. Die Frage muss doch sein, ob man mit ctypes dasselbe erreichen kann. Und die Antwort ist: ja. Musst aber natuerlich vorher etwas Arbeit reinstecken in ein entsprechendes Wrapping, um Funktionen und Typen zu deklarieren. Das geht aber ziemlich einfach.


Natuerlich kannst du auch C-Code von Python aus benutzen. Zum Beispiel, indem du eine DLL erzeugst, die du mit... ctypes einbindest. Oder mit Cython. Aber ich glaube nicht, dass du das wirklich willst, du willst doch von Python aus arbeiten...
EyDu
User
Beiträge: 4881
Registriert: Donnerstag 20. Juli 2006, 23:06
Wohnort: Berlin

Begin hat geschrieben:Und ja anscheinend hat blackjack immer recht ;)
Das haben wir schon vor 3 Jahren geklärt ;-)
Das Leben ist wie ein Tennisball.
Antworten