ReadFile()?

Hi!

I'm programming an aplication to connect with a machine by Serial Port. I would like to show the answer that this machine give me (I read it with ReadFile() ).

For example, If I read:

HANDLE Comm;

CString COM = "COM1";

BOOL Read;

DWORD iBytesRead;
char sBuffer[128];
char msg[255];

Read = ReadFile(Comm, &sBuffer, 1, &iBytesRead, NULL);

sprintf(msg , "Response:%s", sBuffer);
m_ctlRecvd.AddString(msg);
UpdateData(FALSE);

Thanks!!!!




Answer this question

ReadFile()?

  • jason_czb

    Call it until you've received 2 bytes total from ReadFile().


  • SpringTime

    You forgot to ask a question in your post. Some notes: obtain the handle Comm by calling CreateFile(). You'll need to call ReadFile() more than once until you "have enough" data received. The very first time you'll call it, you may well get no data at all.



  • Mickey Gousset

    thanks for all!


  • Christoph_S

    Thanks!

    And the last question: how can I show in my monitor the message I read with ReadFile()



  • Darmadi Komo

    In a console mode program, just call printf():
    sBuffer[iBytesRead] = 0;
    printf("Response:%s\n", sBuffer);

    In a Windows program, put it in a editbox with SendMessage(WM_SETTEXT)



  • mil10

    Sorry! I forgot to explain all. I don't know how can I show in my monitor the message that the machine answer me.

    I know that I have to make createFile and configure cbd. But I didn't want to bored you with a big code ;)

    So, if the machine response "OK", how many times Should I call ReadFile How Can I know it

    Thanks!



  • ReadFile()?