Sending Serial Data to Arduino Using Python Script












0














I am facing a peculiar issue while writing characters to Arduino using python serial communication on macOS 10.14.



The Arduino is programmed to read a string, parse it and take PWM action to run a car.



Ardiuno's serial communication channel is configured to receive the strings in the format < A, B, C, D > where ABCD are numbers which denote car direction, speed, steering direction and steering position.



The problem is, when I send a string from the serial monitor or through the Python Development environment the string is received, parsed properly and command executed successfully.



However if I write a simple program in a file write.py and execute it from the command line, nothing happens.



import serial
ser = serial.Serial('/dev/cu.usbmodem14301', 9600)
data = '<1,150,0,0>'
ser.write(data.encode())


If I run this script from the macOS terminal using the command:



python write.py


nothing happens. What am I missing here?










share|improve this question




















  • 1




    Possible duplicate of Serial communication between Arduino and Matlab is losing data
    – gre_gor
    Nov 13 '18 at 0:14
















0














I am facing a peculiar issue while writing characters to Arduino using python serial communication on macOS 10.14.



The Arduino is programmed to read a string, parse it and take PWM action to run a car.



Ardiuno's serial communication channel is configured to receive the strings in the format < A, B, C, D > where ABCD are numbers which denote car direction, speed, steering direction and steering position.



The problem is, when I send a string from the serial monitor or through the Python Development environment the string is received, parsed properly and command executed successfully.



However if I write a simple program in a file write.py and execute it from the command line, nothing happens.



import serial
ser = serial.Serial('/dev/cu.usbmodem14301', 9600)
data = '<1,150,0,0>'
ser.write(data.encode())


If I run this script from the macOS terminal using the command:



python write.py


nothing happens. What am I missing here?










share|improve this question




















  • 1




    Possible duplicate of Serial communication between Arduino and Matlab is losing data
    – gre_gor
    Nov 13 '18 at 0:14














0












0








0







I am facing a peculiar issue while writing characters to Arduino using python serial communication on macOS 10.14.



The Arduino is programmed to read a string, parse it and take PWM action to run a car.



Ardiuno's serial communication channel is configured to receive the strings in the format < A, B, C, D > where ABCD are numbers which denote car direction, speed, steering direction and steering position.



The problem is, when I send a string from the serial monitor or through the Python Development environment the string is received, parsed properly and command executed successfully.



However if I write a simple program in a file write.py and execute it from the command line, nothing happens.



import serial
ser = serial.Serial('/dev/cu.usbmodem14301', 9600)
data = '<1,150,0,0>'
ser.write(data.encode())


If I run this script from the macOS terminal using the command:



python write.py


nothing happens. What am I missing here?










share|improve this question















I am facing a peculiar issue while writing characters to Arduino using python serial communication on macOS 10.14.



The Arduino is programmed to read a string, parse it and take PWM action to run a car.



Ardiuno's serial communication channel is configured to receive the strings in the format < A, B, C, D > where ABCD are numbers which denote car direction, speed, steering direction and steering position.



The problem is, when I send a string from the serial monitor or through the Python Development environment the string is received, parsed properly and command executed successfully.



However if I write a simple program in a file write.py and execute it from the command line, nothing happens.



import serial
ser = serial.Serial('/dev/cu.usbmodem14301', 9600)
data = '<1,150,0,0>'
ser.write(data.encode())


If I run this script from the macOS terminal using the command:



python write.py


nothing happens. What am I missing here?







python-3.x arduino pyserial






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 16:08









Paul Roub

32.6k85773




32.6k85773










asked Nov 12 '18 at 11:05









Yashwant SinghYashwant Singh

31




31








  • 1




    Possible duplicate of Serial communication between Arduino and Matlab is losing data
    – gre_gor
    Nov 13 '18 at 0:14














  • 1




    Possible duplicate of Serial communication between Arduino and Matlab is losing data
    – gre_gor
    Nov 13 '18 at 0:14








1




1




Possible duplicate of Serial communication between Arduino and Matlab is losing data
– gre_gor
Nov 13 '18 at 0:14




Possible duplicate of Serial communication between Arduino and Matlab is losing data
– gre_gor
Nov 13 '18 at 0:14












3 Answers
3






active

oldest

votes


















0














A new USB connection with ser=serial.Serial('/dev/cu.usbmodem14301',9600) resets the Arduino. The data sent right after connection are lost because the Arduino boots.






share|improve this answer





















  • This works. I was establishing the serial connection within the function so every time i was writing to Arduino a new connection was getting established, moving the connection declaration out function scope solved the problem.
    – Yashwant Singh
    Nov 12 '18 at 17:13



















0














It may be that the port is in text mode and will not send the data until a newline is sent:



data = '<1,150,0,0>n'
ser.write(data.encode())


or flush() is called.



data = '<1,150,0,0>'
ser.write(data.encode())
ser.flush()





share|improve this answer





















  • I have used a sketch shared in Arduino forum which has start marker and end marker configurable, newline should be applicable when i send serial write using Python Shell, it works perfectly when using serial monitor in Arduino IDE or Python shell. Will try adding flush in the code and see if that works.
    – Yashwant Singh
    Nov 12 '18 at 11:26



















0














The most probably thing happening here is that the data is not being sent to the serial port.



There is a simple method to check this.
Connect the Arduino to your laptop (I suspect it to be a mac), and start the serial monitor on the Arduino IDE.
In the serial monitor type in <1,150,0,0> and press send.



The tx LED on the Arduino will blink. Now that you know how the pattern looks, repeat the same experiment with the Python code.



If the LED does not blink in the same manner you have a Serial port access issue, which you can fix using the instructions in the following link
Access USB serial ports using Python and pyserial



If not, I am stumped.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260841%2fsending-serial-data-to-arduino-using-python-script%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    A new USB connection with ser=serial.Serial('/dev/cu.usbmodem14301',9600) resets the Arduino. The data sent right after connection are lost because the Arduino boots.






    share|improve this answer





















    • This works. I was establishing the serial connection within the function so every time i was writing to Arduino a new connection was getting established, moving the connection declaration out function scope solved the problem.
      – Yashwant Singh
      Nov 12 '18 at 17:13
















    0














    A new USB connection with ser=serial.Serial('/dev/cu.usbmodem14301',9600) resets the Arduino. The data sent right after connection are lost because the Arduino boots.






    share|improve this answer





















    • This works. I was establishing the serial connection within the function so every time i was writing to Arduino a new connection was getting established, moving the connection declaration out function scope solved the problem.
      – Yashwant Singh
      Nov 12 '18 at 17:13














    0












    0








    0






    A new USB connection with ser=serial.Serial('/dev/cu.usbmodem14301',9600) resets the Arduino. The data sent right after connection are lost because the Arduino boots.






    share|improve this answer












    A new USB connection with ser=serial.Serial('/dev/cu.usbmodem14301',9600) resets the Arduino. The data sent right after connection are lost because the Arduino boots.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 '18 at 12:22









    JurajJuraj

    344310




    344310












    • This works. I was establishing the serial connection within the function so every time i was writing to Arduino a new connection was getting established, moving the connection declaration out function scope solved the problem.
      – Yashwant Singh
      Nov 12 '18 at 17:13


















    • This works. I was establishing the serial connection within the function so every time i was writing to Arduino a new connection was getting established, moving the connection declaration out function scope solved the problem.
      – Yashwant Singh
      Nov 12 '18 at 17:13
















    This works. I was establishing the serial connection within the function so every time i was writing to Arduino a new connection was getting established, moving the connection declaration out function scope solved the problem.
    – Yashwant Singh
    Nov 12 '18 at 17:13




    This works. I was establishing the serial connection within the function so every time i was writing to Arduino a new connection was getting established, moving the connection declaration out function scope solved the problem.
    – Yashwant Singh
    Nov 12 '18 at 17:13













    0














    It may be that the port is in text mode and will not send the data until a newline is sent:



    data = '<1,150,0,0>n'
    ser.write(data.encode())


    or flush() is called.



    data = '<1,150,0,0>'
    ser.write(data.encode())
    ser.flush()





    share|improve this answer





















    • I have used a sketch shared in Arduino forum which has start marker and end marker configurable, newline should be applicable when i send serial write using Python Shell, it works perfectly when using serial monitor in Arduino IDE or Python shell. Will try adding flush in the code and see if that works.
      – Yashwant Singh
      Nov 12 '18 at 11:26
















    0














    It may be that the port is in text mode and will not send the data until a newline is sent:



    data = '<1,150,0,0>n'
    ser.write(data.encode())


    or flush() is called.



    data = '<1,150,0,0>'
    ser.write(data.encode())
    ser.flush()





    share|improve this answer





















    • I have used a sketch shared in Arduino forum which has start marker and end marker configurable, newline should be applicable when i send serial write using Python Shell, it works perfectly when using serial monitor in Arduino IDE or Python shell. Will try adding flush in the code and see if that works.
      – Yashwant Singh
      Nov 12 '18 at 11:26














    0












    0








    0






    It may be that the port is in text mode and will not send the data until a newline is sent:



    data = '<1,150,0,0>n'
    ser.write(data.encode())


    or flush() is called.



    data = '<1,150,0,0>'
    ser.write(data.encode())
    ser.flush()





    share|improve this answer












    It may be that the port is in text mode and will not send the data until a newline is sent:



    data = '<1,150,0,0>n'
    ser.write(data.encode())


    or flush() is called.



    data = '<1,150,0,0>'
    ser.write(data.encode())
    ser.flush()






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 '18 at 11:16









    Roger IrwinRoger Irwin

    175




    175












    • I have used a sketch shared in Arduino forum which has start marker and end marker configurable, newline should be applicable when i send serial write using Python Shell, it works perfectly when using serial monitor in Arduino IDE or Python shell. Will try adding flush in the code and see if that works.
      – Yashwant Singh
      Nov 12 '18 at 11:26


















    • I have used a sketch shared in Arduino forum which has start marker and end marker configurable, newline should be applicable when i send serial write using Python Shell, it works perfectly when using serial monitor in Arduino IDE or Python shell. Will try adding flush in the code and see if that works.
      – Yashwant Singh
      Nov 12 '18 at 11:26
















    I have used a sketch shared in Arduino forum which has start marker and end marker configurable, newline should be applicable when i send serial write using Python Shell, it works perfectly when using serial monitor in Arduino IDE or Python shell. Will try adding flush in the code and see if that works.
    – Yashwant Singh
    Nov 12 '18 at 11:26




    I have used a sketch shared in Arduino forum which has start marker and end marker configurable, newline should be applicable when i send serial write using Python Shell, it works perfectly when using serial monitor in Arduino IDE or Python shell. Will try adding flush in the code and see if that works.
    – Yashwant Singh
    Nov 12 '18 at 11:26











    0














    The most probably thing happening here is that the data is not being sent to the serial port.



    There is a simple method to check this.
    Connect the Arduino to your laptop (I suspect it to be a mac), and start the serial monitor on the Arduino IDE.
    In the serial monitor type in <1,150,0,0> and press send.



    The tx LED on the Arduino will blink. Now that you know how the pattern looks, repeat the same experiment with the Python code.



    If the LED does not blink in the same manner you have a Serial port access issue, which you can fix using the instructions in the following link
    Access USB serial ports using Python and pyserial



    If not, I am stumped.






    share|improve this answer


























      0














      The most probably thing happening here is that the data is not being sent to the serial port.



      There is a simple method to check this.
      Connect the Arduino to your laptop (I suspect it to be a mac), and start the serial monitor on the Arduino IDE.
      In the serial monitor type in <1,150,0,0> and press send.



      The tx LED on the Arduino will blink. Now that you know how the pattern looks, repeat the same experiment with the Python code.



      If the LED does not blink in the same manner you have a Serial port access issue, which you can fix using the instructions in the following link
      Access USB serial ports using Python and pyserial



      If not, I am stumped.






      share|improve this answer
























        0












        0








        0






        The most probably thing happening here is that the data is not being sent to the serial port.



        There is a simple method to check this.
        Connect the Arduino to your laptop (I suspect it to be a mac), and start the serial monitor on the Arduino IDE.
        In the serial monitor type in <1,150,0,0> and press send.



        The tx LED on the Arduino will blink. Now that you know how the pattern looks, repeat the same experiment with the Python code.



        If the LED does not blink in the same manner you have a Serial port access issue, which you can fix using the instructions in the following link
        Access USB serial ports using Python and pyserial



        If not, I am stumped.






        share|improve this answer












        The most probably thing happening here is that the data is not being sent to the serial port.



        There is a simple method to check this.
        Connect the Arduino to your laptop (I suspect it to be a mac), and start the serial monitor on the Arduino IDE.
        In the serial monitor type in <1,150,0,0> and press send.



        The tx LED on the Arduino will blink. Now that you know how the pattern looks, repeat the same experiment with the Python code.



        If the LED does not blink in the same manner you have a Serial port access issue, which you can fix using the instructions in the following link
        Access USB serial ports using Python and pyserial



        If not, I am stumped.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 11:39









        NitroNitro

        666213




        666213






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260841%2fsending-serial-data-to-arduino-using-python-script%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation