| 830 | } |
| 831 | |
| 832 | int lua_serial_readstring(lua_State *L) { |
| 833 | binding_argcheck(L, 2); |
| 834 | |
| 835 | AP_Scripting_SerialAccess * port = check_AP_Scripting_SerialAccess(L, 1); |
| 836 | |
| 837 | // create a buffer sized to hold the number of bytes the user wants to read |
| 838 | luaL_Buffer b; |
| 839 | const uint16_t req_bytes = get_uint16_t(L, 2); |
| 840 | uint8_t *data = (uint8_t *)luaL_buffinitsize(L, &b, req_bytes); |
| 841 | |
| 842 | // read up to that number of bytes |
| 843 | const ssize_t read_bytes = port->read(data, req_bytes); |
| 844 | if (read_bytes < 0) { |
| 845 | return 0; // error, return nil |
| 846 | } |
| 847 | |
| 848 | // push the buffer as a string, truncated to the number of bytes actually read |
| 849 | luaL_pushresultsize(&b, read_bytes); |
| 850 | |
| 851 | return 1; |
| 852 | } |
| 853 | |
| 854 | /* |
| 855 | directory listing, return table of files in a directory |
nothing calls this directly
no test coverage detected