Sharing data and resources between threads
Posted: Fri Mar 22, 2019 4:08 pm
Hi Tom,
I'm in the middle of rebuilding an old VMC and am retrofitting it with a KFLOP and a custom board that is based on your Kanalog and Konnect boards. The tool changer on this VMC communicates with the KFLOP board through an RS422 interface on the serial port. I have a simple serial command protocol running and can easily send commands from the main thread (1) and have a serial monitor function that is constantly looking at the serial receive buffer and processes messages. I had originally tried to run the port at 115200 baud but it was missing received data some times. Slowed it down to 57600 baud and it seems to be working fine. I was wondering what the size of the transmit and receive buffers are for the serial port. It appears that the transmit buffer is larger than the receive buffer because when I tried to time the code that sends the messages by setting and clearing a test bit. The message is only 5 bytes and the KFLOP only take 5us to send it so there must be some buffering going on.
What I was thinking of doing was having the the actual tool change be managed in another thread, initiated from an M06 code for example or another user button. But now I'm worried I might have a resource allocation issue.
I'm pretty sure that it is not realistic to try and call functions in tread 1 from another thread. I thought I saw something about this once, but I can't remember where.
The persist variables are the obvious choice but it looks like it might get a bit complicated. It would be nice to be able to send a serial message from any thread, and I'm assuming that once the serial port is initialized in thread 1 that any other thread that writes to it will just stick its message into the serial transmit buffer and not collide in the middle of another threads transmit message, especially if it occurs right after a WaitNextTimeSlice(), but I don't know how much room there is in it and I wouldn't want to overrun the buffer.
I would like to use the serial port to communicate with more than just the tool changer. I want to query the state of the buttons on my MPG via the same serial port, not the encoder position, just the axis and rate buttons. And I'd like to have the flexibility to add some other yet undreamed of peripheral to this serial port too.
As far as serial receive goes, I think there is no way around using the persist variables since I can't predict when a receive character is coming in.
And finally if more than one thread is running, will this slow down my ability to process the serial port? A byte at 57600 baud takes about 174us to send and the single thread time I believe is 180us - so right on the threshold. There was something in the ModBus example code about this I think.
Thank for making such a cool product!
Dan Matthews
I'm in the middle of rebuilding an old VMC and am retrofitting it with a KFLOP and a custom board that is based on your Kanalog and Konnect boards. The tool changer on this VMC communicates with the KFLOP board through an RS422 interface on the serial port. I have a simple serial command protocol running and can easily send commands from the main thread (1) and have a serial monitor function that is constantly looking at the serial receive buffer and processes messages. I had originally tried to run the port at 115200 baud but it was missing received data some times. Slowed it down to 57600 baud and it seems to be working fine. I was wondering what the size of the transmit and receive buffers are for the serial port. It appears that the transmit buffer is larger than the receive buffer because when I tried to time the code that sends the messages by setting and clearing a test bit. The message is only 5 bytes and the KFLOP only take 5us to send it so there must be some buffering going on.
What I was thinking of doing was having the the actual tool change be managed in another thread, initiated from an M06 code for example or another user button. But now I'm worried I might have a resource allocation issue.
I'm pretty sure that it is not realistic to try and call functions in tread 1 from another thread. I thought I saw something about this once, but I can't remember where.
The persist variables are the obvious choice but it looks like it might get a bit complicated. It would be nice to be able to send a serial message from any thread, and I'm assuming that once the serial port is initialized in thread 1 that any other thread that writes to it will just stick its message into the serial transmit buffer and not collide in the middle of another threads transmit message, especially if it occurs right after a WaitNextTimeSlice(), but I don't know how much room there is in it and I wouldn't want to overrun the buffer.
I would like to use the serial port to communicate with more than just the tool changer. I want to query the state of the buttons on my MPG via the same serial port, not the encoder position, just the axis and rate buttons. And I'd like to have the flexibility to add some other yet undreamed of peripheral to this serial port too.
As far as serial receive goes, I think there is no way around using the persist variables since I can't predict when a receive character is coming in.
And finally if more than one thread is running, will this slow down my ability to process the serial port? A byte at 57600 baud takes about 174us to send and the single thread time I believe is 180us - so right on the threshold. There was something in the ModBus example code about this I think.
Thank for making such a cool product!
Dan Matthews