• 0

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

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

The python code see the python code


import time
import serial

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


well what do you say - can i go with this approach!?

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

This topic is now closed to further replies.

  • Recently Browsing   0 members

    No registered users viewing this page.