ARTICLE AD BOX
I have a dialog-based MFC application build in Visual Studio. With my App I want to read a serial event from a potentiometer, which is attached to an Arduino board.
The Arduino is working just fine, with a baud rate of 9600 I get a value which I can read at the Serial Monitor 2 within Visual Studio.
But now, I'd like to read this value with my application and draw it into a XY chart (with the value being drawn on the y-axis and the time on the x-axis).
I already put the following code into the OnInitDialog()-function of my App:
m_hCom = CreateFileW(m_sComPort,GENERIC_READ | GENERIC_WRITE, 0,NULL, OPEN_EXISTING, 0, NULL); SetupComm(m_hCom, 128, 128); GetCommState(m_hCom, &m_dcb); m_dcb.BaudRate = 9600; m_dcb.ByteSize = 8; m_dcb.Parity = NOPARITY; m_dcb.StopBits = ONESTOPBIT; m_dcb.fAbortOnError = TRUE; SetCommState(m_hCom, &m_dcb); SetCommMask(m_hCom, EV_RXCHAR);I put some variables into the header file:
HANDLE m_hCom; CString m_sComPort = L"COM4"; DCB m_dcb; DWORD iBytesWritten; DWORD iBytesRead;But now I don't know where to set the ReadFile()-function. Do I have to set a timer event for this?
I set up a timer event with the following code:
char sBuffer[128]; ReadFile(m_hCom, &sBuffer, 8, &iBytesRead, NULL);But the sBuffer variable is just full of /n and /r characters and sometimes a number shows up.
Is the SetTimer-Event correct for this type of operation?
How does the gattering of values work inside MFC, is the sBuffer variable too big?
