You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
*/
+#include "libs/Kernel.h"
+#include "StreamOutputPool.h"
#include "BufferedSoftSerial.h"
#include "ModbusSpindleControl.h"
#include "HuanyangSpindleControl.h"
// prepare data for the spindle off command
char turn_on_msg[6] = { 0x01, 0x03, 0x01, 0x01, 0x00, 0x00 };
// calculate CRC16 checksum
- unsigned int crc = modbus->crc16(turn_on_msg, 4);
+ unsigned int crc = modbus->crc16(turn_on_msg, sizeof(turn_on_msg)-2);
turn_on_msg[4] = crc & 0xFF;
turn_on_msg[5] = (crc >> 8);
modbus->dir_output->set();
modbus->delay(1);
// send the actual message
- modbus->serial->write(turn_on_msg, 6);
+ modbus->serial->write(turn_on_msg, sizeof(turn_on_msg));
// wait a calculated time for the data to be sent
- modbus->delay((int) ceil(6 * modbus->delay_time));
+ modbus->delay((int) ceil(sizeof(turn_on_msg) * modbus->delay_time));
// disable transmitter
modbus->dir_output->clear();
// wait 50ms, required by the Modbus standard
// prepare data for the spindle off command
char turn_off_msg[6] = { 0x01, 0x03, 0x01, 0x08, 0x00, 0x00 };
// calculate CRC16 checksum
- unsigned int crc = modbus->crc16(turn_off_msg, 4);
+ unsigned int crc = modbus->crc16(turn_off_msg, sizeof(turn_off_msg)-2);
turn_off_msg[4] = crc & 0xFF;
turn_off_msg[5] = (crc >> 8);
modbus->dir_output->set();
modbus->delay(1);
// send the actual message
- modbus->serial->write(turn_off_msg, 6);
+ modbus->serial->write(turn_off_msg, sizeof(turn_off_msg));
// wait a calculated time for the data to be sent
- modbus->delay((int) ceil(6 * modbus->delay_time));
+ modbus->delay((int) ceil(sizeof(turn_off_msg) * modbus->delay_time));
// disable transmitter
modbus->dir_output->clear();
// wait 50ms, required by the Modbus standard
void HuanyangSpindleControl::report_speed()
{
+ // clear RX buffer before start
+ while(modbus->serial->readable()){
+ modbus->serial->getc();
+ }
- // TODO: implement this
+ // prepare data for the get speed command
+ char get_speed_msg[8] = { 0x01, 0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00 };
+ // calculate CRC16 checksum
+ unsigned int crc = modbus->crc16(get_speed_msg, sizeof(get_speed_msg)-2);
+ get_speed_msg[6] = crc & 0xFF;
+ get_speed_msg[7] = (crc >> 8);
+
+ // enable transmitter
+ modbus->dir_output->set();
+ modbus->delay(1);
+ // send the actual message
+ modbus->serial->write(get_speed_msg, sizeof(get_speed_msg));
+ // wait a calculated time for the data to be sent
+ modbus->delay((int) ceil(sizeof(get_speed_msg) * modbus->delay_time));
+ // disable transmitter
+ modbus->dir_output->clear();
+ // wait 50ms, required by the Modbus standard
+ modbus->delay(50);
+
+ // wait for the complete message to be received
+ modbus->delay((int) ceil(8 * modbus->delay_time));
+ // prepare an array for the answer
+ char speed[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
+
+ // read the answer into the buffer
+ for(int i=0; i<8; i++) {
+ speed[i] = modbus->serial->getc();
+ }
+ // get the Hz value from trhe answer and convert it into an RPM value
+ unsigned int hz = (speed[4] << 8) | speed[5];
+ unsigned int rpm = hz / 100 * 60;
+ // report the current RPM value
+ THEKERNEL->streams->printf("Current RPM: %d\n", rpm);
}