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()
  pinMode(RELAY1, OUTPUT);       

void loop()
  if(Serial.available() > 0)
      serial =;
      Serial.println( serial, HEX);
      if (serial=='s')
        Serial.println("Light ON");
        Serial.println("Light OFF");

The python code:

Code: Select all
import time
import serial

def foo():
    ardu= serial.Serial('/dev/ttyACM0',9600, timeout=.1)


- well what do you say,,,
for Wordpress-development :: super toolkits a. :: and b.
:: Awesome WordPress: A curated list of amazingly awesome WordPress resources, themes, plugins and shiny things.
User avatar
Posts: 189
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 0 guests