Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Problem with coding for USB Relay card

Posted on 2015-02-19
11
538 Views
Last Modified: 2015-02-27
Hello,

I found on ebay an dual usb relay card and i want to use that in mij .net application. The problem is that i only found a c++ sample application. Attached i included the software i found and which is working fine.
Can someone help me out with this. I want to have a .net dll to use this board. Or, if i can include thie c++ dll it's also fine, but then i want to know how and how to use it. i need it for c# and/or vb.net.

Can someone have a look please?

Thanks a lot,

Edward
0
Comment
Question by:nijmedw
  • 6
  • 5
11 Comments
 

Author Comment

by:nijmedw
ID: 40618465
I already found a wrapper that can be used, but then i don't know how.

For example, how can i connect to my device, and how can i open the relay with this.
public 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
 }

 [StructLayout(LayoutKind.Sequential)]
 public struct usb_relay_device_info
 {
 [MarshalAs(UnmanagedType.LPStr)]
 public string serial_number;
 public IntPtr device_path { get; set; }
 public usb_relay_device_type type { get; set; }
 public IntPtr next { get; set; }
 }

 public class RelayDeviceWrapper
 {
 [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_enumerate",
 CallingConvention = CallingConvention.Cdecl)]
 //[return: MarshalAs(UnmanagedType.LPStruct)]
 private static extern IntPtr Pusb_relay_device_enumerate();

 public static usb_relay_device_info usb_relay_device_enumerate()
 {
 IntPtr x = RelayDeviceWrapper.Pusb_relay_device_enumerate();
 usb_relay_device_info a = (usb_relay_device_info)Marshal.PtrToStructure(x, typeof(usb_relay_device_info));
 return a;
 }




 [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_init", SetLastError = true,
 CharSet = CharSet.Ansi, ExactSpelling = true,
 CallingConvention = CallingConvention.Cdecl)]
 public static extern int usb_relay_init();


 [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_open_with_serial_number", SetLastError = true,
 CharSet = CharSet.Ansi, ExactSpelling = true,
 CallingConvention = CallingConvention.Cdecl)]
 public static extern int usb_relay_device_open_with_serial_number([MarshalAs(UnmanagedType.LPStr)] string serial_number, int len);


 [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_close", SetLastError = true,
 CharSet = CharSet.Ansi, ExactSpelling = true,
 CallingConvention = CallingConvention.Cdecl)]
 public static extern void usb_relay_device_close(int hHandle);

 [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_open_all_relay_channel", SetLastError = true,
 CharSet = CharSet.Ansi, ExactSpelling = true,
 CallingConvention = CallingConvention.Cdecl)]
 public static extern int usb_relay_device_open_all_relay_channel(int hHandle);


 [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_close_all_relay_channel", SetLastError = true,
 CharSet = CharSet.Ansi, ExactSpelling = true,
 CallingConvention = CallingConvention.Cdecl)]
 public static extern int usb_relay_device_close_all_relay_channel(int hHandle);


 [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_close_one_relay_channel", SetLastError = true,
 CharSet = CharSet.Ansi, ExactSpelling = true,
 CallingConvention = CallingConvention.Cdecl)]
 public static extern int usb_relay_device_close_one_relay_channel(int hHandle, int index);


 [DllImport("usb_relay_device.dll", EntryPoint = "usb_relay_device_open_one_relay_channel", SetLastError = true,
 CharSet = CharSet.Ansi, ExactSpelling = true,
 CallingConvention = CallingConvention.Cdecl)]
 public static extern int usb_relay_device_open_one_relay_channel(int hHandle, int index);

 } 

Open in new window

0
 
LVL 33

Expert Comment

by:sarabande
ID: 40627580
Edward, please add the attachment of the c++ source.

Sara
0
 

Author Comment

by:nijmedw
ID: 40632672
Hello Sara,

Here you find the c++ Dll file (english version)

https://www.dropbox.com/s/18mlhn7our2k3zm/USBRelayExtLib.zip?dl=0

I want to reference to this DLL from C# or vb.net and set the relays from my application.

Thanks a lot.

Edward
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 33

Expert Comment

by:sarabande
ID: 40632723
can you extract the relevant c++ source of the dll and post it here as text between code tags? you also may point to the code parts where you have problems with to use them in your code.

downloading from dropbox and searching for your issues in multiple files is not very convenient and probably I am not the only expert who has a problem with unzipping a file from unknown origin ...

Sara
0
 

Author Comment

by:nijmedw
ID: 40633406
Hello Sara,

I don't have the source code of the c++ dll. I only have the dll file, a .lib and a .h file.

The problem is i don't know c++ and i want to use this relay card within a c# application. As far as i can see, i only can use this c++ dll file.

I want to help you, if you can tell me how.

Kind regards,

Edward
0
 
LVL 33

Expert Comment

by:sarabande
ID: 40633455
the h file is good if it contains the interface to the dll functions.

Sara
0
 

Author Comment

by:nijmedw
ID: 40634830
Hello Sara,

Attached the .h file, i hope it is useful.

Thanks a lot.

Edward
usb-relay-device.h
0
 
LVL 33

Expert Comment

by:sarabande
ID: 40634935
here is the contents of the header file

#ifndef USB_RELAY_DEVICE_H__
#define USB_RELAY_DEVICE_H__

#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

Open in new window


as far as I see it fits perfectly to the c# wrapper class you  posted.

so you could create a c# dll project where you add the wrapper class and use it. the setupapi.lib and the setupapi.dll should be available for the dll project. if you get linker issues (unresolved symbols) you may try to link directly against setupapi.lib. perhaps you may need to ask for more help by c# experts as I have not so much experience with c# and vb project environments.

Sara
0
 

Author Comment

by:nijmedw
ID: 40635164
Thanks Sara,

Just to be clear. I create my C# project and make a class with the wrapper code in it. Or do i need to create a c# project with this class, make a new C# Dll and add that one to my project?

Regards,

Edward
0
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 40635220
both should work. actually the wrapper class looks like it is ready to use. the only hurdle probably is to make the c++ dll available for it. i don't know whether the setupapi.lib is needed. from the wrapper code it looks as if it would access the dll directly without the lib. that would mean you only have to guarantee that the dll is available at runtime. you could copy it to the folder where your c# application resides. that should do it.

Sara
0
 

Author Closing Comment

by:nijmedw
ID: 40635309
Sara,

Thanks a lot for your help, i will have a look if it will work with your suggestions.

Kind regards,

Edward
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question