Answers:
没有顶层,易于使用的方式。抱歉。设置串行端口时,将所选的波特率存储在一个变量中可能会更容易。
无论如何,对于基于AVR的硬件UART,您可以尝试做的是撤消Serial.begin设置特定波特率的操作,但这有点麻烦。这是用于设置波特率的代码:
  // Try u2x mode first
  uint16_t baud_setting = (F_CPU / 4 / baud - 1) / 2;
  *_ucsra = 1 << U2X0;
  // hardcoded exception for 57600 for compatibility with the bootloader
  // shipped with the Duemilanove and previous boards and the firmware
  // on the 8U2 on the Uno and Mega 2560. Also, The baud_setting cannot
  // be > 4095, so switch back to non-u2x mode if the baud rate is too
  // low.
  if (((F_CPU == 16000000UL) && (baud == 57600)) || (baud_setting >4095))
  {
    *_ucsra = 0;
    baud_setting = (F_CPU / 8 / baud - 1) / 2;
  }
  // assign the baud_setting, a.k.a. ubrr (USART Baud Rate Register)
  *_ubrrh = baud_setting >> 8;
  *_ubrrl = baud_setting;
您可以通过读取正确的UCSRA,UBRRH和UBRRL寄存器找到结果。在uno上,这些是正确的寄存器名称,在Mega上,其UCSR0A,UBRR0H,UBRR0L(用于Serial),UCRS1A ...用于serial1,依此类推。非Avr板(和SerialLeonardo)将完全不同。
每个波特率下,特定板卡(和该板卡的频率)在AVR硬件串行端口上的这三个寄存器将处于单一状态。您可以尝试建立一个方程式来获得原始的波特率,但是我建议您只将直接值与某种形式的查找进行比较,因为整数算术舍入误差会使其变得一团糟。
例如,在我的大型UBBR0H,UBBR0L和UCSR0A上,9600波特为0、207、2,但38400波特为0、51、2,而57600波特为0、16、0。