Uploading and running files from G code commands



  • Hi, I am looking for a way to upload files automatically to my duet3D and use them without having to use the web interface (basically I want a command prompt script to send commands to my duet3d, but directly sending the g code through usb caused a bunch of missed commands)

    I have found the M559 command, but I cannot make it work. I tried typing the whole file path:

    M559 P"C:\Users\...\Desktop\test.g"
    

    But I get an Error: Can't open file for writing. I can't find a way to use this command..

    There also is this post where @lolorc mentions "in the mean time, i'll continue to upload my files through usb serial"
    https://forum.duet3d.com/topic/8513/duetwifiserver-1-22-released
    I really tried looking at how to upload files through USB but I cannot find any documentation online.

    Then, I plan on using M23 and M24 to run the uploaded file directly from my command line application. Is this feasible? If so, would it be better/easier to use M559 (sent through USB anyway) or upload through USB (if lolorc doesnt refer to the same thing)?

    The key thing here is that I dont want to have to click anything, once the file is generated, it can be uploaded automatically and run on the duet.

    Thanks for your help!



  • @jcsb1994 said in Uploading and running files from G code commands:

    I have found the M559 command, but I cannot make it work. I tried typing the whole file path:

    path relative to the sd card on the duet.

    to upload files from computer to sd card refer to this https://forum.duet3d.com/topic/14461/new-firmware-3-01-rc2-available/17?_=1595941778209



  • @jcsb1994 said in Uploading and running files from G code commands:

    I have found the M559 command

    finally got around to trying this on a Duet2Wifi w/ RRF 3.1.1 that wasn't attached to a Raspberry Pi over the SPI interface which for now breaks some of the SD card operations..

    pi@duet2pi:~ $ cat m559.sh
    #!/bin/bash
    send_file()
    {
      PORT=/dev/$(ls $1)
      FILE=$2
    
      FILESIZE=`du -b "$FILE" | cut -f1`
      /bin/echo Device: $PORT
      /bin/echo File: $FILE \($FILESIZE bytes\)
    
      #init serial
      /bin/stty -F $PORT 57600 raw -echo &&
      #flush serial
      /usr/bin/timeout 1s /bin/echo -e "\n\n" > $PORT
      /usr/bin/timeout 2s /bin/cat $PORT &> /dev/null
      /bin/sleep 1s
      #send M559 + file (add path later?, default to 0:/sys/)
      /usr/bin/timeout 5s /bin/cat $PORT | head -n1 &
      /usr/bin/timeout 1s /bin/echo M559 P\"$FILE\"  S$FILESIZE> $PORT
      /usr/bin/timeout 5s /bin/cat $FILE >$PORT
      /bin/sleep 1s
    }
    
    #test for Duets with working RRF vendor 1d50 product 60e?
    for i in $(find -L  /sys/bus/usb/devices/ -maxdepth 2 -name "tty*"); do
      grep -i "v1d50p60e[c-e]" $i/../modalias &>/dev/null && send_file  $i test.g
    done
    

    and running the script does indeed upload the test.g file in binary mode, just to make sure comments and commands like M29 are also transferred.

    pi@duet2pi:~ $ bash m559.sh
    Device: /dev/ttyACM0
    File: test.g (93 bytes)
    Writing to file: test.g
    pi@duet2pi:~ $
    

    and the file looks like this
    dce4181e-0b9d-426f-a6e4-af87ea339469-image.png

    I didn't get it working by not specifying the file size however, but in theory it should have uploaded just the first line of the test file if I didn't specify the file size and then executed the last command instead of transfering it.

    anyways, this was kinda cool as I can transfer binary files like wifiserver.bin to the duet without removing the SD card (yay!)

    edit: ```bash seems to introduce artifacts in the formatting in for form of spurious {1}?! changed to ```text



  • @bearer said in Uploading and running files from G code commands:

    finally got around to trying this on a Duet2Wifi w/ RRF 3.1.1 that wasn't attached to a Raspberry Pi over the SPI interface which for now breaks some of the SD card operations..

    Wait, that wasn't or that was attached to a Pi?

    What language is this file in? i am not familiar with this syntax. I am really interestted in trying this out



  • @jcsb1994 said in Uploading and running files from G code commands:

    Wait, that wasn't or that was attached to a Pi?

    via usb, yes.

    What language is this file in?

    its bash, just a series of linux commands, equivalent of a batch file on windows.

    its really only a fancy way to get the filesize, and send say M559 P"0:/gcode/test.g" S93 to the serial port followed by the contents of the test.g file.

    you could use something like realterm to send the same M559 command, then send the file contents.

    the only trick here is probaly the filesize, i might poke it a little more to see why it fails without the filesize specified.



  • @jcsb1994 said in Uploading and running files from G code commands:

    i am not familiar with this syntax

    what are you familiar with, and which operating system would you need to run on btw?



  • @bearer I am familiar with C and C++
    learning bash doesnt seem too long.

    I would need the project to be cross platform. Is bash only usable with linux?

    Basically, I have a script that generates G code made out of while loops for the duet. I just want the duet to receive those commands automatically. Ideally the easiest method would be to send the commands directly to the duet via USB through a terminal opened by my script. This is what I did before I start using while loops, which do not seem to be usable with direct USB commands, and it worked great. The problem was that I need to send repetitive commands for long periods of time, and I would eventually fill the buffer because there is no way to stay in sync with where the motors are in their operations, the script has to estimate when to send the commands.
    That being said, a while loop is much more efficient and the motors can indefinitely loop. But I am forced to generate a file.g since I cannot simply send this via a USB terminal, unfortunately. I am not sure I understand the solution you offered. The duet was connected to a Pi's USB (which is linux so bash works) and then the Pi was connected to your computer?

    Since the user will be on a computer (i am on windows right now) I need the person to be able to run the script and ideally the file.g that this script generates can be automatically brought to the duet



  • @jcsb1994 said in Uploading and running files from G code commands:

    learning bash doesnt seem too long.
    I would need the project to be cross platform. Is bash only usable with linux?

    bash works pretty much everywhere its installed, but the stty command and using redirects to send data to the serial port isn't very portable outside of linux, nor the autodetection of the correct usb-serial port.

    you can do the same in c/c++ though

    • get the size of the file to be sent
    • send the M559 command with the filename and size over serial
    • send the contents of the file over serial after having sendt M559
    • once the duet has received the correct number of bytes according to the filesize it will store the file and return a "Done saving file" and "ok" string.

    you also need to sort out choosing, opening, and closing the serial port for the duet.

    I am not sure I understand the solution you offered. The duet was connected to a Pi's USB (which is linux so bash works) and then the Pi was connected to your computer?

    I log in to the pi over the network/ssh, so for all intents and purpose I'm using the pi as a linux computer with the Duet connected over its serial usb.


Log in to reply