81
Mach4 General Discussion / Re: Making a connection through rs232 in Mach4
« on: November 26, 2019, 12:42:26 PM »
Example 3: Flushing the buffer
Flushing the buffer is super simple and all it does is delete the contents of the buffer without reading them.
Lets say you open your serial port and sit for a few minutes. The whole time your Arduino has been writing "Hello World" once per second. Obviously your buffer will be very full. Lets say we want to read one line of "Hello World" and we want it to be the most recent one.
In this code, we make sure the buffer is completely empty before we read. We allow 1.2 seconds for the data to arrive since this will capture one and only one packet from the Arduino.
Flushing the buffer is super simple and all it does is delete the contents of the buffer without reading them.
Lets say you open your serial port and sit for a few minutes. The whole time your Arduino has been writing "Hello World" once per second. Obviously your buffer will be very full. Lets say we want to read one line of "Hello World" and we want it to be the most recent one.
Code: [Select]
inst = mc.mcCntlGetInstance()
string = ""
p:flush()
e, string(12, 1200) --Read all 12 characters of "Hello World\r" waiting just over 1 second for them to arrive
mc.mcCntlSetLastError(inst, string)
In this code, we make sure the buffer is completely empty before we read. We allow 1.2 seconds for the data to arrive since this will capture one and only one packet from the Arduino.