无法通过串行端口将数据从 Matlab 发送到 Python
Trouble sending data from Matlab to Python via serial port
为了更好地理解串口通信,我正在尝试编写一些示例代码,其中 Matlab 有一个循环 运行,它不断地将数据发送到串口,而 Python 脚本 运行 在同一台 windows 机器上侦听此端口,然后接收并打印任何接收到的数据。
在Matlab中,我写了一个简单的循环,将99个测试信号发送到端口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);
据我所知,这个 Matlab 部分有效,因为它每暂停一秒打印 "run"。
Python 听众:
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)
运行 Python 中的脚本导致以下错误:
serial.serialutil.SerialException: could not open port 'COM1': PermissionError(13, 'Zugriff verweigert', None, 5)
显然该端口已被 Matlab 使用,因为它向它发送信息,但我不明白,为什么这是个问题。一个程序向它发送数据而另一个程序从它接收数据不是很好吗?
亲切的问候。
恐怕你不能从两个不同的进程连接到同一个串口。
这意味着,如果您从 Matlab 打开串行端口进行写入,则无法从 Python 打开它进行读取。
串行端口旨在将数据从计算机发送到另一台计算机或设备,而不是在不同应用程序之间共享数据(要做到这一点,有更好的方法,如写入文件或共享内存块)。
话虽如此,如果您尝试做的是为了调试目的或只是为了学习,您可能想研究一下 com0com,它允许您创建一对 virtual(软件)串口。这与将几个硬件(实际)串行端口相互连接是一样的。由于现在您有两个端口,您可以在一个端口上从 Matlab 发送数据,在另一个端口上从 Python 读取数据。
这是关于串行端口的最常见问题之一,因此您应该能够找到很多好的资源。您可能想要开始 .
为了更好地理解串口通信,我正在尝试编写一些示例代码,其中 Matlab 有一个循环 运行,它不断地将数据发送到串口,而 Python 脚本 运行 在同一台 windows 机器上侦听此端口,然后接收并打印任何接收到的数据。
在Matlab中,我写了一个简单的循环,将99个测试信号发送到端口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);
据我所知,这个 Matlab 部分有效,因为它每暂停一秒打印 "run"。
Python 听众:
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)
运行 Python 中的脚本导致以下错误:
serial.serialutil.SerialException: could not open port 'COM1': PermissionError(13, 'Zugriff verweigert', None, 5)
显然该端口已被 Matlab 使用,因为它向它发送信息,但我不明白,为什么这是个问题。一个程序向它发送数据而另一个程序从它接收数据不是很好吗?
亲切的问候。
恐怕你不能从两个不同的进程连接到同一个串口。
这意味着,如果您从 Matlab 打开串行端口进行写入,则无法从 Python 打开它进行读取。
串行端口旨在将数据从计算机发送到另一台计算机或设备,而不是在不同应用程序之间共享数据(要做到这一点,有更好的方法,如写入文件或共享内存块)。
话虽如此,如果您尝试做的是为了调试目的或只是为了学习,您可能想研究一下 com0com,它允许您创建一对 virtual(软件)串口。这与将几个硬件(实际)串行端口相互连接是一样的。由于现在您有两个端口,您可以在一个端口上从 Matlab 发送数据,在另一个端口上从 Python 读取数据。
这是关于串行端口的最常见问题之一,因此您应该能够找到很多好的资源。您可能想要开始