Replies: 6 comments
-
PA4 is configured output so use digitalToggle api. Finally, there is only one USART instance USART1. So as you defined 2 Hardware Serial on the same USART only the last one instantiate works. So remove SerialMy2 and use only SerialMy1. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the explanation, how can I operate with both serial (PA2/3 and PA9/10) ports if there is only one instance? Or is there no way to operate two serial ports at the same time with Arduino IDE? The goal is to make an adapter, I have a camera that operates at 9600 baud and a pan-tilt support that operates at 2400 baud, so I need two hardware serial ports. With this code the LED doesn't blink, maybe I should have understood how to make both ports work, but I still haven't: uint32_t ledcnt1 = 0;
// RX TX
HardwareSerial SerialMy1(PA10, PA9);
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(PA4, OUTPUT);
SerialMy1.begin(9600);
Serial.begin(9600);
}
void loop() {
if ((millis() - ledcnt1) > 100) {
ledcnt1 = millis();
SerialMy1.write(48);
//Serial.write(48);
digitalToggle(PA4);
}
} |
Beta Was this translation helpful? Give feedback.
-
In case anyone needs this, The data leaves the camera at 9600 baud, passes through the Bluepill and arrives at the support at 2400 baud. The Bluepill running at 72MHz is losing data. The Arduino system seems too slow to process the data, which is not much. I bought a stand in China and it wasn't specified that it was 2400 baud in the ad, so I need an adaptation. I managed to make the support move, but it is losing data. Even though it is just sending data (9600 --> 2400), the Pelco D protocol provides for a response from support, so it still needs the other party, from 2400 to 9600 serial ports. uint32_t ledcnt1 = 0;
// RX TX
HardwareSerial SerialMy2(PA3, PA2);
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(PC13, OUTPUT);
Serial.begin(9600);
SerialMy2.begin(2400);
}
void loop() {
}
void serialEvent() {
while (Serial.available()) {
SerialMy2.write(Serial.read());
//digitalWrite(PC13, !digitalRead(PC13));
}
} |
Beta Was this translation helpful? Give feedback.
-
I advise to not use serialEvent as it is not a real handler.... Arduino also think to remove it. |
Beta Was this translation helpful? Give feedback.
-
I found the code generated in the DeepSeek chat interesting:
#include <Arduino.h>
#define BUFFER_SIZE 16
// Circular buffer for Serial1 (9600 baud)
uint8_t buffer1[BUFFER_SIZE];
volatile uint8_t head1 = 0;
volatile uint8_t tail1 = 0;
// Circular buffer for Serial2 (2400 baud)
uint8_t buffer2[BUFFER_SIZE];
volatile uint8_t head2 = 0;
volatile uint8_t tail2 = 0;
void setup() {
// Initialize Serial1 at 9600 baud
Serial1.begin(9600);
// Initialize Serial2 at 2400 baud
Serial2.begin(2400);
}
void loop() {
// Check if data is available on Serial1
if (Serial1.available()) {
// Read data from Serial1
uint8_t data = Serial1.read();
// Store data in buffer1
buffer1[head1] = data;
head1 = (head1 + 1) % BUFFER_SIZE;
// If buffer is full, discard the oldest data (tail)
if (head1 == tail1) {
tail1 = (tail1 + 1) % BUFFER_SIZE;
}
}
// Check if data is available on Serial2
if (Serial2.available()) {
// Read data from Serial2
uint8_t data = Serial2.read();
// Store data in buffer2
buffer2[head2] = data;
head2 = (head2 + 1) % BUFFER_SIZE;
// If buffer is full, discard the oldest data (tail)
if (head2 == tail2) {
tail2 = (tail2 + 1) % BUFFER_SIZE;
}
}
// Send data from buffer1 to Serial2 (9600 -> 2400)
if (tail1 != head1) {
Serial2.write(buffer1[tail1]);
tail1 = (tail1 + 1) % BUFFER_SIZE;
}
// Send data from buffer2 to Serial1 (2400 -> 9600)
if (tail2 != head2) {
Serial1.write(buffer2[tail2]);
tail2 = (tail2 + 1) % BUFFER_SIZE;
}
}
My request:
(I put ESP32 in the chat, due to the greater number of examples than STM32)
|
Beta Was this translation helpful? Give feedback.
-
Hello,
I tried using different means to trigger PA9/PA10 pins as serial port but there seems to be some blocking (The LED does not blink if you try to use the PA9/PA10 pins).
I also noticed that I2C port may be available for these PA9/PA10 pins, in that case how to disable or remap these I2C pins to use PA9/PA10 pins as serial port?
Beta Was this translation helpful? Give feedback.
All reactions