Page 1 of 1

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

PostPosted: Sun Jan 29, 2017 9:30 pm
by JackBerkhout
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.

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

PostPosted: Mon Jan 30, 2017 1:17 am
by JackBerkhout
Hi TP, I'm new to this. Can you explain for me what you mean, and if possible give me an example?

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

PostPosted: Mon Jan 30, 2017 8:02 am
by JackBerkhout
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.

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

PostPosted: Mon Jan 30, 2017 8:36 am
by JackBerkhout
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();
}

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

PostPosted: Mon Jan 30, 2017 3:16 pm
by JackBerkhout
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();
}