Pitufo
February 2nd, 2009, 11:55 AM
Is there such a code in C++ that can read the information coming in from the serial port or the usb port? Can someone please help me...
|
Click to See Complete Forum and Search --> : Serial port or usb port Pitufo February 2nd, 2009, 11:55 AM Is there such a code in C++ that can read the information coming in from the serial port or the usb port? Can someone please help me... ahoodin February 2nd, 2009, 12:54 PM Well the only way I think that question is valid is when it comes to virtual ports. Currently it is possible to map a USB port to a COM port. Then data from that port should be treated as serial data, and any serial port APIs *should* work. Codeplug February 2nd, 2009, 01:33 PM From a serial port: http://msdn.microsoft.com/en-us/library/ms810467.aspx From a USB port: You write a driver for the device. If the device already has a driver, what kind of device is it? gg Pitufo February 3rd, 2009, 02:52 PM it is a microcontroller Pitufo February 3rd, 2009, 03:08 PM for example, if i want to use this code to open a serial port, which libraries do i use? using namespace std; int main() { HANDLE hComm; hComm = CreateFile( gszPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); if (hComm == INVALID_HANDLE_VALUE) // error opening port; abort } ahoodin February 3rd, 2009, 03:14 PM First you better install a virtual serial port driver. I used to use one to synch up my Palm Pilot. Time to google my friend. Pitufo February 3rd, 2009, 03:16 PM thanks ahoodin February 3rd, 2009, 03:53 PM Youre welcome pitufo. :D Pitufo February 9th, 2009, 04:49 PM I have this code but does it work with usb port? #include <windows.h> #include <commctrl.h> #include <iostream> #include <iomanip> #include <fstream> using namespace std; int main() { //1 Opening the serial port HANDLE hUsb; hUsb = CreateFile("USB3", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if(hUsb==INVALID_HANDLE_VALUE){ if(GetLastError()==ERROR_FILE_NOT_FOUND){ cout<<"Serial port does not exist"; } //some other error occurred. Inform user. } //2 Setting Parameters DCB dcbSerial; DCB dcbSerialParams = {0}; dcbSerial.DCBlength=sizeof(dcbSerialParams); if (!GetCommState(hUsb, &dcbSerialParams)) { //error getting state } dcbSerialParams.BaudRate=CBR_9600; dcbSerialParams.ByteSize=8; dcbSerialParams.StopBits=ONESTOPBIT; dcbSerialParams.Parity=NOPARITY; if(!SetCommState(hSerial, &dcbSerialParams)){ //error setting serial port state } //3 Setting timeouts COMMTIMEOUTS timeouts={0}; timeouts.ReadIntervalTimeout=50; timeouts.ReadTotalTimeoutConstant=50; timeouts.ReadTotalTimeoutMultiplier=10; timeouts.WriteTotalTimeoutConstant=50; timeouts.WriteTotalTimeoutMultiplier=10; if(!SetCommTimeouts(hSerial, &timeouts)){ //error occureed. Inform user } //4 Reading/Writing data char szBuff[100] = {0}; DWORD dwBytesRead = 0; if(!ReadFile(hSerial, szBuff, 100, &dwBytesRead, NULL)){ //error occurred. Report to user. } //5 Closing down CloseHandle(hSerial); return 0; } mariocampeau March 5th, 2009, 05:34 PM I'm curious. Did your code worked? Because I am also looking for a way to read USB data coming from a MCU into a ICH-7 Intel board. fantasy1215 March 12th, 2009, 07:42 AM Well the only way I think that question is valid is when it comes to virtual ports. Currently it is possible to map a USB port to a COM port. Then data from that port should be treated as serial data, and any serial port APIs *should* work. Can you tell more in detail? codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |