On Tue, Dec 8, 2020 at 7:53 AM <ken.stra...@gmail.com> wrote:

I am attempting to produce a Python component which periodically outputs X/Y/Z/A coordinates via a serial port that is actually a USB
connection.
This is similar to what is required for pendants that display the current coordinates. Can anyone suggest a tutorial or source code that could be adapted to my needs?

I have looked at http://linuxcnc.org/docs/html/man/man1/xhc-hb04.1.html
but
I'm hoping for something simpler and a little easier to adapt to Python.


I have made something similar to what you ask between a Raspberry and an Arduino board with a 16x2 LCD display.
The Python program that runs on the Raspberry is this:

------
import linuxcnc
import serial, time

ser = serial.Serial('/dev/ttyUSB0')  # open serial port
data = linuxcnc.stat()

try:
        while True:
              data.poll()
              x=data.actual_position[0]-data.g5x_offset[0]
              y=data.actual_position[1]-data.g5x_offset[1]
              z=data.actual_position[2]-data.g5x_offset[2]
              a=data.actual_position[3]-data.g5x_offset[3]
              r0=("X{x:5.2f} Y{y:5.2f}".format(x=x,y=y))
              r1=("Z{z:5.2f} A{a:5.2f}".format(z=z,a=a))

              ser.write(r0.encode('utf-8')+'%')
              ser.write(r1.encode('utf-8')+'%')
              time.sleep(.05)

except KeyboardInterrupt:
        ser.close()
------

The Arduino program is this:

------
/*    ernesto.lova...@unipa.it @ 2020 */

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);
char riga=0;

void setup() {
  Serial.begin(9600);
  while (!Serial) ;
      Serial.print("Avvio");
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Avvio");
}

void loop() {
          while(Serial.available()) {
          inputString=Serial.readStringUntil('%');
          Serial.println(inputString);
          lcd.setCursor(0, riga);
          lcd.print(inputString);
          riga = 1-riga;
         }
}
------

I used the '%' character to signal the newline which was giving me some trouble.

Let me know if that's what you were looking for.

Ernesto Lo Valvo




--
From:    Prof. Ernesto Lo Valvo

Dipartimento di Architettura
Universita' di Palermo
Viale delle Scienze
90128     Palermo (Italy)
Tel: +39-091-23861845 (direct)



_______________________________________________
Emc-users mailing list
Emc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/emc-users

Reply via email to