Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm trying to work with an Elexol USBIO24. It has 24 I/O pins that can be set high or low, or used for input. The code samples provided are in VB, and I don't think I'm converting it to C# properly.

Their datasheet has this:

Opening the Port
As the USBIO24 unit uses binary data transfer we must use the port in binary mode.
If the port number is incorrect or the USBIO24 module is not connected then VB will generate an error.

MSComm1.CommPort = 3 ‘ Set this number as shown in the Device Manager
MSComm1.InputMode = comInputModeBinary ‘ Set Binary Input Mode
MSComm1.PortOpen = True ‘ Open the Port

Setting the pins as Inputs or Outputs
Setting the Ports as Input or Output you must determine the value for the pins you want set as inputs.

To set pins I/O1, I/O2 and I/O3 as inputs and the remaining pins as outputs you simply add the bit values of the input pins 1 + 2 + 4 = 7 and thus the value to be placed in the IOValx variable in the following example code is 7.

‘ Set I/O1, I/O2 & I/O3 of port A to inputs and the rest as outputs.
IOValA = 7 ‘ First 3 inputs all the rest as outputs
IOValB = 0 ‘ All outputs
IOValC = 0 ‘ All outputs
MSComm1.Output = "!A" + Chr$(IOValA) ' Write to Port A Direction Register
MSComm1.Output = "!B" + Chr$(IOValB) ' Write to Port B Direction Register
MSComm1.Output = "!C" + Chr$(IOValC) ' Write to Port C Direction Register

Writing to the Ports
To write to the Output Pins simple repeat the Above without the ! character.
The following example code sets the I/O8 pin on port B to high and the remaining pins as low.

IOValB = 128 ‘ I/O8 high, all the rest low

MSComm1.Output = "B" + Chr$(IOValB)

Can anyone point me toward the equivalent C# for the VB here?
Posted
Comments
Rob Philpott 7-Sep-11 8:41am    
If I remember correctly, MSComm1 in your example is an ActiveX control for communication with serial ports, is this what you're using in C#?

According to http://www.developerfusion.com/tools/convert/vb-to-csharp/[^], it's Strings.Chr(IOValB). I expected a static method on the Char class to take a numeric value, I still wonder if Convert.ToChar would also work.
 
Share this answer
 
Comments
Sunasara Imdadhusen 11-Jul-11 1:15am    
Excellent link and solution! my vote of 5
Chr$ just converts an integer representing a code point to the actual char the code point represents.

MSComm1.Output = "B" + Convert.ToChar(IOValA);


But you have to be careful. If the current thread is MBCS (like Unicode) you may get a larger code point than the PIC allows. You could check that IOVal* is < 128 which will be fine in most cases as simpel code. But to really be sure and robust, see the ASCIIEncoding class.
 
Share this answer
 
Comments
Sunasara Imdadhusen 11-Jul-11 1:15am    
good one!!
you can find readymade C# code for a similar Elexol product here:

http://www.elexol.com/Downloads/EtherIO24DIPRDS1.pdf

You will have to make some modifications since this product is using the Ethernet instead of a usb connection but I think the part of the code used for setting Elexol port direction registers and the port values is identical
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900