USB-Relais ansteuern



  • Hallo zusammen,
    ich bin hier grad etwas am Verzweifeln!
    Habe bei Eckstein eine 4-Kanal-USB-Relaiskarte gekauft und würde diese gerne ganz banal so ansteuern, dass ich ein Programm schreibe, welches einfach alle Relais öffnet sobald ich die .exe ausführe und eine weitere .exe um alle zu schließen.
    Ganz unten auf der Seite ist ein Beispielcode zu finden, aus dem ich nur leider nicht schlau werde.
    Hier mal der Link (ganz unten ist der Code zum Download):
    https://eckstein-shop.de/5V-220V-4-Channel-Relay-Shield-Control-interface-USB-4-Kanal-USB-Relais-Modul?curr=EUR&gclid=Cj0KCQjwvuDPBRDnARIsAGhuAmbp-P-oDtCgF9DK_m_xo6Ju5G_4mOhOsysMOHAYCObDaAu4v3W353oaAqaFEALw_wcB

    Habe mich an das How-to gehalten und ein neues Projekt erzeugt und die mitgelieferte .dll sowie die Headerdatei eingebunden. Auf die beiliegenden Befehle folgen allerdings immer nur diverse Fehlermeldungen.

    Ich wäre für jede Hilfe dankbar!

    Beste Grüße



  • Diverse. Aha.



  • Dieser Fehler kommt aktuell:
    Unbehandelte Ausnahme bei 0x65EA1D07 (usb_relay_device.dll) in Projekt2.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0x00000001


  • Mod

    Dann hast du offensichtlich einen Nullzeiger dereferenziert. Diese Erklärung macht dich aber gewiss auch nicht schlauer. Vielleicht möchtest du wissen, wie man Probleme nachstellbar und nachvollziehbar macht?



  • Bin leider sehr, sehr unbewandert was das Ganze angeht. Meine Idee war ganz simpel folgende:

    #include <stdio.h>
    #include <conio.h>
    #include "usb_relay_device.h"
    #pragma comment(lib, "usb_relay_device.lib")
    
    void main (void)
    {
    	usb_relay_init();
    	usb_relay_device_enumerate();
    
    	usb_relay_device_open_all_relay_channel(255);
    
    	getch();
    }
    

    Im vorgefertigten Header ist folgendes:

    #ifndef USB_RELAY_DEVICE_H__
    #define USB_RELAY_DEVICE_H__
    
    #include "usb_relay_device.h"
    #pragma comment(lib, "usb_relay_device.lib")
    
    #pragma comment(lib, "setupapi.lib")
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    	//#pragma comment(lib, "setupapi.lib")
    
    #ifdef _WIN32
    #define EXPORT_API __declspec(dllexport)
    #else
    #define EXPORT_API 
    #endif
    
    	enum usb_relay_device_type
    	{
    		USB_RELAY_DEVICE_ONE_CHANNEL = 1,
    		USB_RELAY_DEVICE_TWO_CHANNEL = 2,
    		USB_RELAY_DEVICE_FOUR_CHANNEL = 4,
    		USB_RELAY_DEVICE_EIGHT_CHANNEL = 8	
    	};
    
    	/*usb relay board info structure*/
    	struct usb_relay_device_info
    	{
    		unsigned char *serial_number;
    		char *device_path;
    		usb_relay_device_type type;
    		usb_relay_device_info* next;
    	};
    
    	/*init the USB Relay Libary
    	@returns: This function returns 0 on success and -1 on error.
    	*/
    	int EXPORT_API usb_relay_init(void);
    
    	/*Finalize the USB Relay Libary.
    	This function frees all of the static data associated with
    	USB Relay Libary. It should be called at the end of execution to avoid
    	memory leaks.
    	@returns:This function returns 0 on success and -1 on error.
    	*/
    	int EXPORT_API usb_relay_exit(void);
    
    	/*Enumerate the USB Relay Devices.*/
    	struct usb_relay_device_info EXPORT_API * usb_relay_device_enumerate(void);
    
    	/*Free an enumeration Linked List*/
    	void EXPORT_API usb_relay_device_free_enumerate(struct usb_relay_device_info*);
    
    	/*open device that serial number is serial_number*/
    	/*@return: This funcation returns a valid handle to the device on success or NULL on failure.*/
    	/*e.g: usb_relay_device_open_with_serial_number("abcde", 5")*/
    	int EXPORT_API usb_relay_device_open_with_serial_number(const char *serial_number, unsigned len);
    
    	/*open a usb relay device
    	@return: This funcation returns a valid handle to the device on success or NULL on failure.
    	*/
    	int EXPORT_API  usb_relay_device_open(struct usb_relay_device_info* device_info);
    
    	/*close a usb relay device*/
    	void EXPORT_API usb_relay_device_close(int hHandle);
    
    	/*open a relay channel on the USB-Relay-Device
    	@paramter: index -- which channel your want to open
    	hHandle -- which usb relay device your want to operate
    	@returns: 0 -- success; 1 -- error; 2 -- index is outnumber the number of the usb relay device
    	*/
    	int EXPORT_API usb_relay_device_open_one_relay_channel(int hHandle, int index);
    
    	/*open all relay channel on the USB-Relay-Device
    	@paramter: hHandle -- which usb relay device your want to operate
    	@returns: 0 -- success; 1 -- error
    	*/
    	int EXPORT_API usb_relay_device_open_all_relay_channel(int hHandle);
    
    	/*close a relay channel on the USB-Relay-Device
    	@paramter: index -- which channel your want to close
    	hHandle -- which usb relay device your want to operate
    	@returns: 0 -- success; 1 -- error; 2 -- index is outnumber the number of the usb relay device
    	*/
    	int EXPORT_API usb_relay_device_close_one_relay_channel(int hHandle, int index);
    
    	/*close all relay channel on the USB-Relay-Device
    	@paramter: hHandle -- which usb relay device your want to operate
    	@returns: 0 -- success; 1 -- error
    	*/
    	int EXPORT_API usb_relay_device_close_all_relay_channel(int hHandle);
    
    	/*
    	status bit: High --> Low 0000 0000 0000 0000 0000 0000 0000 0000, one bit indicate a relay status.
    	the lowest bit 0 indicate relay one status, 1 -- means open status, 0 -- means closed status.
    	bit 0/1/2/3/4/5/6/7/8 indicate relay 1/2/3/4/5/6/7/8 status
    	@returns: 0 -- success; 1 -- error
    	*/
    	int EXPORT_API usb_relay_device_get_status(int hHandle, unsigned int *status);
    
    #ifndef EXPORT_DLL
    	int EXPORT_API usb_relay_device_set_serial(int hHandle, char serial[5]);
    #endif
    
    #ifdef __cplusplus
    }
    #endif 
    
    #endif //end of ifdef __cplusplus
    


  • Im How-to steht folgendes:

    Ò»£¬How to use in Vistual Studio.
    1. Creat a new C++ Porject
    2. In stdfax.h file, add this code:
    #include "usb_relay_device.h"
    #pragma comment(lib, "usb_relay_device.lib")

    3. Copy the usb_relay_device.dll file into the dir that generate your applicaiton. Like the Release or Debug dir

    ¶þ¡¢The way to use funcation:
    1. call usb_relay_init() to init the lib.
    2. call usb_relay_device_enumerate() to get all the device pluged into pc
    3. call usb_relay_device_open() open the device you need
    4. other operation funcation:
    call sb_relay_device_open_one_relay_channel() to open one way relay
    call usb_relay_device_open_all_relay_channel() to open all relays
    call usb_relay_device_close_one_relay_channel()to close one way relay
    call usb_relay_device_close_all_relay_channel()to close all relays

    One.command test application: CommandApp_USBRelay.exe £º

    usage£ºCommandApp_USBRelay [serial number] [op_type] [relay_index]

    [parameter]
    serial number£ºervery device has a only one serila number. Use the application GuiApp_English.exe to get the serial number.
    op_type£º open--open the relay, close-- close the relay
    relay_index: 01 -- relay one£¬02 -- relay two£¬... 255 indicate the all relays.

    Example:
    1. Open the first relay of the device serial number is afEd5
    CommandApp_USBRelay afEd5 open 01

    2. Open the all relay of the device serial number is afEd5
    CommandApp_USBRelay afEd5 open 255

    3. Close the first relay of the device serial number is afEd5
    CommandApp_USBRelay afEd5 close 01

    4. Close the all relay of the device serial number is afEd5
    CommandApp_USBRelay afEd5 close 255

    If succeed, the command tools will return value 0, else return 1;
    The caller can jude the result of execute from the return value.

    TWO. GuiApp_English.exe is a test application with GUI.
    First, click the "Find Device" button to find the devices that pluged into computer.
    Second, select the device which you want to use from the drop-down list .
    Third, click the "open device" button to open the device you selected, and then you can do operation.



  • usb_relay_device_open_all_relay_channel(255);
    

    255? Woher kommt die Zahl?
    Du überprüfst nie das Ergebnis der Funktionsaufrufe.

    sehr, sehr unbewandert

    Das sind ja ideale Voraussetzungen...



  • Im Header steht:

    struct usb_relay_device_info EXPORT_API * usb_relay_device_enumerate(void);
    

    Daraus kannst du schließen, dass die Funktion usb_relay_device_enumerate einen Zeiger auf eine (oder ein Array von) struct usb_relay_device_info zurückgibt.

    Die darin enthaltenen Daten sind sicher nicht überflüssig.

    Denn die Funktion usb_relay_device_open, die du laut Punkt 3 aufrufen sollst (es aber nicht machst), benötigt einen Zeiger auf so eine struct als Paramter.

    Hast du dich mal mit einfachen (öffnen, lesen/schreiben, schließen) Dateioperationen beschäftigt?


Anmelden zum Antworten