Nov 27, 2020

send one gcode

How to send one gcode command over USB?


 I am trying to write a simple python script that sends a gcode command to my wanhao D9 motherboard printer, running Marlin. I am running the python script on a raspberry pi that is connected to the printer via USB.
import serial

ser = serial.Serial("/dev/ttyUSB0", 115200)
ser.write("G28\n")

I have read over 20 forum pages with simular problems and tried their answers such as changing the baud rate to 250000 and the following changes to the write function parameter:

ser.write("G28\r\n")
ser.write(b'G28\r\n')
ser.write(b'G28\n')
ser.write(b'G28')
ser.write("G28")

 have tried all these combinations, and I have also added:

time.sleep(5)

along with the relevant import statement for the time module at the top of my file. I added this line of code between my ser declaration and my ser.write function call.

I have also tried adding:

ser.close()

to see if that would make a difference but it has not, as I know this is best practice anyway.

No matter what combination of this code I used, when I run my python script my printer seems to restart (the screen changes from the home page to the opening wanhao logo and back to the home page)

I look forward to any help anyone can give me in regards to my code and what I may be doing wrong. 

read_potentiometer

 read_potentiometer.ino

import serial

import time

 # set up the serial line

ser = serial.Serial('COM3', 9600)

time.sleep(2)

# Read and record the data

data =[]                       # empty list to store the data

for i in range(50):

    b = ser.readline()         # read a byte string

    string_n = b.decode()  # decode byte string into Unicode  

    string = string_n.rstrip() # remove \n and \r

    flt = float(string)        # convert string to float

    print(flt)

    data.append(flt)           # add to the end of data list

    time.sleep(0.1)            # wait (sleep) 0.1 seconds

ser.close()


read_potentiometer.ino

// potentiometer_read.ino

// reads a potentiometer and sends value over serial

int sensorPin = A0;  // The potentiometer on pin 0                  

int ledPin = 13;     // The LED is connected on pin 13

int sensorValue;     // variable to stores data


void setup() // runs once when the sketch starts

{

  // make the LED pin (pin 13) an output pin

  pinMode(ledPin, OUTPUT);


  // initialize serial communication

  Serial.begin(9600);

}


void loop() // runs repeatedly after setup() finishes

{

  sensorValue = analogRead(sensorPin);  // read pin A0   

  Serial.println(sensorValue);         // send data to serial


  if (sensorValue < 500) {            // less than 500?

    digitalWrite(ledPin, LOW); }     // Turn the LED off


  else {                               // greater than 500?

    digitalWrite(ledPin, HIGH); }     // Keep the LED on


  delay(100);             // Pause 100 milliseconds

}

Nov 26, 2020

How PWM works | Controlling a DC motor with a homemade circuit

How PWM works | Controlling a DC motor with a homemade circuit

https://jlcpcb.com



Nov 25, 2020

read/write

Read/Write

===========================

i = 1

while i < 600:

  print(i)

  i += 100

else:

  #print("i is no longer less than 6")

  print(i)

================================= 

#for x in range(1,6,2):

#for x in range(1,6):                

# print(x)

=============================

 def my_function(): 

   print("Hello from a function")

 my_function()

====================================

 f = open("demofile.txt", "r")

 print(f.read())

=======================================

f = open("demofile2.txt", "a")

f.write("Now the file has more content!")

f.close()

#open and read the file after the appending:

f = open("demofile2.txt", "r")

print(f.read()) 

============================================