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

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?

 Accepted Answer

I have used Instrument Control Toolbox for serial communication and it worked.

More Answers (0)

Categories

Products

Release

R2022a

Asked:

on 27 Aug 2024

Answered:

on 30 Aug 2024

Community Treasure Hunt

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

Start Hunting!