最大化Arduino串行输出(Maximize Arduino serial output)

编程入门 行业动态 更新时间:2024-10-28 12:16:17
最大化Arduino串行输出(Maximize Arduino serial output)

这篇文章中散布着多个问题。 请仔细阅读,并回答你可以为upvote的作品。

用例

创建压力读数的时间序列csv文件。 这些读取需要具有最大频率,但我只需要它持续不到10秒左右。

材料

Arduino Uno Clone(不可改变) 串口USB 2.0(可变) pyserial(Python 3) SSD

问题

识别并修复瓶颈,防止最大读数的频率。

代码

Arduino的

void setup() { //speed over the usb serial interface Serial.begin(450000); } void loop() { //get sensor data: read the input on analog pin 0: int sensorValue = analogRead(A0); //returns value 0-1023 //send over serial Serial.println(sensorValue); }

蟒蛇

... ser.serial() ser.baudrate=450000 ser.open() while True: try: serialData = float(ser.readline().decode().replace('\n', '')) except(ValueError): continue #I ran the code without this try/except and got 100 reads higher than when this is included in here. Caused no change in order of magnitude. now = datetime.datetime.now() serialData2 = serialData * voltageOffset #a couple of these happen outfile.write(now+', '+str(serialData2)+'\n') except(KeyboardInterrupt): ... os.sys.exit(0)

Arduino

void setup() { //speed over the usb serial interface Serial.begin(450000); } void loop() { //get sensor data: read the input on analog pin 0: int sensorValue = analogRead(A0); //returns value 0-1023 //send over serial Serial.println(sensorValue); }

Python

... ser.serial() ser.baudrate=450000 ser.open() while True: try: serialData = float(ser.readline().decode().replace('\n', '')) except(ValueError): continue #I ran the code without this try/except and got 100 reads higher than when this is included in here. Caused no change in order of magnitude. now = datetime.datetime.now() serialData2 = serialData * voltageOffset #a couple of these happen outfile.write(now+', '+str(serialData2)+'\n') except(KeyboardInterrupt): ... os.sys.exit(0)

Bandwidths

Some calculations to figure out where the bottleneck resides.

100 µs to read analog pin

1 packet = 10 bits

serial wire speed: 450000/10 = 45000 packets / second

Arduino samples-into-serial/second rate:

100 µs * 1s/(10^-6 µs) = 1 x 10^4 samples / second

arduino silicon adapter: cheap cloned chip. Assumed to be underperforming as it is not an arduino-licensed product, but this is deemed acceptable loss as will be explained later. Assumed to have unlimited bandwidth, though this obviously will increase performance by an order of magnitude.

USB 2.0

packets/s = 480 mb/s * 10^6 b/mb * 1 packet/10 b = 48 x 10^6 packets / second

PC file write speed

400 MB/s = 320 x 10^6 packets/s

PC python script

Unknown. Presumed to be infinite? the datetime.now() query is known to take ~ 8 µs.

Conclusion: The bottleneck is the serial baud rate.

Adjusting the baud rate confirms this. I'm finding that el-cheapo arduino uno only supports a maximum 450000 baud rate, when the arduino actually supports 2-7 million from what I can gather online. That being said, something is still wrong.

The linchpin question

Recall that at this baud rate, we should be seeing pressure reads generated at a theoretical 45000 packets / second.

baudrate-data length-no parity-1 end bit present: 450000-8-n-1

Let sensorValue = 1023. Data looks like this going into println: 1023\n

When I run serial.println(sensorValue) in the arduino, how many packets are sent over the wire to the PC? How is this serialized into packets? How many packets will be sent over the wire? Which one of the following is true:

A. 16 bit int requires 2 packets to be transmitted. The \n exacts 2 more. Total = 4

B. sensorValue is converted to a string and sent over the wire with separate packets: 1 for 1, 1 for 0, 1 for 2, 1 for 3, 1 for \, and 1 for n. Total = 6

C. Other (help me optimize??)

Tests

I'm not seeing results as I would expect.

Let baud = 450000.

Changing value in the arduino code serial.println(value) produces different reads/second in my txt file:

value = readPin\n >> 5417 pressure reads/s

value = 1 >> 10201 reads/s

value = 1023 >> 4279 reads/s

This tells me a couple things. There is overhead when reading the analogPin's value. I can also conclude the arduino also only sends over multiple packets of data when it has to (1 > 8 bit of data, 1023 > 16 bit of data). Wait, what? That doesn't make very much sense. How does that happen?

Adjusting the baud rate causes the number of reads to change until the arduino maxes out. But lets find the throughput I should be expecting.

450000 b/s * (1 packet / 10b) * [1 read / (4 or 6 packets)] = 11250 reads OR 7500 theoretical reads/s

Actual = 5417 reads/s

Where did 2,000 reads disappear to?

最满意答案

你是对的,瓶颈是串口(从某种意义上说,你无法有效地传输数据)。 但是,您的大多数其他假设都是错误的。

波特率代表什么?

波特率是每秒不同符号变化的数量。 这里它等效于每秒位数。 波特率将包括所有传输的比特,而不仅仅是数据(开始,停止,奇偶校验)。

如何传输10位值

由于您有8n1传输,因此无法准确发送10位数据。 它必须是8的倍数,因此8位,16位,24位等将在两个8位部分中发送,就像它们作为int存储在Arduino存储器中一样。

println()如何工作

println()将数字转换为字符串。 您可以为此转换指定基数(DEC,BIN,HEX,OCT) - 默认为DEC,因此1023将作为4字节+ \n传输,即单字节(ASCII 10)和\r (ASCII 13),即也是一个字节。 总共6个字节。 1将需要3个字节 - 1表示数据,2表示换行和回车。

如何让它更快

几乎不改变任何东西 - 使用println(val, HEX) - 对于大于255的数字,最多需要5个字节。 使用Serial.write() - 此函数将原始二进制数据放入串行中,因此如果要发送值10,它将只发送一个字节值10.但是,当您要发送10位变量时,事情会得到复杂 - 你需要2个字节,PC需要知道哪个是第一个部分,哪个是第二个将它拼接在一起。 您需要提出某种形式的简单传输协议来处理这个问题,所以可能需要一些启动/停止字符。 你需要10位分辨率吗? 如果您可以继续使用8位,则可以传输纯数据,而无需任何其他字符。 此外,正如评论中所建议的那样,您可以使用更快的ADC时钟。 KIIV也建议你 - 你可以使用ADC转换中断,它会在测量完成时立即触发。

如果你仍然使用10位,我会建议3字节帧:1个起始字节和2个字节的数据。 这样理论上你可以达到每秒约18 000帧,这高于最大analogRead()频率。

You are right that the bottleneck is the serial port (in the sense that you transmit data inefficiently). However, most of your other assumptions are wrong.

What does baud rate stand for

Baud rate is the number of distinct symbol changes per second. Here it would be equivalent for bits per second. The baud rate would include all bits transmitted, not only data (start, stop, parity).

How 10-bit value is transmitted

As you have 8n1 transmission, you cannot send exactly 10 bits of data. It must be a multiple of 8, so 8, 16, 24, etc. 10 bits will be sent in two 8 bit parts, just like they are stored in Arduino memory as an int.

How does println() work

println() converts numbers to string. You can specify base for this conversion (DEC, BIN, HEX, OCT) - default is DEC, so 1023 will be transmitted as 4 bytes + \n which is a single byte (ASCII 10) and \r (ASCII 13) which is also a single byte. Total of 6 bytes. 1 will require 3 bytes - 1 for data and 2 for newline and carriage return.

How to make it faster

Without changing almost anything - use println(val, HEX) - you will need 5 bytes maximum for numbers greater than 255. Using Serial.write() - this function puts raw binary data into serial, so if you want to send a value 10, it will send just one byte of value 10. However, when you want to send 10-bit variable, things get complicated - you need 2 bytes for that, and PC needs to know which is the first part and which is second to stitch it back together. You will need to come up with some form of simple transmission protocol to handle this, so probably some start/stop character. Do you need 10-bits resolution? If you could get on with 8-bits you could transmit just plain data, without any additional characters. Also, as suggested in the comments, you could use faster ADC clock then. Also suggested by KIIV - you could use ADC conversion interrupt that would trigger instantly when the measurement is completed.

If you still go with 10-bit, I would suggest 3-byte frame: 1 start byte, and 2 bytes of data. This way you could theoretically reach ~18 000 frames per second, which is above the maximum analogRead() frequency.

更多推荐

本文发布于:2023-04-28 07:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1331372.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Maximize   Arduino   output   serial

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!