I'm in lua ffi trying to read all the bytes from the serial port with read from C However, the pointer to the buffer moves as it reads ( buf = buf + n ). As a result, I only get the last read in the buffer.

As I understand it, you need to create another pointer to the buffer. But how to do that?

    1 answer 1

    As I understand it, you create a buffer like this (note the starfish):

     local buf = ffi.new('char*[?]', size) 

    In this case, the pointer to buf should be created like this:

     local fullbuf = ffi.cast('char*', buf) 

    Note: simple assignment

     local fullbuf = buf 

    It does not roll, cdata is a common userdata , therefore, it has an identity, and fullbuf will also change with buf .

    • one
      Is this a translation from the English version of SO? Not bad to leave a link to the original. - zed
    • @zed: uh ... I didn't translate, really. - val
    • one
      Those. are you talking to yourself? "I try to lua ffi ..." and then "As I understand it, you create a buffer like this" I have nothing against it, it just seemed like it was taken from somewhere. - zed
    • @zen: yes, yes, I communicate with an intelligent man :) - val