我尝试在Arduino和Python之间通信串行数据。我得到了此角色列表,但即时通讯无法将其转换为整数。我没有通过解码运行它:当我从0-255 aka单字节发送值时,它可以工作...
我正在使用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....
Arduino代码的Serial.write部分:
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");
答案 0 :(得分:0)
使用Serial.write,您可以发送字节,字符串或字节数组。您不能发送整数。
如果要发送整数,则必须先将其转换为字节数组。
有无数种方法可以做到这一点。位掩码,强制转换指针,并集...只需在网上搜索并选择喜欢的人即可。
请参见https://www.arduino.cc/reference/en/language/functions/communication/serial/write/