|
How can I send data bits through USB port? I am familiar with the parallel and serial port and have done some hardware controlling project through parallel port. But how do I send data through USB port using Visual C++?
|
|
|
|
|
Accessing a USB port is not the same as accessing the serial port: you can't simply "open" it and send data on it. This is because multiple devices could be connected to the same port (see for instance an USB hub). So, what you have to do instead is retrieve the handle to the device driver associated with the device you want to communicate with (of course, the device should be installed on your computer before being able to do so). To retrieve the handle to the driver, it is similar as opening the serial port: you call CreateFile with the driver symbolic name for the file name argument. Once this is done, you can start sending and receiving data using the WriteFile and ReadFile functions (like for a serial port). Not that the driver will implement a specific behavior for these functions, so it really depends on your driver. Finally, you can also send driver specific control code by using the DeviceIOControl function.
All these things are very specific to the device you want to access (so, to the driver you are accessing). Which kind of USB device are you trying to access ? There should be some documentation with the driver that explains you how to retrieve the handle to the driver and list the device control codes.
If you are trying to access a USB-to-serial converter, things are different because in that case, the driver will emulate a virtual COM port and you can simply use this virtual COM port as a standard COM port.
|
|
|
|
|
Adding to the reply...
The communication finally boils down to the type of endpoint.
A USB device has the following hierarchy.
Device -> Configuration -> Interface -> Endpoint.
Endpoints can be of type Interrupt, Bulk or Isochronous.
If you want to use a Virtual COM port, it will be different for different endpoints.
Interrupt is the easiest to communicate with as there are drivers built-in.
By the way to identify the USB device there is something called a Vendor Id (VID), Product Id (PID) and a serial number.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Thanks for you reply.. Can u please send me a sample program for this... I am trying to send bits through USB and control the circuit directly... Actually I am doing it for Robotics, I have used Micro Controller for this but now i want to use computers to access them directly.
Thanks
|
|
|
|
|
dev_programer wrote: Can u please send me a sample program for this...
Humm... Did you read my reply ? This is simply impossible because I don't even which kind of device you are trying to access. Didn't you receive any documentation with the USB device you are trying to achieve ?
As I said in my previous reply, the driver symbolic name depends on the driver, the control codes depend on the driver also,... So, you should have receive some kind of documentation with the driver (specially if it is some kind of USB development kit).
|
|
|
|
|
Actually its not any specific device that i am trying to access. I develop my own robots and circuits and control them using a series of 0's and 1's. Lets say to active a motor first i need to send 1 through parallel port to the circuit which opens the gate and the motor driver circuit is turned on and thus motor begins to operate. How do i do this using USB?
|
|
|
|
|
You need to have at least something on your robot that has a USB port (some kind of micro-controller with a USB port). You need to also to have a driver installed on the computer that is able to communicate with this device.
So, I really don't understand what you are trying to achieve: do oyu have some kind of micro-controller that has a USB port on your robot ? If no, I don't see how you are going to communicate with your robot over USB if your robot doesn't even have a USB port
So, once you have your USB device (a micro-controller with a USB port for instance), this device should have been supplied with a driver (and documentation). The driver has to be installed on your computer and you have to access the driver if you want to send information to your robot. That's how it works, you can't open the USB port directly, you need to have a driver installed on your computer which takes care of the communication with your USB device.
|
|
|
|
|
They make USB to parallel adapters. I suggest this as you say you have previously done your robotic control via a parallel port. There's a chance that your existing code that works through a real parallel port might work with one of these.
|
|
|
|
|
I use std::wstring for unicode.
To write a file with wstring, I need the memory size of wstring.
But I couldn't find the method about memory size.
wstring::size() and wstring::length() show only the length of characters.
for example,
std::wstring strText = L"ABC";
In the example, length of strText is '3'
But memory size of strText is '6' (in windows)
Is there any method to get memory size of std::wstring?
Thanks your help
David.
|
|
|
|
|
You can always multiply the string length by sizeof(wchar_t) to get the exact memory size.
|
|
|
|
|
Look at the capacity method.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
This method returns the full capacity (in element, not in bytes) of the string. It means that it can be larger than the actual size of your string (and it is still not expressed in bytes).
|
|
|
|
|
Isn't that what the OP is about?
To convert it to bytes you can do this.
size_t memsize = sizeof(wstring::value_type) * strText.capacity();
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
No, as far as I understood he wants to retrieve the lenght (in bytes and not in 'characters') of the string. For a string, the lenght is the same as the size in memory because one char equals one byte, which is not the case for a wstring.
|
|
|
|
|
If that's the case
size_t memsize = sizeof(wstring::value_type) * strText.size();
After reading the OP again, I think you're right.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
That was more or less my answer, but I have to agree that yours is more elegant
|
|
|
|
|
The basic_string::size() returns array size of <element> but not always the same to the character length.
Some new characters have 4 bytes (means wchar_t[2]) in each one character at the new unicode specification named JIS2004 in Japan.
They are so called surrogate-pair and someone said some characters have 6 bytes in some other countries. (I have not checked the specification yet. )
|
|
|
|
|
Do you mean you want to write the wstring to a file? With the null-terminator? Or what?
Anyway - the amount of memory taken up by the string data (i.e. the characters pointed at by the c_str() or data() methods) is size() * sizeof (std::wstring::value_type) (not including the null terminator).
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Why am I getting this error?
Error 1 error C2228: left of '.checked' must have class/struct/union
Here's my code.
UpdateData(TRUE);
m_Summary += m_Name;
m_Summary += char(13);
m_Summary += char(10);
m_Summary += m_Address;
m_Summary += char(13);
m_Summary += char(10);
m_Summary += m_Phone;
if (m_Small.checked==true)
{
m_Summary += "Small Size 5.00";
}
UpdateData(FALSE);
|
|
|
|
|
Ryuk1990 wrote: if (m_Small.checked==true)
{
m_Summary += "Small Size 5.00";
}
How is m_Small declared?
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
When I do view declaration, it shows me this code.
public:
int m_Small;
|
|
|
|
|
Ryuk1990 wrote: Error 1 error C2228: left of '.checked' must have class/struct/union
That is exactly what the error says.
Fundamental data types do not have any members that you can call that way.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
So how should the code be rewritten?
|
|
|
|
|
That totally depends on why m_Small was declared.
My guess would be
if (m_Small == true)
or
if (m_Small == TRUE)
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
use a #define and then do the checking.
|
|
|
|