Huanyang VFD Macro/ Plugin

Page 1 of 29 1234 11 ... LastLast
Results 1 to 12 of 345

Thread: Huanyang VFD Macro/ Plugin

  1. #1
    Registered
    Join Date
    Mar 2010
    Location
    America
    Posts
    642
    Downloads
    0
    Uploads
    0

    Default Huanyang VFD Macro/ Plugin

    Hello Everyone,

    If you were using a Huanyang plugin for Mach3 to control VFD/spinde or using Spindle Talker than this macro's for you. I didn't add actual RPM yet but I will.

    Only set up in UCCNC is in configuration, set spindle velocity min to 8000(rpm) and max to 24000(rpm). Any setting below it wont set frequency. Go to Macroloop set up and add M1092 with autorun.

    If you used HY plugin you should already have these settings for HY VFD but here's a reminder.

    PD163 is set to 1
    PD164 is set to 1
    PD165 is set to 3

    Huanyang VFD Macro/ Plugin-m1092-txt

    Similar Threads:
    Attached Files Attached Files
    Last edited by ger21; 04-16-2017 at 12:43 PM.


  2. #2
    Registered
    Join Date
    Mar 2010
    Location
    America
    Posts
    642
    Downloads
    0
    Uploads
    0

    Default Re: Hunayang VFD Macro

    Sorry Mods I meant to post this in macro sharing, didn't mean to make a second thread.



  3. #3
    Registered
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    1431
    Downloads
    2
    Uploads
    0

    Default Re: Hunayang VFD Macro

    It's a good start if it works, but... hard coding checksum and RPM? Why? Also, why all the Open/Close ports? Oh... I see teh reason...

    Proposal of improvements:

    Make the port number a variable at the top. Having to change a number of "COM 1" in the code is not very nice from a programmer perspective.

    Open port should be done once at the beginning and close at the end, preferably when you start UCCNC, but at least at the start/end of macro loop, not every time you enter an if state. If you are not locking the port during start-up some other software might grab the resource. Maybe that's nor realistic normally in a PC running UCCNC, but it is a possible source of issue, so generally you want to lock all the ports you need as long as UCCNC (or Mach3) is running and only open/close ports which you want to let other resources to use, shared with you, like for example a USB hard disk. I suppose this is not the case with your VFD, you don't want to share that with any other software, in case that software would grab the port between your open/close, so it is best to lock (open) when you start UCCNC. You don't have to worry about closing the port, when you close UCCNC the port will (should) be closed and released.

    Why fix the RPM? Isn't it better to read the RPM field, convert to frequency and out of the converted value make up the bytes to send plus calculate the checksum? The checksum calculation is definitely working in HY and is according to Modbus standard.

    If you think the above is too complicated you should increase the number of RPM and even add 6000. Most high speed spindles have a minimum RPM of 6000, so everything above 1 RPM and below 79999 RPM should set and send 6000RPM to the VFD.

    Comparing for an interval of values is normally done through OR statements, not through two IF statements. Try...

    if ((AS3.Getfielddouble(869) = 8000) | (AS3.Getfielddouble(869) < 10000) )

    ...instead of using...

    if (AS3.Getfielddouble(869) >= 8000 )

    if (AS3.Getfielddouble(869) <= 9999 )


    ...which is not a very nice way of doing the same and makes the code look "messier".

    The last IF statement is unnecessary because if all the others above are failed then it is obvious that the RPM should be set to 24000. Also, if you want to use the statement...

    if (AS3.Getfielddouble(869) == 24000 )

    ...then it would be better to write...

    if (AS3.Getfielddouble(869) > 23999 )

    ...since it covers all cases above 23999, even the errors of trying to set higher than 24000RPM.

    Never the less, I am glad that you got it working and the above proposals are just proposals and my opinion. The most important in my opinion is to grab the port and don't let it go inside the macro loop, but I would grab it during the startup of UCCNC.

    In my opinion, it is also a good idea to add comments and a header with some explanation about what the code is doing and how it is working. I don't write code without comments because that's causing future extra work and comments also allow for better re-usability or code improvements/modifications. I also like indenting, especially if there are a lot of loops and statements. It is difficult to find errors and visualize what the code is doing, what belongs to which part if it is written like a book, strait flow of text down the page. In this case it is an important note in the header that the "COM1 " is fixed and may not suit everybody and that it must be changed in the code before the code is used with something else than COM 1. You can say that it's obvious, but it is a good idea to document the major things to think about in the code.

    Last edited by A_Camera; 11-08-2016 at 06:38 AM.


  4. #4
    Registered
    Join Date
    Mar 2010
    Location
    America
    Posts
    642
    Downloads
    0
    Uploads
    0

    Default Re: Hunayang VFD Macro

    Hi A_C,

    Been away for the past 2 weeks with no comp access so sorry for the late response.

    The Macro does work well and thrilled that its now possible to use UCCNC with it controlling spindle. I wasn't really happy with the way the set frequency is written for the only reason that its making the macro loop very long, I would prefer a formula but with the lack of experience in macro writing that's to advanced for me write now, I will tweak as I learn.

    Is there any limit or disadvantage in size of a macro ?

    Thanks for your tips on improvement and will certainly try, that's the sort of response I was hoping to get here. I quickly tried your comparison suggestion but received a script error in UCCNC.

    Replaced

    if (AS3.Getfielddouble(869) >= 8000 )

    if (AS3.Getfielddouble(869) <= 9999 )




    with

    if ((AS3.Getfielddouble(869) = 8000) | (AS3.Getfielddouble(869) < 10000) )


    Did I do something wrong?

    Thanks for your help,
    Dan



  5. #5
    Registered
    Join Date
    Jun 2015
    Location
    Sweden
    Posts
    351
    Downloads
    0
    Uploads
    0

    Default Re: Hunayang VFD Macro

    Comparion for equal value is == and not =.in C#.
    = is giving value and not comparion.
    This is definietly wrong: AS3.Getfielddouble(869) = 8000 in your if statement.
    OR is || and not | , if I recall | is bitwise or ... probably, not sure.

    Should be OK like this: if ((AS3.Getfielddouble(869) == 8000) || (AS3.Getfielddouble(869) < 10000) )



  6. #6
    Registered
    Join Date
    Mar 2010
    Location
    America
    Posts
    642
    Downloads
    0
    Uploads
    0

    Default Re: Hunayang VFD Macro

    Thank you OlfCNC that did it.



  7. #7
    Registered
    Join Date
    Oct 2005
    Location
    USA
    Posts
    716
    Downloads
    0
    Uploads
    0

    Default Re: Hunayang VFD Macro

    HIYA Dan. I have another piece of the puzzle for you to test (;-)

    (;-) TP



  8. #8
    Registered
    Join Date
    Mar 2010
    Location
    America
    Posts
    642
    Downloads
    0
    Uploads
    0

    Default Re: Hunayang VFD Macro

    Hi TP,

    I just tested the macro you sent last week and works great! Right now I'm trying to send it to serial port instead of message box, than the last thing to overcome is the checksum. Like I mentioned in email the HY manual supply's code in C language to accomplish this. You already succeeded in accomplishing the hardiest part.

    Can't thank you enough for all your help,
    Dan



  9. #9
    Registered
    Join Date
    Mar 2010
    Location
    America
    Posts
    642
    Downloads
    0
    Uploads
    0

    Default Re: Hunayang VFD Macro

    Thanks to Vmax549 I aborted hard coding the RPM and checksum in macro since he wrote one that gets Sset ,converts to Hzs and calculates the checksum. Works Great!!! I will continue to use and test and than will post for anyone who's interested.



  10. #10
    Registered
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    1431
    Downloads
    2
    Uploads
    0

    Default Re: Hunayang VFD Macro

    Quote Originally Posted by OlfCNC View Post
    Comparion for equal value is == and not =.in C#.
    = is giving value and not comparion.
    This is definietly wrong: AS3.Getfielddouble(869) = 8000 in your if statement.
    OR is || and not | , if I recall | is bitwise or ... probably, not sure.

    Should be OK like this: if ((AS3.Getfielddouble(869) == 8000) || (AS3.Getfielddouble(869) < 10000) )
    Yes and no.

    Yes of course, it should be == not just =
    No for || vs. | but if you use || or | in this case makes no difference. If you use || then if the first evaluation is true the second will not be executed. Some times you want to make sure (especially in checking for intervals) that both evaluations are executed, even if the first evaluation is true. In other words, in my opinion it is better to use | here because what we are doing is checking for intervals. Of course, this is now irrelevant since Dan can now read and set any spindle speed and don't have to look for intervals and set it step wise.

    https://adapting-camera.blogspot.com
    https://www.youtube.com/channel/UC_vrAWOz5YGX6_e3qtXg3YA


  11. #11
    Registered
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    1431
    Downloads
    2
    Uploads
    0

    Default Re: Hunayang VFD Macro

    Quote Originally Posted by Dan911 View Post
    Hi A_C,

    Been away for the past 2 weeks with no comp access so sorry for the late response.
    Don't worry, no big deal, of course priorities must be IRL and not here. I just assumed that everything is fine so life goes on.

    Quote Originally Posted by Dan911 View Post
    The Macro does work well and thrilled that its now possible to use UCCNC with it controlling spindle. I wasn't really happy with the way the set frequency is written for the only reason that its making the macro loop very long, I would prefer a formula but with the lack of experience in macro writing that's to advanced for me write now, I will tweak as I learn.

    Is there any limit or disadvantage in size of a macro ?
    No, it actually makes no difference in this case, and it is probably more efficient than a formula because because a formula needs calculations and comparisons are very fast. It's just nicer with a formula, not necessarily more efficient. Some times it is better to write a longer program than a more condensed one, since the longer one may be easier to debug and figure out where it goes wrong. Of course, there are no general rules, except that a far too condensed code can be extremely difficult to read if it is a complex code.
    Quote Originally Posted by Dan911 View Post
    Thanks for your tips on improvement and will certainly try, that's the sort of response I was hoping to get here. I quickly tried your comparison suggestion but received a script error in UCCNC.

    Replaced

    if (AS3.Getfielddouble(869) >= 8000 )

    if (AS3.Getfielddouble(869) <= 9999 )

    with

    if ((AS3.Getfielddouble(869) = 8000) | (AS3.Getfielddouble(869) < 10000) )

    Did I do something wrong?
    No, I did. It should be:

    if ((AS3.Getfielddouble(869) == 8000) | (AS3.Getfielddouble(869) < 10000) )

    The above should work, but you can also do like Olf suggests:

    if ((AS3.Getfielddouble(869) == 8000) || (AS3.Getfielddouble(869) < 10000) )

    Quote Originally Posted by Dan911 View Post
    Thanks for your help,
    Dan
    You're welcome. Glad that it is working, especially now with the better flexibility and calculated values.

    https://adapting-camera.blogspot.com
    https://www.youtube.com/channel/UC_vrAWOz5YGX6_e3qtXg3YA


  12. #12
    Registered
    Join Date
    Mar 2010
    Location
    America
    Posts
    642
    Downloads
    0
    Uploads
    0

    Default Re: Hunayang VFD Macro

    I guess its not as many people as I thought using the HY plugin and spindle talker and would find this macro of use. The Macro works great, and my last test will be how UCCNC handles large files. This is why I found UCCNC of interest
    in the first place, Mach3 was buggy when it came to them.

    Here's the macro for anyone who wants to give it a try.

    ___________________________________________________________________________________________________________________________________________________________________


    // Macro to send Modbus string with CRC added
    // Get Sset and convert to Hzs **********************************



    if (AS3.Getfielddouble(869) >= 8000)
    {
    double Sspeed = AS3.Getfielddouble(869);
    string Over = AS3.Getfield(233);
    int index = Over.IndexOf('%');
    string sub;
    if (index >= 0)
    {
    sub = Over.Substring(0, index);
    double Sover = Convert.ToDouble (sub) ;
    double Sact = (Sspeed * (Sover/100));
    double Shzs = ( Sspeed* (Sover/100));
    Shzs =( ( Shzs *.016667)*100);
    Shzs = Math.Floor(Shzs);
    int Shzs2 = Convert.ToInt32(Shzs);
    string str = Shzs2.ToString("X");
    string full = str ;
    string part1 = ( "0x" + full.Substring(0, 2));
    int Part1 = Convert.ToInt32(part1, 16);
    string part2 = ("0x" + full.Substring(2, 2));
    int Part2 = Convert.ToInt32(part2, 16);
    System.IO.Ports.SerialPort spSerial = new System.IO.Ports.SerialPort();
    spSerial.PortName = "COM1";
    spSerial.BaudRate = 9600;
    spSerial.DataBits = 8;
    spSerial.StopBits = System.IO.Ports.StopBits.One;
    spSerial.Parity = System.IO.Ports.Parity.None;
    spSerial.Open();

    int[] intSend = new int[8];
    intSend[0] = 0x01; // ID of slave device
    intSend[1] = 0x05; // code of the command
    intSend[2] = 0x03; // Start of Address
    intSend[3] = Part1; // PART1 RPM
    intSend[4] = Part2; // PART2 RPM
    intSend[5] = 0x00; // END of string
    int int_crc = 0xFFFF;
    int int_lsb;
    int int_crc_byte_a, int_crc_byte_b;
    for (int int_i = 0; int_i < intSend.Length - 2; int_i++)
    {
    int_crc = int_crc ^ intSend[int_i];
    for (int int_j = 0; int_j < 8; int_j++)
    {
    int_lsb = int_crc & 0x0001; // Mask of LSB
    int_crc = int_crc >> 1;
    int_crc = int_crc & 0x7FFF;
    if (int_lsb == 1) int_crc = int_crc ^ 0xA001;

    }
    }

    int_crc_byte_a = int_crc & 0x00FF;
    int_crc_byte_b = (int_crc >> 8) & 0x00FF;

    byte[] bytSend = new byte[8];
    byte bytTransfer;
    for (int i = 0; i <= intSend.Length - 2; i++)
    {
    bytSend[i] = (byte)intSend[i];
    }

    bytTransfer = (byte)int_crc_byte_a;
    bytSend[intSend.Length - 2] = bytTransfer;
    bytTransfer = (byte)int_crc_byte_b;
    bytSend[intSend.Length - 1] = bytTransfer;
    spSerial.Write(bytSend, 0, bytSend.Length);
    spSerial.Close();

    }
    }


    // EOF


    if ( AS3.GetLED(50) == true ) // IF CW LED is active

    {

    System.IO.Ports.SerialPort spSerial = new System.IO.Ports.SerialPort();
    spSerial.PortName = "COM1";
    spSerial.BaudRate = 9600;
    spSerial.DataBits = 8;
    spSerial.StopBits = System.IO.Ports.StopBits.One;
    spSerial.Parity = System.IO.Ports.Parity.None;
    spSerial.Open();
    byte[] bytestosend = { 0x01, 0x3, 0x01, 0x01, 0x31, 0x88 };
    spSerial.Write(bytestosend, 0, bytestosend.Length);
    spSerial.Close();

    }

    else



    if ( AS3.GetLED(51) == true ) // IF CCW LED is active

    {
    System.IO.Ports.SerialPort spSerial = new System.IO.Ports.SerialPort();
    spSerial.PortName = "COM1";
    spSerial.BaudRate = 9600;
    spSerial.DataBits = 8;
    spSerial.StopBits = System.IO.Ports.StopBits.One;
    spSerial.Parity = System.IO.Ports.Parity.None;
    spSerial.Open();
    byte[] bytestosend = { 0x01, 0x3, 0x01, 0x011, 0x30, 0x44 };
    spSerial.Write(bytestosend, 0, bytestosend.Length);
    spSerial.Close();
    }

    else


    // Shuts off spindle

    {

    System.IO.Ports.SerialPort spSerial = new System.IO.Ports.SerialPort();
    spSerial.PortName = "COM1";
    spSerial.BaudRate = 9600;
    spSerial.DataBits = 8;
    spSerial.StopBits = System.IO.Ports.StopBits.One;
    spSerial.Parity = System.IO.Ports.Parity.None;
    spSerial.Open();
    byte[] bytestosend = { 0x01, 0x3, 0x01, 0x08, 0xf1, 0x8e };
    spSerial.Write(bytestosend, 0, bytestosend.Length);
    spSerial.Close();
    }



Page 1 of 29 1234 11 ... LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


About CNCzone.com

    We are the largest and most active discussion forum for manufacturing industry. The site is 100% free to join and use, so join today!

Follow us on


Our Brands

Huanyang VFD Macro/ Plugin
Huanyang VFD Macro/ Plugin