C/C++ in python einbinden
Verfasst: Montag 1. August 2011, 09:43
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:
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;
}