What is the "send" button on the Arduino serial monitor doing? - python

This has been resolved!!
I have to wait a few seconds after opening serial port.
I want to execute the python program that is being done with the send button on the Arduino serial monitor.
if (Serial.available() > 0){
Serial.print(hoge);
}
is written in arduino, and I want to make Serial.available ()> 0 by python program.
I tried...
1.
If I send something like A or 3 on the IDE serial monitor, the contents of hoge will be output on the serial monitor.
2.
Using pyserial
ser = serial.Serial('/dev/ttyACM0', 115200,timeout=None)
ser.write(str.encode('A'))
data = ser.readline()
print(data)
When this is executed, it waits for reception before ser.read ().
After deleting the if (Serial.available ()> 0) of the program on Arduino and executing it, the contents of hoge were printed properly on the terminal.
The contents of hoge are
b'0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\r\n'
What do I need to write to do the same thing as 'send'? How do I get Serial.available ()> 0 ...?
It would be very helpful if you could tell someone.

The first data of 'hoge[]' is NULL character. Therefore there's no chance for 'Serial.print()' function to print out the whole content.
Serial.print(hoge) immediately returns because the first character is NULL character.
ser.readline() is waiting forever because there's no newline incoming from Arduino.
Simply changing the content will solve this issue or use write() function (but if you keep the same content then you need to adapt your python code as well without using readline()).
In your python code, you need to add "time.sleep()" before "ser.write()" so that Arduino is ready to receive the serial data. More than 1 seconds would be required.

Related

pyserial readline() times out after reconnection

I use pyserial to communicate with a 3D printer (Monoprice Select Mini V2) via USB. Everything works well when I connect to the printer for the first time, but when I try to reopen a connection I can still send commands but do not receive any character.
This happens when I close the port and reopen it in the same program or when I rerun for the second time a python script opening the port after the first script has returned. The only way to reconnect properly is to restart my printer or to unplug and replug it. Changing the timeout value or trying to read only one byte does not solve the problem.
Short nonworking example:
import serial
ser = serial.Serial('/dev/ttyACM0', baudrate=115200, timeout=5)
ser.write("\n".encode())
print(ser.readline().decode())
# prints 'echo:Unknown command: "~"' (Not sure why)
print(ser.readline().decode())
# prints 'ok N0 P15 B15'
ser.write("M105\n".encode())
# prints expected response
ser.close()
print(ser.isOpen())
# prints 'False'
ser.open()
print(ser.isOpen())
# prints 'True'
ser.write("\n".encode())
print(ser.readline().decode())
# times out
ser.write("M105\n".encode())
print(ser.readline().decode())
# times out

Why doesn't pyserial write on Linux?

I've coded a simple script for Windows that works fine and I have adapted it to Linux (Ubuntu). The problem is that it doesn't read the byte sent.
I tried all the different serial ports available according to the Arduino IDE but the problem persists.I also used \n and \r without success and different encodings.
Code working on win10:
import serial
import time
import keyboard
arduino = serial.Serial('COM4', 9600, timeout=0)
while True:
arduino.write('a'.encode())
time.sleep(0.1)
print(arduino.readline())
Code not working on Ubuntu:
import serial, time
arduino = serial.Serial('/dev/ttyAMC0', 9600, timeout = 0)
while True:
arduino.write('a'.encode())
time.sleep(0.1)
print(arduino.readline())
So the first script prints continuously a\r\n, the second doesn't. Simply shows b'' continuously. So I think it doesn't simply write the letter.
Solved. No idea what exactly was thr issue but worked sending capital letter.

Following Code works in python interpreter but not as a script

I just made a tiny code to change the colors of my led strip in Linux too (I already did it in C# on Windows).
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.write(b'a')
When I type this into bash like this:
$sudo python2
>>>import serial
>>>ser = serial.Serial('/dev/ttyACM0', 9600)
>>>ser.write(b'a')
1
it's working fine, but if I then execute the .py script like this:
$sudo python2 light.py
The ser.write part seems not to work. I dont get an err msg or anything. But I know that it's communicatin with the arduino cause the Onboard LED flashes when I execute the script.
Okay, got it, the Arduino resets after getting serial input, I just added a 3 Seconds delay before writing the Serial data

Pyserial read from serial port and write to C program

I want to read some data using pyserial and then send the output to a C program using subprocess.
Here is my code (in progress):
from serial import Serial
import subprocess
process = subprocess.Popen("./print",stdin=subprocess.PIPE)
ser = Serial("/dev/ttyAMA0",9600,timeout=2)
while True:
if ser.inWaiting!=0:
ser.read()
where print is the C program that simply prints the output (a stepping stone to what I actually want to do).
How do I get it so that I can write the result of ser.read() to the C program?
How do I interpret or use that input?
Write to stdin of Popen. It's a file object.
process.stdin.write(ser.read())

How can I send an SMS via AT command with Zoom 7.2m Tri-Band USB Modem?

I am trying to make a simple python 2.6 application on OSX 10.6.6 that can send and receive SMS on my Zoom 7.2m (3g) USB Modem.
On initially plugging into the USB modem, no TTY or CU sessions seem to be created. I have to run the modem software to initiate the following sessions;
cu.LJADeviceInterface2621
cu.LJADiagConnector2620
cu.LJAMobileConnector2622
tty.LJADeviceInterface2621
tty.LJADiagConnector2620
tty.LJAMobileConnector2622
After much "fun", it seems the only session I can read and write to is "cu.LJADeviceInterface2621". On attempting to connect to the tty instance of this, I get an error -
serial.serialutil.SerialException: could not open port /dev/tty.LJADeviceInterface2621: [Errno 16] Resource busy: '/dev/tty.LJADeviceInterface2621'
Thats fine though - I at least have something to work with, the cu equivalent.
My script is as follows;
ser = serial.Serial("/dev/cu.LJADeviceInterface2621", 9600, timeout=1)
print "Setting DTR..."
ser.setDTR(True)
sleep(3)
print "Turning off DTR..."
ser.setDTR(False)
searching = True
ser.setDTR(True)
while searching:
print "Write instruction..."
txt=raw_input()
if txt.find("ZZ")>-1:
txt=txt.replace("ZZ",chr(13))
ser.write(txt)
ser.close()
Now, I also have another script that is monitoring the messages on "cu.LJADeviceInterface2621". That script is as follows;
ser = serial.Serial("/dev/cu.LJADeviceInterface2621", 9600, timeout=1)
print "Attempting search."
while True:
line = ser.readline()
print ">", line
With these scripts both running, in the WRITE code, I enter the following lines;
(Note: ZZ input is replaced for Ctrl-Z via the write script above - chr(13))
AT+CMGF=1ZZ [press enter to commit write]
OK
AT+CMGW="+447725123123"\r\n [press enter to commit write]
ERROR
I should be writing the text of the message, followed by Ctrl-Z (chr(13) but I get an immediate error.
The USB modem has a valid sim, with credit, it has signal, I can send a text from the Zoom Modem Software (this however only works in with PDU mode - but the modem does support text mode, as per the AT+CMGF=? command) and receive messages.
Any ideas?
Happy to provide more information where needed, thanks
Stu
Also remember that there are many projects out there that do the task for you (pysms is one of them)
Well, I never use that modem but I suppose it uses standard GSM AT commands, and AT+CMGW is wrong.
You should send: AT+CMGS="+111111111"\r\n SMS TEXT Ctrl-Z
And that should work

Categories

Resources