using the serial port to send data from python to Arduino

Einfach Dinge, die nichts mit XAMPP, Apache Friends, Apache, MySQL, PHP und alle dem zu tun haben. Allerlei halt. ;)

using the serial port to send data from python to Arduino

Postby unleash_it » 10. October 2019 21:49

dear experts,


To communicate with the Arduino board from a Windows machine, we have to install PySerial.

well i have tried to open the serial monitor to see if python was actually sending the char "s" but if I open the serial monitor before sending with python the char python prints an error that says "Error 5. Access Denied". This is an annoying issue - i need to debug all the things.
If I send the char "s" before opening the serial monitor the serial monitor won't start stating "Port already in use".
That is why I guessed there was some kind of lock of system to put in order to write/read from serial.


what did i do to get the thing up and working:

- i already have installed pyserial - since I was having problems with serial
- i have made sure that i ve installed the correct serial driver for the board.
- then, i also made sure that i am using the correct com port.
- i run the arduino IDE, upload the program to the arduino, and then under the tool menu (in the IDE), i set the com port and run the serial monitor. - - subseqnently, i did the following step: in the serial monitor, i entered an 's' to verify that i see the light flashing and the light off messages.

to test the whole thing i did a minimal viable code:
- i runned the following arduino- and python-codes, stripped to the absolute minimum (a MVP) set of instructions to demonstrate an example,
- i added a println() statement (in the arduino code) to echo the received characters in hex. this is a piece of debugging statement that will help me to sort out line feeds and so forth as i develop the code.


here a sample of the codes as listed here below, to work on my board and Linux machine after changing the pin number for the relay,
and the device name for the port.
- The close() is commented-out - the whole thing runs and works without that line.

On the arduino:

Code: Select all
#include <stdlib.h>

char serial;
#define RELAY1  7                       
void setup()
{   
  Serial.begin(9600);
  pinMode(RELAY1, OUTPUT);       
}

void loop()
{
  if(Serial.available() > 0)
  {
      serial = Serial.read();
      Serial.println( serial, HEX);
      if (serial=='s')
      {
        digitalWrite(RELAY1,0);           
        Serial.println("Light ON");
        delay(2000);                                     
        digitalWrite(RELAY1,1);         
        Serial.println("Light OFF");
        delay(2000);
      }
   }
}



The python code:

Code: Select all
import time
import serial

def foo():
    print("sent")
    ardu= serial.Serial('/dev/ttyACM0',9600, timeout=.1)
    time.sleep(1)
    ardu.write('s'.encode())
    time.sleep(1)
    #ardu.close()

foo()


- well what do you say,,,
unleash_it
 
Posts: 56
Joined: 10. December 2011 18:32
Operating System: linux opensuse 12.1

Return to Allerlei

Who is online

Users browsing this forum: No registered users and 2 guests