Plugin for X, Y, Z to serial port for Arduino?

Post anything you want to discuss with others about the software.

Plugin for X, Y, Z to serial port for Arduino?

Postby JackBerkhout » Sun Jan 29, 2017 9:30 pm

Hi, is it possible to write a plugin in C#, that writes X, Y and Z to a serial com port?
I would get the values, something like:
X = UC.Getfield(true, 226); // XposDRO
Y = UC.Getfield(true, 227); // YposDRO
Z = UC.Getfield(true, 228); // ZposDRO
And write them say 10 times/second to a serial port?
Then I could use an Arduino with an LCD on the control panel.
I could post the Arduino solution to receive the characters from the serial port and display them on a display here, if somebody likes the idea.
User avatar
JackBerkhout
 
Posts: 8
Joined: Sun Jan 29, 2017 9:19 pm

Re: Plugin for X, Y, Z to serial port for Arduino?

Postby Vmax549 » Sun Jan 29, 2017 10:05 pm

You don't need a Plugin to do that you can do it with a simple MacroLoop.

(;-) TP
Vmax549
 
Posts: 1297
Joined: Sun Nov 22, 2015 3:25 am
Location: USA

Re: Plugin for X, Y, Z to serial port for Arduino?

Postby JackBerkhout » Mon Jan 30, 2017 1:17 am

Hi TP, I'm new to this. Can you explain for me what you mean, and if possible give me an example?
User avatar
JackBerkhout
 
Posts: 8
Joined: Sun Jan 29, 2017 9:19 pm

Re: Plugin for X, Y, Z to serial port for Arduino?

Postby Vmax549 » Mon Jan 30, 2017 2:41 am

This should help you get started. It will gather teh data and send it over teh Serial port(com1) as a string.

Create a mcaro with it then apply it as a macroLoop in UCCNC. The macroLoop is a LOOP at 25hzs.

string X = AS3.Getfield(226);
string Y = AS3.Getfield(227);
string Z = AS3.Getfield(228) ;

string XYZ = "" + X + " , "+ Y + " , " + Z;
// MessageBox.Show(""+XYZ);

System.IO.Ports.SerialPort myPort = new System.IO.Ports.SerialPort("COM1");
if (myPort.IsOpen == false) //if port (com1)not open, open the port
{
//open port
myPort.Open();
}
// Working code goes here

if (myPort.IsOpen == true)
{
// Send String through the serial port:
myPort.Write( XYZ );

}
// eof

(;-) TP
Vmax549
 
Posts: 1297
Joined: Sun Nov 22, 2015 3:25 am
Location: USA

Re: Plugin for X, Y, Z to serial port for Arduino?

Postby JackBerkhout » Mon Jan 30, 2017 8:02 am

Hi TP, that looks very good!
I created a macro m200005.txt (I believe they have to be in that range).
I checked COM1 is there: "USB Serial Port (COM1)", in the Device Manager.
It is not in use by another program (Like Putty).
I get "An exception has been thrown by the M20005 macro.
System.UnauthorizedAccessException: Access to the port 'COM1' is denied.
I tried to access it with Putty, which works, and I can see the LEDs flashing in the serial connector (FTDI).
Trying to move it to another COM number.
User avatar
JackBerkhout
 
Posts: 8
Joined: Sun Jan 29, 2017 9:19 pm

Re: Plugin for X, Y, Z to serial port for Arduino?

Postby JackBerkhout » Mon Jan 30, 2017 8:36 am

Hi TP, it works now!
Because we call the macro repeatedly, we also need to close the serial port.

Code: Select all
string X = AS3.Getfield(226);
string Y = AS3.Getfield(227);
string Z = AS3.Getfield(228) ;

string XYZ = "X=" + X + " Y="+ Y + " Z=" + Z + Environment.NewLine;
// MessageBox.Show(""+XYZ);

System.IO.Ports.SerialPort myPort = new System.IO.Ports.SerialPort("COM1");
if (myPort.IsOpen == false) //if port (com1)not open, open the port
{
    //open port
    myPort.Open();
}
// Working code goes here

if (myPort.IsOpen == true)
{
    // Send String through the serial port:
    myPort.Write( XYZ );
    myPort.Close();
}
User avatar
JackBerkhout
 
Posts: 8
Joined: Sun Jan 29, 2017 9:19 pm

Re: Plugin for X, Y, Z to serial port for Arduino?

Postby Vmax549 » Mon Jan 30, 2017 11:55 am

While this simple method does work you may want to open teh port once then write teh info from a While loop inside the macro. AND don't forget to give it a short Sleep inside teh while loop.

OR if it works ok for you and does not cause any problems let it run (;-)

To move to another unused comport (Com3) for example change teh code from COM1 to COM3.


(;-) TP
Vmax549
 
Posts: 1297
Joined: Sun Nov 22, 2015 3:25 am
Location: USA

Re: Plugin for X, Y, Z to serial port for Arduino?

Postby JackBerkhout » Mon Jan 30, 2017 3:16 pm

Hi TP, well that works well, I have just increased the baudrate to 38400, and do now also transmit F and S.
I just ordered these I2C LED-displays:
https://www.adafruit.com/products/1002
Two are needed for one 8-digit display, and 10 in total for 5 displays.
Eight addresses can be selected with address-selection jumpers, so two I2C busses are required.
It is cheaper to implement this on the mbed platform:
https://developer.mbed.org/platforms/ST-Nucleo-F446RE/
Those will be programmed in C++, using an online compiler. (registration required)

Code: Select all
// Comport specification
string ComPort = "COM1";
int    ComBaud = 38400;

// Get fields
string X = AS3.Getfield(226);   // XposDRO
string Y = AS3.Getfield(227);   // YposDRO
string Z = AS3.Getfield(228);   // ZposDRO
string F1 = AS3.Getfield(867);  // Setfeedrate
string F2 = AS3.Getfield(868);  // Actfeedrate
string S1 = AS3.Getfield(869);  // Setspindlespeed
string S2 = AS3.Getfield(870);  // Actspindlespeed

// Form a string, separated by spaces, so it can be split easily on the remote platform
string XYZ = "X=" + X + " Y=" + Y + " Z=" + Z;
XYZ += " Fs=" + F1 + " Fa=" + F2;
XYZ += " Ss=" + S1 + " Sa=" + S2;
XYZ += Environment.NewLine; // This could as well be a space character, but it displays well in a terminal for debugging
// MessageBox.Show(""+XYZ);

// Create the Serial port object
System.IO.Ports.SerialPort myPort = new System.IO.Ports.SerialPort(ComPort, ComBaud);
if (!myPort.IsOpen) // If the serial port is not open, open it
{
    // Open serial port
    myPort.Open();
}

if (myPort.IsOpen)
{
    // Send String through the serial port:
    myPort.Write( XYZ );
    // Close serial port
    myPort.Close();
}
User avatar
JackBerkhout
 
Posts: 8
Joined: Sun Jan 29, 2017 9:19 pm


Return to General discussion about the UCCNC software

Who is online

Users browsing this forum: Bing [Bot] and 3 guests