Trouble sending data from Matlab to Python via serial port - python-3.x

to better understand serial port communication, I am trying to write some sample code, where Matlab has a loop running, which continously sends data to a serial port, while a Python script running on the same windows machine listens to this port, then receives and prints any received data.
In Matlab, I have written a simple loop, which sends 99 test signals to Port COM1,
% Setup a serial port and open it
tep=serial("COM1", "Baudrate", 9600);
fopen(tep);
% this loop is supposed to send a number to a serial port repeatedly
n = 1; % counter variable
while n < 100
fprintf(tep,"50"); % Send data to serial port
n = n + 1; % Counter variable
pause(0.5) % Sleep to make this loop run for a total of 50s0
fprintf('run'); % Test output within matlab to check, whether it runs
end
% finally close the serial port
fclose(tep);
as far as I can tell, this Matlab part works, as it prints "run" every hald second.
The Python listener:
import serial
import time
# set up the serial line same as in Matlab
ser = serial.Serial('COM1', 9600)
time.sleep(2)
# Read and record the data
data =[] # empty list to store the data
for i in range(50):
b = ser.readline() # read a byte string
string_n = b.decode() # decode byte string into Unicode
string = string_n.rstrip() # remove \n and \r
flt = float(string) # convert string to float
print(flt)
data.append(flt) # add to the end of data list
time.sleep(0.1) # wait (sleep) 0.1 seconds
ser.close()
# show the data
for line in data:
print(line)
Running the script in Python results in the following error:
serial.serialutil.SerialException: could not open port 'COM1': PermissionError(13, 'Zugriff verweigert', None, 5)
Obviously the port is already in use by Matlab, as it sends information to it, but I don't understand, why that is a problem. Shouldn't it be fine, for one program to send data to it and for another to receive data from it?
Kind regards.

I'm afraid you cannot connect to the same serial port from two different processes.
That means if you open the serial port from Matlab for writing you cannot open it from Python to read.
The serial port is intended to send data from a computer to another computer or device not to share data between different applications (to do that there are better ways like writing to a file or to a chunk of shared memory).
Having said that, if what you are trying to do is for debugging purposes or just learning you might want to investigate com0com which allows you to create a pair of virtual (software) serial ports. This would be the same as having a couple of hardware (real) serial ports wired to each other. Since now you have two ports you can send data from Matlab on one and read from Python on the other.
This is one of the most recurring questions about serial ports, so you should be able to find a lot of good resources. You might want to start here.

Related

How to get the raw serial output from a serial device file without using pySerial?

I have an embedded development board connected to my Ubuntu host PC. And the board's serial port is exposed as /dev/ttyACM0 file.
I experiment with below naïve python code to read the raw serial output from the board.
with open("/dev/ttyACM0", "r") as sdf:
content = sdf.readline()
print(content)
I first let the board to output some string.
After a while, when I am sure the string output is finished, I run above script. It does print out the readable string. (BTW, it seems the serial output from the board seems to be cached somewhere.)
My questions are:
With tools like putty, I need to specify some parameters of the serial port, such as baud rate, etc. But I didn't specify anything in my script. How could it ever work?
If the serial port is outputting a stream of binary data endlessly, how to capture them lively?
Note: I don't want to use pySerial or other packages. I just want to do it from scratch.

Serial Communication with ASCII Commands in Python

Hello guys I am attempting to write ASCII commands & in return read the serial buffer & get serial data. I am having an issue parsing the serial data out to only read the serial data I want to see and use.
The Code:
import serial
# Serial Connection
ser = serial.Serial(
port="COM2", baudrate = 38400,
parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS, timeout=1)
# Write ASCII Commands To TSI 4043 Flow Sensor
ser.write(b'?\r\n') # Ask Sensor if it is getting a signal (Returns "OK")
ser.write(b'SUS\r\n') # Set Flow type to SLPM (Returns "OK")
ser.write(b'SG0\r\n') # Set Flow Gas to Air (Returns "OK")
ser.write(b'SSR0010\r\n') # Set Sample Rate to 10ms (Returns "OK")
ser.write(b'DAFxx0010\r\n') # Read 10 sensor Flow Reading
# Read ASCII From Serial Buffer
output = ser.read(100) # Read 100 bytes from serial buffer
print(output)
OUTPUT:
b'OK\r\nOK\r\nOK\r\nOK\r\nOK\r\n80.63,80.42,80.52,80.32,80.33,80.66,79.88,80.39,80.26,80.28\r\n'
What I want to happen:
OK
OK
OK
OK
80.63,80.42,80.52,80.32,80.33,80.66,79.88,80.39,80.26,80.28
The OKs are just for debugging purposes. What I really need is to put all of the flow readings into an array so I can average it so I can watch the change in real time and control a motor.
You should change Your approach a bit.
Read from serial port after every ser.write and parse / process it's output.
Check if You actually receive b'OK\r\n' response from the device and make sure You comply with Device communication protocol (what are possible outputs for every command You send). You never know when Device throw an error while processing the command.
Like this You will already have communication in a way WRITE command => READ output for every command You send.
Then it comes to the part of flow readout. You should have just b'80.63,80.42,80.52,80.32,80.33,80.66,79.88,80.39,80.26,80.28\r\n' in Your output.
You can process it and get average with:
# Output value from DAFxx0010 command
output = b'80.63,80.42,80.52,80.32,80.33,80.66,79.88,80.39,80.26,80.28\r\n'
# Decode bytes to string, remove `\r\n`, split by ',' and convert values to float and save them into list
output_values = [float(val) for val in output.decode().strip().split(",")]
# Average all values
output_avg = sum(output_values) / len(output_values)
Now You have clean communication with Device and Your script can handle possible communication errors.

Sending Serial Data to Arduino Using Python Script

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?
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.
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()
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.

Access denied when trying to access serial port for the second time in Cygwin

I am trying to get data from a USB serial port that is connected to an Arduino. I am using Cygwin and I write
cat /dev/ttyS4
to output the data in the shell.
When I stop the process, I am given
Access Denied
when I try to access it again. I have to close Cygwin, open it again and type in the same
command to get the output to the shell.
I have noticed that I am able to read the serial port from only one program. For example, if I read the data from the serial port in the Arduino Software, I can't access it in Cygwin.
Is there a way I can access the serial port data as many times as I want in Cygwin without having to have to close the program, open it again and write in the same command?
It appears that the statement cat /dev/ttyS4 would echo characters from the serial port until the end of the file is reached. Only, by nature, a serial port never reaches the end. So, you would need to arrange for the input to "end". One way would be to have the Arduino put an end of file character (control-D) into the output stream. The other way would be to use the so-called "heredoc" by which you tell it to look for a string to end on, as detailed in this question.
There are still a number of problems with this, though. One is, it seems wrong that control-C wouldn't close the access to the serial port. The other is, I tried this on my machine, and I can't get it to produce the problem you asked about. So, that's as much as I can offer.

OS X Vs Linux - Serial port handling

I am trying to port (or rather customize) a pure Linux application to OS X Snow Leopard (10.6.4). It is an application that sends a binary to a target hardware over the serial port. The app is almost running but i am hit with an interesting problem with the serial port writes.
With all the same settings as Linux (115.2k is the baudrate) OS X serial data send seems to be some 10 times or more slower compared to Linux. What takes 3 secs in Linux , takes 30-40 secs and by that time the target firmware at the receiving end times out :).
Digging in to the serial port write function, i see that it is using select() system call to find if the device (or rather the file descriptor) is ready to write data to. Each write system call writes 1024 bytes of data in OS X and 1087 bytes of data in Linux (Thats what the return value of write is). My data size is some 50KB for a first level binary (it is a small bootloader which can load a bigger binary in the next level).
Pseudo code
select() configuration with 1s time out and observing the serial port file descriptor for write ready.
while(true)
{
rc=select(...)
if(rc>0){write(...) and other logic to get out of while if done}
if(rc==0){//try again}
if(rc<0){//error}
}
I observed that in linux, writes happen all the time one after another . A sequence of writes and it comes out of the function in a jiffy. But, in OS X, it is like 3 writes and then select returns zero twice (2 seconds gone) again a few writes and select time out etc etc making the function a lot slower.
Any clues?
Notes:
The app is using termios lib API to control the serial port.
I could fix this by changing the prolific chip device driver. By default it was using a non-standard open source driver, I downloaded the OS X driver from the prolific website and it works ok.
Thanks to Nils and others for the support!

Resources