Echo program between Arduino and Python

I want to send some data to Arduino via pyserial in Python. All I want Arduino to do is read the variable-length string data from the serial port and write it back so that Python can read it. Since I was unable to do this, the code below only has Python sent by character. Here's the Python code:

import serial import sys import pywapi import time def main(): ser = serial.Serial(3, 9600, timeout=1) print "Conn established" print "Sending: %s" % "z".__repr__() print ser.write('z'.encode("ascii")) time.sleep(2) print "Received: %s" % ser.read(10).__repr__() ser.close() 

Here is the Arduino code:

 void setup(){ analogReference(DEFAULT); Serial.begin(9600); } void loop(){ if(Serial.available() > 0) Serial.println("x"); while(Serial.available() > 0){ Serial.print(Serial.read(), BYTE); } } 

Exit:

 Conn established Sending: 'z' 1 Received: '' 

I know that the code for Arduino works because it works when data is sent from the Arduino terminal. However, the moment I try to send something with Python, it fails. I struggled with this all day. Any help would be greatly appreciated.

+4
source share
4 answers

Try increasing or deleting the timeout and setting the reading size to 1. You can also increase the sleep delay or even implement a simple reading cycle.

Sort of:

 try: while True: data = ser.read(1).__repr__() if data: print "Received: %s." % data else: print "Looping." except KeyboardInterrupt: print "Done." except: raise finally: ser.close() print "Closed port." 

Then just use ctrl-c to stop it.

+3
source

I would recommend checking the two parts independently of each other using a separate serial port and serial communication software on a PC.

eg. if your PC has two serial ports, then use a null modem (loopback) to connect them. Or use com0com to create a pair of connected virtual serial ports. Run Python software on one serial port and a terminal program (Hyperterminal or RealTerm ) on the other serial port. Manually check your Python program this way.

Then connect your PC directly to the Arduino, as usual, and use the terminal software to manually confirm the operation of the Arduino software.

This process will allow you to narrow down the problem. After you have tested both of them, they should work well together.

Serial port monitor

Another method you can use is software that connects to the PC serial port driver and allows you to monitor traffic on the serial port. I have used the Free Serial Port Monitor software from HHD Software in the past and it worked well for our purposes. It allows you to control any of the PC serial ports and shows you a log (hexadecimal text and text) of serial data passing through the port in both directions.

+3
source

Do you need to discard the transmitted character from any stored sequential buffer?

Perhaps your character does not leave the COM port and arrives at Arduino. When you test this with the Arduino Terminal (I assume you mean the UI terminal in the development environment), you are actually sending your line + carriage return, not just a character. (i.e. will you press return after you type "z" in your test?)

Try ser.flush () or maybe also send the \ r character. From your testing, Arduino works just fine, it's a python program that seems to not send anything.

0
source

The reason you have to send twice is because if you are connecting via USB, the first serial connection will be reset Arduino.

0
source

Source: https://habr.com/ru/post/1302300/


All Articles