Simon,
Happy to help!
but the bit I'd missed was to close then flush the buffer and using two instances for Tx & Rx
Not entirely sure what you're referring to here. Ideally you keep the serial port open the entire time you are using Mach. I'm pretty sure Windows will automatically close the port when Mach 4 closes, but I opted to close it in the screen unload script anyways.
Without the flush, mine would send intermittently. I'm guessing that when you create the instance, the buffer pointer has some random value - then depending on the string you send it, the buffer overruns?
As far as I know, the serial buffer is only used for data
received on the open port (the buffer will not fill when the port is closed). Since writing data happens immediately, there is no need to buffer it. Without a buffer, you would need to get incredibly lucky to have your program looking for incoming data at the exact moment it arrived. Instead Windows captures and saves this data asynchronously from your code in the buffer.
Whenever the port is open, you should be able to write out the port, and it should happen immediately.
The buffer flush is not explicitly necessary, but I use it to make sure there isn't any junk in the buffer that I'd need to sort through to find the data packet I wanted. Even then, the serial code will handle a buffer full of junk characters as it looks for specific start and end characters and verifies the data with a checksum check.
This is only my second outing with Lua (first was an ESP8266) though I've coded in C++ & C# for a long time - so it's just getting my head round the different syntax & environment.
I was in the same boat as you 6 months to a year ago. I had just a bit of C++ experience and no idea what LUA was. The learning curve is a bit painful, but I really feel comfortable with the language now.
I'm also intending to use Ultra3000 drives on a Denford Cyclone F using Fanuc Red Cap motors. I've had one spinning with pulse & direction though the motor needs a bit of tuning. I'd not yet tried talking to the drives using Serial.
Wow! I'd love to hear how you got that working. It was my understanding that the Fanuc motors used pulsecoders with a proprietary serial interface that most drives couldn't understand.
In my setup, the serial data is just "bonus" stuff and my CNC would still work fine without it. I am using serial to configure the drives to my standard settings when Mach 4 loads, get the spindle load in real time, pull absolute position data into Mach 4 for homing, and hopefully soon - rigid tapping.
If you are interested, I would be happy to share my sister module to the serial one posted above that has all the functions to talk with the Ultra 3000.
If that fails, an Ultra3000 30A ought to be sufficient to drive it. Then talking to the drive to implement the spindle as an Axis would be useful. My intention is to add live tooling - for which this is rather important!
Sounds like a sweet setup! Would love to see it. Here to help with anything I can.