|
Thx m8
|
|
|
|
|
HI, guys
I am interested in operating USB port in C# . But I don't konw how to do that.
Anybody have some idea about it. Thank you
vigorous
|
|
|
|
|
The .Net framework doesn't allow you to manipulate hardware. You would have to use C++ to accomplish what you want.
Aaron Eldreth
TheCollective4.com
My Articles
While much is too strange to be believed,
Nothing is too strange to have happened.
- T. Hardy
|
|
|
|
|
You can do it with C#, I have working code right here...
This is maddness, I can't believe you said that.
/\ |_ E X E GG
|
|
|
|
|
You'd have to be ALOT more specific about what you want to do. On top of that, how to do it will be vendor and device dependent because you'll be interacting with the driver for that device. Also, since all the NT kernel versions of Windows (NT, 2000, XP, 2003) abstract the hardware, you can't get to the hardware directly. You have to go through either the device driver for the device you want, or write your own device driver that exposes an interface to your managed code (C#).
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Elaborate please.
/\ |_ E X E GG
|
|
|
|
|
In the future, I will need get data from a usb port connected with a bar scanner. The bar scanner will scan a bar graph and send them to the PC by usb. At the moment, I still not know more details about the scanner. So I just want to know some idea about operating usb port.
For example, I know we can use MSCom control or API to control serial port. But how can I use usb port?
If you can provide some code, I will feel very very grateful for your help.
Thank you very much.
vigorous
|
|
|
|
|
Alright, I think I have the idea...
I know microsft already provides generic drivers for scanners, so you can use that instead of the one that comes with the scanner (which is probably proprietry and not documented). SO, you wont' have to write your own driver, thats good.
Um, I suggest you maybe get a development kit, EZ-USB[^]. This will give you some visual results. although it's not necessary. These kits though, come with external memory, a UART and serial connector, a set of push buttons, and an LED readout to facilitate development and debugging of your software. SO, that will help alot in your learning process.
Next you need to learn the fundamentals of USB, like writing to a bus, etc. It's much different then the serial port.
So just wrap the usb functions in c++ and then you can simply call the methods in your c# app/gui. It's a little more complicated then that though.... I have some code if you want to see it.
Actually, everything is in that kit. It's too hard to type all this with one hand. (my wrist is broken.)
/\ |_ E X E GG
|
|
|
|
|
This complies into a nice .net library with 2 main methods (bulk read, write) that I can seamsly use in my C# app. Actually with no pinvoking.
#include "stdafx.h"
#include "csusb.dll"
#include "ezusbsys.h"
#include "usb100.h"
using namespace System;
using namespace System::Text;
using namespace System::Runtime::InteropServices;
namespace EzUsb
{
[StructLayout(LayoutKind::Explicit, Size=18, CharSet=CharSet::Auto)]
public __gc class UsbDeviceDescriptor
{
public:
[FieldOffset(0)] System::Byte bLength;
[FieldOffset(1)] System::Byte bDescriptorType;
[FieldOffset(2)] System::UInt16 bcdUSB;
[FieldOffset(4)] System::Byte bDeviceClass;
[FieldOffset(5)] System::Byte bDeviceSubClass;
[FieldOffset(6)] System::Byte bDeviceProtocol;
[FieldOffset(7)] System::Byte bMaxPacketSize0;
[FieldOffset(8)] System::UInt16 idVendor;
[FieldOffset(10)] System::UInt16 idProduct;
[FieldOffset(12)] System::UInt16 bcdDevice;
[FieldOffset(14)] System::Byte iManufacturer;
[FieldOffset(15)] System::Byte iProduct;
[FieldOffset(16)] System::Byte iSerialNumber;
[FieldOffset(17)] System::Byte bNumConfigurations;
};
public __gc class Usb
{
private:
HANDLE _hEzUsb;
void ReportError( String* msg )
{
if( OnErrorMessage )
{
OnErrorMessage( this, new ErrorEventArgs( msg ) );
}
}
void CheckWin32Error()
{
int errCode = GetLastError();
if( errCode != 0 )
{
char msg[256];
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, errCode, 0,
msg, 256, NULL );
ReportError( msg );
}
}
public:
static const int MaxBlkSize = 64;
__delegate void ErrorMessage( Object* sender, ErrorEventArgs* e );
__event ErrorMessage* OnErrorMessage;
Usb()
{
_hEzUsb = INVALID_HANDLE_VALUE;
}
~Usb()
{
if( _hEzUsb != INVALID_HANDLE_VALUE )
{
CloseHandle( _hEzUsb );
}
}
bool Open( String* driverName )
{
char* lpFileName = new char[driverName->Length + 10];
sprintf( lpFileName, "\\\\.\\%s", driverName );
_hEzUsb = CreateFile( lpFileName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if( _hEzUsb == INVALID_HANDLE_VALUE )
{
CheckWin32Error();
}
delete lpFileName;
return( _hEzUsb != INVALID_HANDLE_VALUE );
}
void Close()
{
CloseHandle( _hEzUsb );
}
Usb::UsbDeviceDescriptor* GetDeviceDescriptor()
{
BOOLEAN success;
ULONG nBytes;
PUSB_DEVICE_DESCRIPTOR pDeviceDiscriptor = new USB_DEVICE_DESCRIPTOR();
if( _hEzUsb == INVALID_HANDLE_VALUE )
{
ReportError( "Driver not open." );
}
else
{
success = DeviceIoControl( _hEzUsb,
IOCTL_Ezusb_GET_DEVICE_DESCRIPTOR,
NULL,
0,
pDeviceDiscriptor,
sizeof(USB_DEVICE_DESCRIPTOR),
&nBytes,
NULL );
if( success == FALSE )
{
CheckWin32Error();
}
}
UsbDeviceDescriptor* DeviceDiscriptor = new UsbDeviceDescriptor();
DeviceDiscriptor = (UsbDeviceDescriptor*)Marshal::PtrToStructure(
pDeviceDiscriptor, DeviceDiscriptor->GetType() );
delete pDeviceDiscriptor;
return( DeviceDiscriptor );
}
System::Byte BulkRead( int pipe ) []
{
if( _hEzUsb == INVALID_HANDLE_VALUE )
{
ReportError( "Driver not open." );
return( 0 );
}
char lpOutBuffer[MaxBlkSize];
BULK_TRANSFER_CONTROL btc;
btc.pipeNum = pipe;
BOOL success;
int nBytes;
success = DeviceIoControl( _hEzUsb,
IOCTL_EZUSB_BULK_READ,
&btc,
sizeof (BULK_TRANSFER_CONTROL),
lpOutBuffer,
MaxBlkSize,
(unsigned long *)&nBytes,
NULL);
if( success == FALSE )
{
CheckWin32Error();
return( 0 );
}
System::Byte buffer __gc[] = new System::Byte[nBytes];
IntPtr ptr( lpOutBuffer );
Marshal::Copy( ptr, buffer, 0, nBytes );
return( buffer );
}
int BulkWrite( System::Byte buffer __gc[], int pipe )
{
if( _hEzUsb == INVALID_HANDLE_VALUE )
{
ReportError( "Driver not open." );
return( 0 );
}
char* lpOutBuffer = new char[buffer->Length];
Marshal::Copy( buffer, 0, lpOutBuffer, buffer->Length );
BULK_TRANSFER_CONTROL btc;
BOOLEAN success;
int nBytes;
btc.pipeNum = pipe;
success = DeviceIoControl( _hEzUsb,
IOCTL_EZUSB_BULK_WRITE,
&btc,
sizeof (BULK_TRANSFER_CONTROL),
lpOutBuffer,
buffer->Length,
(unsigned long *)&nBytes,
NULL);
if( !success )
{
CheckWin32Error();
}
delete lpOutBuffer;
return( nBytes );
}
};
}
/\ |_ E X E GG
|
|
|
|
|
Thank you very much. I am sure your words are very useful and helpful. And I am so sorry to hear about your wrist and hope you will be well quickly.
Best regards
vigorous
|
|
|
|
|
Hey All,
I was just wondering if anybody knows of a "Data Formatting Expression" that I can use with a bound column (of type string) in a datagrid to say something like "If the string is > x characters long, trim it and put '...' at the end"???
Currently I'm manually (i.e. programmatically) checking the lenght and altering the string, if needs be, before the ASP.NET page is rendered which works fine but it's a little slow (and using a formatting expression would be a lot neater) and I can't find a table of formatting expression in the MSDN library.
Any help would be appreciated.
Regards,
Brian Dela
http://www.briandela.com IE 6 required. http://www.briandela.com/pictures Now with a pictures section http://www.briandela.com/rss/newsrss.xml RSS Feed
|
|
|
|
|
There is no Data Formatting Expression for that, simply because it is font and available screen space dependent. The best way to do that is the way your doing it now...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Dave Kreskowiak wrote:
simply because it is font and available screen space dependent
How is the number of characters in a string font and screen space dependent??? If I have code like this...
string myLovelyHorsey = "A beautiful song from Fr. Ted!";
..the number of characters is the string has nothing to do with font or screen space...
What I wanted to do was not based on the amount of space taken up on screen but based on the number of characters in the string.
Regards,
Brian Dela
http://www.briandela.com IE 6 required. http://www.briandela.com/pictures Now with a pictures section http://www.briandela.com/rss/newsrss.xml RSS Feed
|
|
|
|
|
Mostly, you'll see, what you want to do, is based on the available screen space in a row of a table. Like the Subject field in Outlook when you open your Inbox and have all your messages listed in the right pane, the column the Subject is displayed in can be any width. That width is dependent on the width of the column and the size of font it is displaying.
But, in either case, be it on-screen or simply a maximum number of characters, there is no formatting expression that will cover the functionality you want.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Dave Kreskowiak wrote:
Mostly, you'll see, what you want to do, is based on the available screen space in a row of a table. Like the Subject field in Outlook when you open your Inbox and have all your messages listed in the right pane, the column the Subject is displayed in can be any width. That width is dependent on the width of the column and the size of font it is displaying
Yep. I understand that... I've wanted to do things like that in the past and have had to do it manually which isn't a problem.
Dave Kreskowiak wrote:
maximum number of characters, there is no formatting expression
yeah. It's a pain.
Regards,
Brian Dela
http://www.briandela.com IE 6 required. http://www.briandela.com/pictures Now with a pictures section http://www.briandela.com/rss/newsrss.xml RSS Feed
|
|
|
|
|
Does someone know where I can find a good explaination of message handling in C#? i.e. how do the PreProcessFilter and WndProc methods work?
Initially I thought that everything in the windows enviroment is plugged into a central message system and PreProcessFilter selects which messages are relevant to a control or form and this gets passed into WndProc and handled. But now I don't think that's right.
Here's an example of what I'm doing. I have a windows form that overrides CreateParams so that it shows like a tooltip or context menu, it won't take focus or activation so I can't use the LostFocus event to close the window when I click off of it. So I figured I would have to override something in the messaging system to do this but I'm a little lost. any ideas?
thanks,
Jesse
|
|
|
|
|
|
|
hey!
i try to make a keypress event. but i dont know the (char) they have on the keyboard. and i dont find it on google *shameonme*
can anybody help? thx
|
|
|
|
|
In a KeyPress event you cannot detect the arrow keys. Instead, use the KeyDown event. Then, in the Keydown event,
if (e.Keydata = keys.up){
...
}
BTW, the KeyCode for the up key is 38.
Aaron Eldreth
TheCollective4.com
My Articles
While much is too strange to be believed,
Nothing is too strange to have happened.
- T. Hardy
|
|
|
|
|
What do you understand under an assembly? All I know about it is when .Net code is compiled into Microsoft Intermediate Language and stored in a file called an assembly. Is there more to it? Or don’t you really worry about it?
|
|
|
|
|
An assembly consists of a manifest. That's really all that's required. If can have zero or more of modules (contains the IL), embedded resources, dependencies, files (not currently supported by the C# compiler), external references (to native modules for P/Invoke) and assembly-attributes.
If all you're doing is compiling a project in VS.NET, you really don't need to worry about it.
There are more advanced things you can do if you understand the assembly, versioning, etc. You can find more information in the .NET Framework SDK, such as assembly version redirection, satellite assemblies (specific to a culture but contains no modules), etc.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I understand "assembly" as "a module or a group of modules that is treated like a logical unit".
Usually an assembly is one file that contains one namespace.
You don't have to care much about the official definition...
|
|
|
|
|
Corinna John wrote:
Usually an assembly is one file that contains one namespace.
An assembly can - and typically does - contain more than one namespace, and namespaces can span assemblies. Technically, namespaces have nothing to do with it. A module within an assembly doesn't even have to use namespaces (the C# language doesn't support this) at all.
Maybe for a tiny application you could do this, but larger projects should not restrict a namespace to an assembly. First of all, it may not work out anyway, and it will probably create too many assemblies (or you have too many classes in a namespace and should probably organize classes a little better, though that's an architectual choice).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
How do we compare objects in C#, If I want to compare I would write
bool equalob(object that)<br />
{<br />
if(that == classname)<br />
{<br />
return true<br />
}<br />
else<br />
{<br />
return false<br />
}<br />
}
Is this fine.. I got error..
in java we might say
(hat instanceof classname)
wats in C#.
_____________________________________________________
Believe! Every thing has a purpose
|
|
|
|
|