Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 327
Kumaa Avatar asked May 23 '26 22:05

Kumaa


1 Answers

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

char buffer[128];

sprintf(buffer, "MINC,%f", moveL);
WriteFile(hComm, buffer, ...);
like image 199
Jerry Coffin Avatar answered May 25 '26 20:05

Jerry Coffin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!