Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using USB port to read data in C programming on windows, i am currently using CreateFile() function but it only works on serial port and i can't found proper help for usb port can any one

What I have tried:

HANDLE usb;
usb=CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, NULL);

If(usb= INVALID_HANDLE_VALUE)
printf("can't open port");
else
printf("port open successfully");
CloseHandle(usb);
Posted
Updated 28-Dec-20 7:11am
Comments
Richard MacCutchan 28-Dec-20 12:03pm    
What error do you receive?
Aqsa Munir 28-Dec-20 13:21pm    
No error but port can not open because this will work for serial only.
Richard MacCutchan 28-Dec-20 15:04pm    
If you do not get an error, how do you know it will not open?
Dave Kreskowiak 28-Dec-20 13:47pm    
That's because USB is NOT A PORT. It's an expansion BUS, not unlike the expansion slots on your motherboard.

Start communicating with the U-Blox device using a serial communication program, like, for instance Hyperterminal (or PuTTY, you may probably found some example provided by from the manufacturer). Once you are sure the communication is OK then you might start writing your application using the (found) proper port settings. If you choose C# as programming language then your task is easier. On the other hand, if you are going to write it using C, the have a look at MSDN code samples: Using Communications Resources - Win32 apps | Microsoft Docs[^].
 
Share this answer
 
It's going to depend on what kind of USB device it is: a USB serial communications device can be accessed as a COM port via CreateFile because the USB driver that is installed "knows about them" and is set up to work that way. A USB card reader isn;t, and you can;t access via a file at all.

But you can't "talk directly" to a USB device at all, you need to install the right driver and then talk to that, and exactly how you do that will depend on the driver, and it's capabilities.

I'd start by going to the manufacturers site and seeing if they provide any tech support - if so, talk to them and see what they suggest. But us? We have no idea what device you are plugging in, much less what it is supposed to do or how it "presents itself" to Windows.
 
Share this answer
 
Comments
Aqsa Munir 28-Dec-20 10:59am    
I have a ublox and i am reading gps data via usb port,i have driver installed, my PC shows that my device is at port "COM1", now i want to open it,read and print data on screen using C code..
OriginalGriff 28-Dec-20 11:21am    
As I said: talk to the manufacturers - they will probably have sampel code which will do at least some of that - even if it's in C++ or C#, it will show what you need to look at doing.
The Other John Ingram 28-Dec-20 11:09am    
i would just use the open command to open the device, and then use the read command to get the data. I don't see the createfile command as a good fit.
You will have to find out the format of the data coming from the device and parse it into something you can use. Start small by just printing the data to the screen and see what you get and go from there.

Good luck
Aqsa Munir 28-Dec-20 11:18am    
Can u please show me which command u used for device open instead of CreateFile.
Mr. Griff pretty much spelled it out. From what I've gathered, the device's driver presents itself as a virtual comm port. That means you should treat it as one. Look for snippets and samples that deal with the serial port. This site has several of them. Here's one : Serial Communication in Windows[^]. That is probably a bit lower level then you will need since I doubt you will have to set baud rate or change device control parameters. The key things are opening the port and reading from it. CreateFile, as you have, is used to open it and ReadFile is used to read from it. The article shows how to use those functions so I would start there. There are many other samples on this site that demonstrate this also.
 
Share this answer
 
Comments
Aqsa Munir 28-Dec-20 13:13pm    
Rick York i have tried all these function CreateFile ReadFile WriteFile but these are only for serial Communication and I have to communicate to device through USB Port.
Rick York 28-Dec-20 15:31pm    
It makes little difference that it is through a USB port. In fact, it is best to forget completely that is connected through a USB port. The driver presents a virtual comm port - that's why it is called "COM1". I have used multi-port adapters that give you eight comm ports through a single USB connection. They were all virtual comm ports and we treated them exactly has built-in serial ports. I would follow Mr. Pallini's suggestion regarding hyperterminal or an equivalent program. That would tell you if it is working. Its documentation should tell you how to ask it for data so see if you can do that through hyperterminal.

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