Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i am searching for a sample program in which there would be code to open the com port send data to com port then show the send data on the screen of my computer and that can be able to run in visual studio 2010 and not using MFC,i want the com port communication on my PC,i mean i don't have any other PC to send data.
Posted
Comments
Sergey Alexandrovich Kryukov 16-Aug-12 4:14am    
What, broken Google? Or Bing? Both? -- oh, no! :-)
--SA
Tarun Batra 16-Aug-12 4:17am    
i didn't find any code which ran successfully on VS 2010 in which we are sending and receiving data from same machine
Hawkfuture 16-Aug-12 4:44am    
i remember that com ports can be open and read/writed by win32 apis. Such as CreateFile / ReadFile / WriteFile. pls see MSDN for detail.

Please see, for example: www.robbayer.com/files/serial-win.pdf[^].

If you need better help, please use some search engine.

—SA
 
Share this answer
 
To receive data on the same PC you need a loopback plug on your serial port (without using hardware flow control this plug just connects the TXD line to the RXD line).

Use the Windows API function CreateFile() to open the port and WriteFile() / ReadFile() to write and read (untested example):

C++
HANDLE hCom = ::CreateFile(_T("COM1"),
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL,
    OPEN_EXISTING,
    0,
    NULL);
if (hCom != INVALID_HANDLE_VALUE)
{
    DWORD dwWritten;
    ::WriteFile(hCom, "Test", strlen("Test"), &dwWritten, NULL);
    DWORD dwRead;
    char pBuf[32];
    ::ReadFile(hCom, pBuf, sizeof(pBuf), &dwRead, NULL);
    pBuf[min(dwRead, sizeof(pBuf)-1)] = '\0';
    ::MessageBox(NULL, pBuf, _T("Received text"), MB_OK);
    ::CloseFile(hCom);
}
 
Share this answer
 

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