Click here to Skip to main content
13,300,002 members (62,964 online)
Rate this:
 
Please Sign up or sign in to 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 15-Aug-12 22:46pm
Comments
   
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

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

—SA
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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):

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);
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.171207.1 | Last Updated 16 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100