Writing strings and variable to buffer in C

I use a serial port to control a device called a nano controller. For communication I used CreateFile, writeFileand readFile.

this syntax writeFile,

if (!WriteFile(hComm, lpBuf, dwToWrite, &dwWritten, &osWrite)) {      
    if (GetLastError() != ERROR_IO_PENDING) {   
        // WriteFile failed, but isn't delayed. Report error and abort.
        fRes = FALSE;     
    }
}

Here the data should be included internally lpBuf. This is a buffer.

I want to assign "MINC, moveL". Here MINCis the text. however, it moveLis a variable whose type must be double. Values ​​must be passed on moveLwith time. moveLvaries from 0 ~ 10,000.

So, how do I fill the buffer?

+3
source share
2 answers

It sounds like you want sprintf(or one of his cousins):

char buffer[128];

sprintf(buffer, "MINC,%f", moveL);
WriteFile(hComm, buffer, ...);
+1
source

sprintf(lpBuf, "MINC,%lf", moveL); ?

lpBuf, .

+1

All Articles