| | |
rs232 to usb comm question
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 98
Reputation:
Solved Threads: 2
Hi,
I am writing a simple program to read and write a serial port. I only have access to one serial port on my pc so I am using a HugePine RS-232 to USB convertor. The driver is installed and windows recognizes it is attached. My question is what comm port is the USB and can I treat it as if it was a second serial port on my PC. Thanks!!
Dave
I am writing a simple program to read and write a serial port. I only have access to one serial port on my pc so I am using a HugePine RS-232 to USB convertor. The driver is installed and windows recognizes it is attached. My question is what comm port is the USB and can I treat it as if it was a second serial port on my PC. Thanks!!
Dave
•
•
Join Date: Feb 2008
Posts: 98
Reputation:
Solved Threads: 2
I think I solved my first issue but the following code does not work. It seems to write (though I have no proof), but just sits at the readfile (doesnt fail, just sits). Any suggestions on why this may be?
// Testrs232.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
DCB dcb;
HANDLE myCom;
BOOL InitializeDevice();
BOOL SetupPort(int BaudRate);
char *Recieve();
void Send(char txchar);
BOOL InitializeDevice()
{
BOOL isInitialized = TRUE;
myCom = CreateFile(TEXT("COM1"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if(myCom == INVALID_HANDLE_VALUE)
{
isInitialized = FALSE;
cout << "Could not initialize the port." << endl;
return isInitialized;
}
isInitialized = SetupPort(1200);
return isInitialized;
}
BOOL SetupPort(int BaudRate)
{
BOOL isSetup = TRUE;
if(!SetupComm(myCom, 4096, 4096))
{
isSetup = FALSE;
cout << "Setup comm error." << endl;
}
if(!GetCommState(myCom, &dcb))
{
isSetup = FALSE;
cout << "Get comm state error." << endl;
}
dcb.BaudRate = BaudRate;
dcb.ByteSize = 8;
if(!SetCommState(myCom, &dcb))
{
isSetup = FALSE;
cout << "Set comm state error." << endl;
}
return isSetup;
}
char *Recieve()
{
//DWORD myReadEvent;
DWORD bytesRead;
char *myReadByte;
//char *myIncomingMessage = "";
if(!SetupPort(1200))
cout << "Error setting up the port." << endl;
else
{
//if(!SetCommMask(myCom, EV_RXCHAR))
//cout << "Could not set read mask." << endl;
//if(WaitCommEvent(myCom, &myReadEvent, NULL))
//{
//do
//{
if(ReadFile(myCom, &myReadByte, 1, &bytesRead, NULL))
//strncat_s(myIncomingMessage, sizeof(myIncomingMessage), myReadByte, 1);
cout << "Reading..." << endl;
else
cout << "Read message error." << endl;
//}while(bytesRead);
//}
}
//return myIncomingMessage;
return myReadByte;
}
void Send(char myWriteByte)
{
DWORD bytesWritten;
if(!SetupPort(1200))
cout << "Error setting up the port." << endl;
else
{
if(WriteFile(myCom, &myWriteByte, 1, &bytesWritten, NULL))
cout << "Writing..." << endl;
else
cout << "Write message error." << endl;
}
return;
}
int _tmain(int argc, _TCHAR* argv[])
{
//char *letter;
if(!InitializeDevice())
cout << "Could not Setup port for comm" << endl;
else
{
Send('c');
//letter = Recieve();
}
Sleep(8000);
CloseHandle(myCom);
return 0;
}
// Testrs232.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
DCB dcb;
HANDLE myCom;
BOOL InitializeDevice();
BOOL SetupPort(int BaudRate);
char *Recieve();
void Send(char txchar);
BOOL InitializeDevice()
{
BOOL isInitialized = TRUE;
myCom = CreateFile(TEXT("COM1"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if(myCom == INVALID_HANDLE_VALUE)
{
isInitialized = FALSE;
cout << "Could not initialize the port." << endl;
return isInitialized;
}
isInitialized = SetupPort(1200);
return isInitialized;
}
BOOL SetupPort(int BaudRate)
{
BOOL isSetup = TRUE;
if(!SetupComm(myCom, 4096, 4096))
{
isSetup = FALSE;
cout << "Setup comm error." << endl;
}
if(!GetCommState(myCom, &dcb))
{
isSetup = FALSE;
cout << "Get comm state error." << endl;
}
dcb.BaudRate = BaudRate;
dcb.ByteSize = 8;
if(!SetCommState(myCom, &dcb))
{
isSetup = FALSE;
cout << "Set comm state error." << endl;
}
return isSetup;
}
char *Recieve()
{
//DWORD myReadEvent;
DWORD bytesRead;
char *myReadByte;
//char *myIncomingMessage = "";
if(!SetupPort(1200))
cout << "Error setting up the port." << endl;
else
{
//if(!SetCommMask(myCom, EV_RXCHAR))
//cout << "Could not set read mask." << endl;
//if(WaitCommEvent(myCom, &myReadEvent, NULL))
//{
//do
//{
if(ReadFile(myCom, &myReadByte, 1, &bytesRead, NULL))
//strncat_s(myIncomingMessage, sizeof(myIncomingMessage), myReadByte, 1);
cout << "Reading..." << endl;
else
cout << "Read message error." << endl;
//}while(bytesRead);
//}
}
//return myIncomingMessage;
return myReadByte;
}
void Send(char myWriteByte)
{
DWORD bytesWritten;
if(!SetupPort(1200))
cout << "Error setting up the port." << endl;
else
{
if(WriteFile(myCom, &myWriteByte, 1, &bytesWritten, NULL))
cout << "Writing..." << endl;
else
cout << "Write message error." << endl;
}
return;
}
int _tmain(int argc, _TCHAR* argv[])
{
//char *letter;
if(!InitializeDevice())
cout << "Could not Setup port for comm" << endl;
else
{
Send('c');
//letter = Recieve();
}
Sleep(8000);
CloseHandle(myCom);
return 0;
}
![]() |
Other Threads in the C++ Forum
- Previous Thread: Why doesn't this program catches my exception
- Next Thread: Math tutor
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





