Click here to Skip to main content
Rate this: bad
good
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 21:46pm
Comments
Sergey Alexandrovich Kryukov at 16-Aug-12 4:14am
   
What, broken Google? Or Bing? Both? -- oh, no! :-)
--SA
Tarun Batra at 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 at 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
0 Sergey Alexandrovich Kryukov 503
1 OriginalGriff 384
2 George Jonsson 258
3 Animesh Datta 130
4 Shemeemsha RA 128
0 OriginalGriff 6,099
1 Sergey Alexandrovich Kryukov 5,411
2 CPallini 4,770
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 16 Aug 2012
Copyright © CodeProject, 1999-2014
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