bin.alloc()
bin.alloc(bytes) → buffer
Allocates a empty buffer (string) that can be used to store and read binary values.
Creates an empty string bytes long. String objects in mJS are byte strings,
so they can hold arbitrary binary data — null characters do not terminate
them. After any call to bin.alloc() the binary read/write cursor is
automatically positioned at the start of the new buffer.
Arguments
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| bytes | number | yes | — | Buffer size in bytes — 4 bytes per 32-bit register/float. |
Returns
string
A buffer with seek/read/write methods, cursor positioned at byte 0.
Example
let mbData = bin.alloc(20*4); // room for 20 float registers
modbus.setData(mbData); // Assign the data buffer for to modbus read and write requests.
while(run()){
mbData.write("F32ABCD",75); // Write a float32 (4 bytes) to our modbus data table
waitMS(1000);
}