串行通信 PySerial 我得到奇怪的字符(可能是 ASCII?)我无法解码它
Serial Communication PySerial i get weird Characters (Probably ASCII?) I'm not able to decode it
我正在尝试在 Arduino 和 Python 之间传输串行数据。我得到了这个字符列表,但我无法将它们转换为整数。我没有通过解码得到它 运行:它在我发送 0 - 255 也就是单字节的值时有效...
我正在使用 Python 3.7
y.encode('utf-8').strip()
Python代码
import serial
import numpy as np
barr = [4]
ser = serial.Serial(
port='COM12',\
baudrate=56000,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=5)
print("connected to: " + ser.portstr)
count=0
while True:
y = ser.readline(512)
print(y)
Python 输出:
b'\x9a1\x9aX\x9a\x7f\x9a\xa6\x9c\xce\x9a\xf5\x9a\x1c\x9aC\x9cj\x9a\x91\x9a\xb8\x9a\xdf\x9c\x06\x9a-\x9cT\x9a{\x9c\xa2\x9c\xc9\x9a\xf0\x9a\x17\x9c>\x9af\x9a\x8d\x9a\xb4\x9a\xdb\x9a\x02\ and it goes one....
Serial.write 部分 Arduino 代码:
for (unsigned short int nIndexStep = 0; nIndexStep<g_objRF.getConfiguration()->getFreqSpectrumSteps(); nIndexStep++)
{
//Print every step of sweep data onto display
int stepFreq = g_objRF.getSweepData()->getFrequencyKHZ(nIndexStep);
int stepAmp = g_objRF.getSweepData()->getAmplitudeDBM(nIndexStep);
delay(serialdelay);
Serial.write(stepFreq);
delay(serialdelay);
Serial.write(stepAmp);
if (stepAmp <= (TopLevel -90))
{
stepAmp = TopLevel -90;
}
if (stepAmp > TopLevel)
{
stepAmp = TopLevel;
}
//Serial.print(stepAmp);
//Serial.print(", ");
comp2 = stepAmp;
comp1 = max(comp1, comp2);
{
}
}
}
Serial.write("\n");
使用Serial.write您可以发送字节、字符串或字节数组。您不能发送整数。
如果要发送整数,则必须先将其转换为字节数组。
有无数种方法可以做到这一点。位掩码、cast 指针、联合...只需搜索网络并选择您最喜欢的。
见https://www.arduino.cc/reference/en/language/functions/communication/serial/write/
我正在尝试在 Arduino 和 Python 之间传输串行数据。我得到了这个字符列表,但我无法将它们转换为整数。我没有通过解码得到它 运行:它在我发送 0 - 255 也就是单字节的值时有效...
我正在使用 Python 3.7
y.encode('utf-8').strip()
Python代码
import serial
import numpy as np
barr = [4]
ser = serial.Serial(
port='COM12',\
baudrate=56000,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=5)
print("connected to: " + ser.portstr)
count=0
while True:
y = ser.readline(512)
print(y)
Python 输出:
b'\x9a1\x9aX\x9a\x7f\x9a\xa6\x9c\xce\x9a\xf5\x9a\x1c\x9aC\x9cj\x9a\x91\x9a\xb8\x9a\xdf\x9c\x06\x9a-\x9cT\x9a{\x9c\xa2\x9c\xc9\x9a\xf0\x9a\x17\x9c>\x9af\x9a\x8d\x9a\xb4\x9a\xdb\x9a\x02\ and it goes one....
Serial.write 部分 Arduino 代码:
for (unsigned short int nIndexStep = 0; nIndexStep<g_objRF.getConfiguration()->getFreqSpectrumSteps(); nIndexStep++)
{
//Print every step of sweep data onto display
int stepFreq = g_objRF.getSweepData()->getFrequencyKHZ(nIndexStep);
int stepAmp = g_objRF.getSweepData()->getAmplitudeDBM(nIndexStep);
delay(serialdelay);
Serial.write(stepFreq);
delay(serialdelay);
Serial.write(stepAmp);
if (stepAmp <= (TopLevel -90))
{
stepAmp = TopLevel -90;
}
if (stepAmp > TopLevel)
{
stepAmp = TopLevel;
}
//Serial.print(stepAmp);
//Serial.print(", ");
comp2 = stepAmp;
comp1 = max(comp1, comp2);
{
}
}
}
Serial.write("\n");
使用Serial.write您可以发送字节、字符串或字节数组。您不能发送整数。
如果要发送整数,则必须先将其转换为字节数组。
有无数种方法可以做到这一点。位掩码、cast 指针、联合...只需搜索网络并选择您最喜欢的。
见https://www.arduino.cc/reference/en/language/functions/communication/serial/write/