How to write analog input values to file on disk with macro

This is where you talk about Macros, show examples of your macro scripting and SHARE handy segments of script code as examples.

How to write analog input values to file on disk with macro

Postby Mudden » Thu Oct 26, 2023 9:33 am

Hi! :)
I'm a new forum user but long time user of the UCCNC-software on serveral machines. Based in Sweden.

In one of the machines, a simple CNC-controlled measuring machine, installed in a factory that manufactures plastic components, I have an AXBB-E motion controller with two "Sick Magnetic Cylinder Sensor - Position Sensor" connected to the AI1 and AI2 ports (analog inputs 0-5V). I run the AXBB-E controller with UCCNC.

The machine acts a simple measuring machine with two pneumatic cylinders that is measuring heights between 0-5 mm.
You put the product in and press start. The cylinders go down.
If the measurement is OK the product pass.
If the measurement is NOK the product failes.
The machine is working as intended.

However, I now want to record the measured heights and export the value to a text file on C: for statistics.

How do I do that within a macro?
I have found forum topics suggesting using something called "system.IO.StreamWriter" but if I add "using System.IO;" in my code it does not run the script I just get a syntax error from UCCNC.


This is how I collect the value from the analogue sensors.
Code: Select all
int AnalogIn1;          //Variabel analog input 1
int AnalogIn2;          //Variabel analog input 2 
AnalogIn1 = exec.Getanaloginput(1) / 100; //Read analog input 1
AnalogIn2 = exec.Getanaloginput(2) / 100; //Read analog input 2


Now, how do I write the values of AnalogIn1 and AnalogIn2 to a file on C:\ ?

Thanks for any suggestions.
Mudden
 
Posts: 19
Joined: Thu Oct 26, 2023 9:09 am

Re: How to write analog input values to file on disk with ma

Postby dezsoe » Fri Oct 27, 2023 9:55 pm

You can use the System.IO.StreamWriter, you just can't include the "using ..." lines. This means that you always have to write the full name System.IO.StreamWriter, e.g.:

Code: Select all
using (System.IO.StreamWriter sw = System.IO.File.CreateText(sOutputPath))
{
   sw.WriteLine("(Variables #500..#" + iUpperLimit + " saved from probing)");
   sw.WriteLine();
   sw.WriteLine("(Probe file: " + sFileName + ")");
   sw.WriteLine("(Results   : " + sOutputPath + ")");
   sw.WriteLine();
   for (int i = 500; i <= iUpperLimit; i++)
      sw.WriteLine("#" + i + " = " + exec.ivars[i]);
   sw.Close();
}
dezsoe
 
Posts: 2093
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: How to write analog input values to file on disk with ma

Postby Mudden » Tue Nov 07, 2023 11:58 am

Thank you for your suggestion, unfortunantely it does not work. As soon as i add the line:
Code: Select all
using (System.IO.StreamWriter sw = System.IO.File.CreateText(sOutputPath))
I get error: "Script error: M20191 has error(s) and cannot execute."

I tried to change the code to something else, like, for instance:
Code: Select all
using (System.IO.StreamWriter sw = System.IO.File.CreateText(textfile.txt))
using (System.IO.StreamWriter sw = System.IO.File.CreateText("textfile.txt"))
using (System.IO.StreamWriter sw = System.IO.File.CreateText("C:\textfile.txt"))
using (System.IO.StreamWriter)
None of the above worked. Every line gave the same output, "Script error: M20191 has error(s) and cannot execute."

I also tried this:
Code: Select all
string filename = @"C:\UCCNCnolicence\testfile.txt";
using (System.IO.StreamWriter sw = System.IO.File.CreateText(filename))
and it did not work either.

I also tried another syntax:
Code: Select all
using System.IO.StreamWriter;
But that also failed.

So, the question remains, how do I write data to a file on disk, with the help of a macro code??
Mudden
 
Posts: 19
Joined: Thu Oct 26, 2023 9:09 am

Re: How to write analog input values to file on disk with ma

Postby Mudden » Tue Nov 07, 2023 12:15 pm

I added a ; after (filname)) and now it gave no error message! It's always a missing ; that fails :D

Code: Select all
string filename = @"C:\UCCNCnolicence\testfile.txt";
using (System.IO.StreamWriter sw = System.IO.File.CreateText(filename));


However, now I must figure out on how to actually write the file and add the data in it. ;)

My code is now as follows:
Code: Select all
string filename = @"C:\UCCNCnolicence\testfile.txt";
using (System.IO.StreamWriter sw = System.IO.File.CreateText(filename));
sw.WriteLine("(SampleText   : " + filename + ")");
And I would guess that the row sw.WriteLine would add the data "SampleText in the file C:\UCCNCnolicence\testfile.txt but it doesn't, instead I get script error...
Mudden
 
Posts: 19
Joined: Thu Oct 26, 2023 9:09 am

Re: How to write analog input values to file on disk with ma

Postby dezsoe » Tue Nov 07, 2023 12:34 pm

As I wrote you cannot insert "using ..." lines. This is because your macro will be inserted into a function of a class and the using must be out of anything else.

Here is a shorter test macro, I've just run it:

Code: Select all
// use @ or escape the backlash: @"c:\something.txt" or "c:\\something.txt"

using (System.IO.StreamWriter sw = System.IO.File.CreateText(@"E:\testfile.txt"))
{
   sw.WriteLine("Line1");
   sw.WriteLine();
   sw.WriteLine("Line3");
   sw.WriteLine();
   for (int i = 1; i <= 5; i++)
      sw.WriteLine("#" + i);
   sw.Close();
}

And its result:

Code: Select all
Line1

Line3

#1
#2
#3
#4
#5

Note that normally you don't have rights to write into C:\, so define a working path, e.g. in your user folder.
dezsoe
 
Posts: 2093
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: How to write analog input values to file on disk with ma

Postby Mudden » Tue Nov 07, 2023 2:12 pm

I'm not sure I understand what you mean by I cannot use the "Using..." command since the code contains the Using command. :?

However, it works, :D
I copy-pasted your code example and It seems to work as expected besides I do not have write access to C:\ so I changed it to a path that i do have write-access to and it creates the textfile "testfile.txt" and it contains:
Code: Select all
Line1

Line3

#1
#2
#3
#4
#5



Thank you so much for pointing me in the right direction, :D
I will modify the code so it fits my analogue measurement and dump the values of the measurements in the file.


I will however need to develope the code further so that I get a new file every time I perform the measuring, maybe I can fetch the system time to create the file name :?:


Edit:
This is how I wrote the code to get uniqe time-stamps in the file name for one day. This resulting in a new file for every measurement.
Code: Select all
DateTime currentDateTime = DateTime.Now;
string formattedDateTime = currentDateTime.ToString("yyyymmdd_hhmmss");
exec.AddStatusmessage("Current date and time: " + formattedDateTime);
string filename = @"X:\FOLDER\LOGS\" + formattedDateTime + ".txt";
using (System.IO.StreamWriter sw = System.IO.File.CreateText(filename))
Mudden
 
Posts: 19
Joined: Thu Oct 26, 2023 9:09 am

Re: How to write analog input values to file on disk with ma

Postby ger21 » Tue Nov 07, 2023 3:55 pm

I get error: "Script error: M20191 has error(s) and cannot execute."

There's an error log file in the macro folder than will often give you an idea what the error is. It can be very helpful at times.
Gerry
UCCNC 2022 Screenset - http://www.thecncwoodworker.com/2022.html
ger21
 
Posts: 2720
Joined: Sat Sep 03, 2016 2:17 am


Return to Macros

Who is online

Users browsing this forum: No registered users and 2 guests