Kumaa
Kumaa

Reputation: 31

Writing strings and a variable to a buffer in C

I am using serial port to control a device called nano controller. I used CreateFile, writeFile and readFile for communication.

this is the syntax for 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 data should be included inside lpBuf. It is a buffer.

I want to assign "MINC,moveL" . Here MINC are text. however, moveL is variable which the type should be double. values should be passed to moveL with the time. moveL is varying from 0~10 000.

So how do I fill the buffer?

Upvotes: 3

Views: 4233

Answers (2)

Adam Liss
Adam Liss

Reputation: 48290

Can you use sprintf(lpBuf, "MINC,%lf", moveL); to create the string?

Be sure to allocate sufficient memory for lpBuf before you fill it with data.

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490108

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

char buffer[128];

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

Upvotes: 1

Related Questions