I will let you know if I ever get thereamazing how easy it looks after you figure it out
slight change used tableCount() for tx loop limit
I will leave spindle off/on as the are since the data will never change
Code: Select all
// Library Vfd-Spindle
// Created Wednesday, January 04, 2017
// Author rc -- Do not edit above this line
// Enter Global vars below this line.
// the librarian no matter where they are, but its easier
// to find and edit them in one spot.
// vfd stuff
global myser = Serial();
//
global Vfd_Open = function()
{
myser.Open(6,9600);
myser.SendData("123");
print( "VFD opened");
SpindleSpeed(5500);
};
//this is a HUANYANG vfd control,
//it will use rs485 to control a spindle
//on/off spindle, and speed
global SpindleOn = function()
{
vfd={0x01,0x03,0x01,0x01,0x31,0x88};
myser.Table = vfd;
myser.SendTable();
print("Vfd Spindle was turned on");
print("Set to Frequency " + GlobalGet("SpindleSpeed"));
};
//this is a HUANYANG vfd control,
//it will use rs485 to control a spindle
//on/off spindle, and speed
global SpindleOff = function()
{
vfd= table(0x01,0x03,0x01,0x08,0xF1,0x8E);
myser.Table = vfd;
myser.SendTable();
print("Spindle speed zeroed");
};
global SpindleSpeed = function( speed )
{
if( Engine.GetSpindleState() != 0)
{
CRC=0xffff;
print("speed set to " + speed);
vfd=table(0x01,0x05,0x03,0,0,0);
freq=speed/55.0;
freq=freq*100;
vfd[3]=ToInt(freq/256);
vfd[4]=ToInt(freq%256);
print("Frequency set to " + freq );
for (inx=0;inx<tableCount(vfd);inx+=1)
{
myser.SendHex(vfd[inx]);
CRC=Crc16(CRC,vfd[inx]);
};
print("the CRC " + CRC);
myser.SendHex(ToInt(CRC/256));
myser.SendHex(ToInt(CRC%256));
};
};
// calculate a ccitt 16 bit crc on byte at a time
// preset crc to 0xffff to initialise a new calc
global Crc16 = function(crc,byte_in)
{
//print("CRC " + byte_in );
crc = crc ^ byte_in ;
for (c=0;c<8;c+=1){
if (crc&1)
{
crc= ((crc>>1) ^0xa001 ) ;
}
else
{
crc= crc>>1;
};
};
return ToInt(crc);
};