• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

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

Question

tarifa    0

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(12600);
  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 see the python code

 

import time
import serial

def foo():
    print("sent")
    ardu= serial.Serial('/dev/ttyACM0',12600, 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!?

Share this post


Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.