G-codes/offsets/tools with .Net

Moderators: TomKerekes, dynomotion

Moray
Posts: 288
Joined: Thu Apr 26, 2018 10:16 pm

Re: G-codes/offsets/tools with .Net

Post by Moray » Tue Dec 29, 2020 7:46 pm

Thanks Tom.

That was far less painful than I thought it would be. I may have to actually learn the whole bit/byte bashing thing at some point, as it's something I have only a very vague recollection off covering many years ago.
MsgBox.png
Next step will be working out to implement the icons in a similar way to KMotionCNC, and return results.
My initial research tells me Abort, Retry, and Ignore are not available in WPF message boxes, along with the Modal options.

Moray
Posts: 288
Joined: Thu Apr 26, 2018 10:16 pm

Re: G-codes/offsets/tools with .Net

Post by Moray » Wed Dec 30, 2020 10:15 pm

Tom, that conversion works well, however it seems to produce a string I can't add anything to, and I'm not even sure how to diagnose the problem.

What I'm trying to do, is for the button options no longer supported, is add extra lines to the messagebox text, advising the options no longer work, and that the buttons are mapped accordingly.
At the moment I have MB_ABORTRETRYIGNORE mapped to generate a MessageBoxButton.YesNoCancel
And I want to add to the message "MB_ABORTRETRYIGNORE not available. Yes = RETRY, No = ABORT, Cancel = IGNORE".

However, although I can add text to the string read from the KFlop, it doesn't show in the message box.
Some testing with outputting code to my debug text box, results in the text showing in the text box, however if you try copying and pasting the text from the text box, it doesn't paste the additional text (I only realised this after trying to paste the text into Notepad++ to check for any hidden characters).

I've stripped back my code to the basics to try and understand this-

Code: Select all

string s;
                        if (!GetStringFromGather(MS.PC_comm[1], out s, 50)) break;
                        messageDisplayed = true;
                        MS.PC_comm[0] = 0;  // clear the command now that it has been executed
                        s += "more text";
                        TextBox_Debug.AppendText("\r\n" + s);
                        //MessageBoxResult result = DisplayMessageBox(s, MS.PC_comm[2]);
                        MessageBoxResult result = MessageBox.Show(s, "KMoCNCC");
                        messageDisplayed = false;
                        string st = string.Format("SetPersistDec%d %d", 103, result);
                        KM.WriteLine(st);
                        SetKFlopCommandResult(0);
Using the same KFlop C code as my previous post, to just generate a basic messagebox with just an OK button, I get this in my debug text output-
StringStrangeness.png
StringStrangeness.png (1.34 KiB) Viewed 3173 times
However although I can select the text, and copy it, whatever I paste it into, only pastes the TK Rocks!!! part. I've even tried generating more lines in my textbox, and removing the !!! but any text after the string read from the KFlop, doesn't get pasted. I suspect whatever is causing this, is also causing the lack of text to be added to the message box.

To me it's like there is some kind of magic charachter being added to the KFlop string that blocks all text after it, and I'm not even sure what to google for to even start looking for a solution :?:
Any ideas?

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

Re: G-codes/offsets/tools with .Net

Post by TomKerekes » Thu Dec 31, 2020 12:59 am

Hi Moray,

Oops I mistakenly added the zero terminator to the string. I'm not sure exactly how C# strings work internally but the null isn't needed it already is a terminated string. Change:

Code: Select all

                    msg += Convert.ToChar(val);  // add to string
to

Code: Select all

                    if (val != 0) msg += Convert.ToChar(val);  // if not the null terminator add to string
Regards,

Tom Kerekes
Dynomotion, Inc.

Moray
Posts: 288
Joined: Thu Apr 26, 2018 10:16 pm

Re: G-codes/offsets/tools with .Net

Post by Moray » Thu Dec 31, 2020 6:14 pm

Thanks Tom, that has fixed the problem.


C# generally handles strings poorly.
I've got to admit I never knew what a zero terminator was, but having reminded myself how to watch variables in Visual Studio, the \0 is blatantly obvious.

Moray
Posts: 288
Joined: Thu Apr 26, 2018 10:16 pm

Re: G-codes/offsets/tools with .Net

Post by Moray » Sat Jan 02, 2021 8:12 pm

Tom,

I'm nearly there with input boxes, but I can't seem to transfer the value back to the KFlop.

This is my test KFlop code-

Code: Select all

#include "KMotionDef.h"
#define TMP 10 // which spare persist to use to transfer data
#include "KFLOPToKMotionCNCFunctions.c"

float inputResult;
void main()
{
	//DoPCFloat(PC_COMM_SET_X,1);
	printf("Running...\n");
	int Answer = InputBox("Enter a number",&inputResult);
	printf("Answer:%d\n",Answer);
			if (Answer){
				printf("Operator Canceled\n");
			} else {
				printf("Operator Entered Value of %.3f\n",inputResult);
				
			}
}
which works as expected in KMotionCNC, however when I try it in my program, the correct response for how the dialog was closed is returned, but the KFlop always returns an entered value of 0.000 via the console.

This is my code that writes the value to the KFlop-

Code: Select all

st = "SetPersistHex102 " + DoubleToHex(fvalue,8);
                    TextBox_Debug.AppendText("\r\n" + st);
                    KM.WriteLine(st);

                    st = "SetPersistDec103 " + result;
                    TextBox_Debug.AppendText("\r\n" + st);
                    KM.WriteLine(st);
                    SetKFlopCommandResult(0);
and this is what it prints to my debug screen, and is what should be getting written to the KFlop-
SetPersistHex102 C.1EB85
SetPersistDec103 0
I'm not sure if the hex conversion is correct (c# doesn't support converting double to hex, so I'm using something I found which I think is correct), but even if it was wrong, surely the KFlop should still display some kind of value?

For reference, the corresponding code from KMotionCNC is-

Code: Select all

//s.Format("SetPersistHex%d %x", PC_COMM_PERSIST + 2, *(int*)&fvalue);
                    //if (TheFrame->KMotionDLL->WriteLine(s)) break;
                    //s.Format("SetPersistDec%d %d", PC_COMM_PERSIST + 3, result);
                    //if (TheFrame->KMotionDLL->WriteLine(s)) break;
Any idea why I can't get this to work?

Moray
Posts: 288
Joined: Thu Apr 26, 2018 10:16 pm

Re: G-codes/offsets/tools with .Net

Post by Moray » Sat Jan 02, 2021 11:45 pm

I've just been trying this some more, and even if I hard code a value into Persist102, I still get zero, unless I hardcode a big value, such as FFFFFFFF, where I then get a NaN value.

I've even tried using SetPersistDec102, but it still the same.

What am I missing?

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

Re: G-codes/offsets/tools with .Net

Post by TomKerekes » Sun Jan 03, 2021 12:09 am

Hi Moray,

InputBox is expecting a 32-bit float not a 64-bit double and persist variables are only 32 bits.

You should be able to use KM.SetUserDataFloat()

HTH
Regards,

Tom Kerekes
Dynomotion, Inc.

Moray
Posts: 288
Joined: Thu Apr 26, 2018 10:16 pm

Re: G-codes/offsets/tools with .Net

Post by Moray » Sun Jan 03, 2021 10:18 pm

Thanks Tom.
Why didn't I think of that far simpler method of transferring values?

Anyway, Input Boxes now work, and I've just ticked PC_COMM_MDI off the list, which I think means most of the more complicated PC_COMM commands have now been ticked off the list. At least the ones I need to implement -
KMo still to do.png
Once I get the tool offset handling sorted, I might even get back to working on my touch probe routines :-)

Moray
Posts: 288
Joined: Thu Apr 26, 2018 10:16 pm

Re: G-codes/offsets/tools with .Net

Post by Moray » Tue Jan 05, 2021 9:52 pm

Tom, a couple hopefully simple questions for you, in my attempt to replicate all the PC_COMM functionality.

What is the equivalent of tool_table_index (D number) in dotNet?
This is my code so far for PC_COMM_GET_MISC_SETTINGS, with the original KMotionCNC code commented out.

Code: Select all

case PC_COMM_GET_MISC_SETTINGS:  // Units, T, H, D indexes 
                    {
                        //s.Format("SetPersistHex%d %x", MainStatus.PC_comm[1], Interpreter->p_setup->length_units);
                        KM.SetUserData(MS.PC_comm[1], (int)KM.CoordMotion.Interpreter.SetupParams.LengthUnits);
                        //if (TheFrame->KMotionDLL->WriteLine(s)) break;
                        //s.Format("SetPersistHex%d %x", MainStatus.PC_comm[1] + 1, Interpreter->p_setup->selected_tool_slot);
                        KM.SetUserData(MS.PC_comm[1] + 1, (int)KM.CoordMotion.Interpreter.SetupParams.SelectedToolSlot);
                        //if (TheFrame->KMotionDLL->WriteLine(s)) break;
                        //s.Format("SetPersistHex%d %x", MainStatus.PC_comm[1] + 2, Interpreter->p_setup->length_offset_index); // H number
                        KM.SetUserData(MS.PC_comm[1] + 2, (int)KM.CoordMotion.Interpreter.SetupParams.ToolLengthOffsetIndex);
                        //if (TheFrame->KMotionDLL->WriteLine(s)) break;
                        //s.Format("SetPersistHex%d %x", MainStatus.PC_comm[1] + 3, Interpreter->p_setup->tool_table_index); // D number
                        KM.SetUserData(MS.PC_comm[1]+3, KM.CoordMotion.Interpreter.SetupParams.????)
                        //if (TheFrame->KMotionDLL->WriteLine(s)) break;
                        SetKFlopCommandResult(0);
                    }
                    break;


Also, not as important, but for the PC_COMM_GET/SET_VARS, am I correct in thinking this accesses the emc var file values?
KMotionCNC code line is-

Code: Select all

double d = Interpreter->p_setup->parameters[MainStatus.PC_comm[1] + i];
And is there a dotNet equivalent?
If it does access the var file, I'm not too bothered about it, as I don't use that file. I only use a copy, as the interpreter needs it to exist, and I store all values in my own config file.

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

Re: G-codes/offsets/tools with .Net

Post by TomKerekes » Thu Jan 07, 2021 5:24 pm

Hi Moray,

tool_table_index isn't currently exported to .NET. It would need to be added. I don't know of anyone who used it for anything. You might set it the same as H as they usually the same.

Similarly with Interpreter->p_setup->parameters[] it would need to be added. It isn't really accessing the file btw. The variables are read from the file on startup and then GCode or other code may change the variables and in that case they would be different from the file. On exit certain values have the option to be saved to the file.
Regards,

Tom Kerekes
Dynomotion, Inc.

Post Reply