using the serial port to send data from python to Arduino - how to do?!

dear community,

using the serial port to send data from python to Arduino

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. The funny thing: 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:

#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:

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 - can i go with this approach!?

Only one application can have a given serial port open at once. Your only way to debug what is being sent is to use an additional serial adapter (tie ground and it's RX line to ground and the serial line you want to monitor) - yes, this means you need 2 additional serial adapters if you want to monitor both directions - and yes, I have done this. Usually it's not necessary when talking to a computer though, as you generally know what the computer is sending to it - when I did it, it was because the microcontroller was talking to an xbee over serial.

If you had the python script subsequently listen for characters and print any it received, you could have it echo them back.

You could also wire the TX pin of the Arduino to a separate serial adapter (basically what I described above) to see what it's sending while the python program had the serial port open.

There is a section dedicated to Interfacing w/ Software on the Computer. Maybe your question would have fitted better there as this is a PC issue :wink: