Issue in Reading serial data from the Arduino MEGA in Simulink using Serial Receive block

4 Ansichten (letzte 30 Tage)
I am trying to read serial data from an Arduino Mega 2560 using Simulink.
My setup is as follows:
Arduino Code:
I am sending the sensor value over the serial ports (Serial and Serial1) every 20 ms using the following code:
unsigned long previousTime = 0; // Initialize previousTime outside the loop
void setup() {
Serial.begin(115200); // Begin Serial communication (COM14)
Serial1.begin(115200); // Begin Serial1 communication
}
void loop() {
int sensorValue = 20; // Example sensor value
unsigned long currentTime = millis(); // Get the current time in milliseconds
if (currentTime - previousTime > 20) { // Check if 20 ms have passed
Serial.println(sensorValue); // Print to Serial (USB connection)
Serial1.println(sensorValue); // Print to Serial1 (TX1/RX1 on Mega)
previousTime = currentTime; // Update previousTime to the current time
}
}
Simulink Setup:
  • I am using the Serial Receive block to capture the data sent from the Arduino.
  • The block is configured to read from Serial1 (Port 1).
  • The Host-board connection and Connected I/O are both set to Serial 0 with the same baud rate (115200).
  • The Arduino board's TX and RX LEDs are blinking, indicating communication.
Problem:
Despite this setup, the data I receive in Simulink is consistently zero instead of the expected value of 20. I have double-checked that the baud rates and serial ports are correctly configured, and the Simulink model runs without any errors.
Has anyone encountered a similar issue or can provide guidance on how to properly receive the data in Simulink?

Akzeptierte Antwort

Danaish
Danaish am 30 Aug. 2024
I have used Instrument Control Toolbox for serial communication and it worked.

Weitere Antworten (1)

Harry
Harry am 27 Aug. 2024
As a fellow student working with Arduino and Simulink, I can understand the frustration of trying to get serial communication working correctly. It seems like you've done most of the setup correctly, but there might be some issues with the configuration of the Serial Receive block in Simulink or the way the data is being handled. You might want to try verifying the data type and formatting being received in Simulink to ensure it's correctly interpreting the incoming serial data. If you're looking for more detailed support, you could benefit from Simulink assignment help.I highly recommend reaching out to the experts at MATLAB Assignment Experts. They offer professional help with MATLAB and Simulink projects. You can contact them via WhatsApp at +1 (315) 557-6473. You can also email them at info@matlabassignmentexperts.com for more personalized assistance. They might be able to provide deeper insights into the issue you're facing!

Produkte


Version

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by