Saturday, March 2, 2024

arduino uno – Python program can learn however not write on serial monitor


I am attempting to put in writing a small python program to speak with my Arduino board however, in some way, I am having a bit bother.
My purpose is to modify on and off a LED by interacting with the desktop program in python.

In python, I’ve created a small class to deal with my requests:

class ArduinoSerialLine:
    __serial  = None

    def init(self):
        self.__serial = arv.Serial(port="COM5", baudrate=9600)
        time.sleep(1)

    def learn(self):
        if self.__serial.in_waiting > 0:
            information = self.__serial.readline().decode('utf-8')[:-2]
            print('READ_DATA::' + information)
    
    def write(self, msg: str):
        print("Writing on serial: " + msg)
        msg += "n"
        self.__serial.write(msg.encode())
        self.__serial.flush()
        time.sleep(0.5)

At first, it did not invoke time.sleep() however, I’ve added it after studying this submit, hoping that it’d assist me resolve my downside. Nothing modified.
I am positive the serial monitor is linked, for the reason that learn() perform appears to work appropriately.

As for the Arduino counterpart, I’ve tried to deal with the instructions as follows:

void loop()
{
  if (Serial.obtainable())
  {
    led.switchOn();
    delay(100);
    led.switchOff();
    delay(100);

    String msg = "";
    char c;
    do {
      c = Serial.learn();
      msg += c;
    } whereas(c != "n");
    
    // if (msg == "on")
    //   led.switchOn();
    // else if (msg == "off")
    //   led.switchOff();
  }
  delay(100);
}

I’ve commented the portion of code through which it truly switches the LED as a result of I needed to truly test if there was something to learn in any respect. Seems, it by no means even enters the if block.

Be aware that in switchOn() and switchOff() I simply use the digitalWrite perform, nothing particular.

Does anybody have an concept on how I may resolve this downside?


Edit to reply the feedback:

That is my setup() within the .ino file:

Led led();

void setup()
{
  Serial.start(9600);
  led.pin = 7;
  pinMode(led.pin, OUTPUT);
  Serial.println("System prepared");
}

The place the LED class is as follows:

class Led
{
  int pin;
  void switchOn()
  {
    digitalWrite(pin, HIGH);
  }
  void switchOff()
  {
    digitalWrite(pin, LOW);
  }
}

As for the PyScript, it is likely to be a bit extra difficult however, I will attempt my finest to provide you extra particulars!

I used to be planning on increasing the system sooner or later, so I’ve created just a few lessons. ArduinoSerialLine for now could be solely used within the class LightService.

class LightService:
    __serial: ArduinoSerialLine
    __isLightOn = False

    def LightService(self):
        None

    def init(self):
        self.__serial = ArduinoSerialLine()
        self.__serial.init()

    def checkStatus(self):
        self.__serial.learn()

    def switchOnLight(self):
        self.__isLightOn = True
        self.__serial.write("on")

    def switchOffLight(self):
        def switchOnLight(self):
        self.__isLightOn = False
        self.__serial.write("off")

The checkStatus() perform is named about each two seconds in a separate thread. I used to be planning to make use of it only for monitoring what occurs on the serial although, I am undecided whether or not it ought to be capable to learn the strains written from python. It reads appropriately the “System prepared” printed within the Arduino setup. After that, it simply skips the if block inside learn().

switchOnLights() and switchOffLights() are known as upon the interplay with a button within the GUI. I am not going to stick the whole code, however I can guarantee you that in debug it enters the features appropriately and the state of the category is identical as within the different thread. It would not skip directions in debug nor give runtime errors.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles