- Create Project and Libraries Folder
- Write Hardware Specific C/C++ Code
- Select System Object Template
- Specify Initialization, Output, and Termination Behavior
- Update Paths for Source and Header Files
- Set Output Port Properties Create
- MATLAB System Block
- Generate Code and Deploy Model to Hardware
Can I create a new serial port on the Arduino Uno to run a Simulink model in real time that also uses a serial port?
14 views (last 30 days)
Show older comments
Gavin Trevorrow
on 12 Jun 2024
Commented: Gavin Trevorrow
on 13 Jun 2024
I'm attempting to run the example MODBUS RS485 Communication Between Client and Server Using Arduino Board with two Arduino Uno units. These units have only one dedicated serial port on pins 0 and 1. When I go to Monitor and Tune the Client Model, I get an error message for all 6 MODBUS blocks in the model that reads:
Error evaluating 'InitFcn' callback of codertarget.arduinobase.internal.arduino_ModbusRS485MasterRead block (mask) 'arduino_modbus_client/Modbus RS485 Client Read'.
Callback string is 'codertarget.arduinobase.conflictcheck.validateandregisterBlocks(gcbh);'
Caused by:
The SerialPin_1 '1' being used by 'arduino_modbus_client/Modbus RS485 Client Read' is currently in use by 'Monitor and tune over XCP on Serial'. To resolve the conflict, change the value of SerialPin_1 in 'arduino_modbus_client/Modbus RS485 Client Read' to an alternate valid value.
It appears that the only port the Arduino Uno has is in use by the USB interface with my desktop computer. I know that ArduinoIDE has the SoftwareSerial library addon that is supposed to allow you to assign pins to create a separate serial port, but I don't know if a similar operation is possible in Simulink. Or perhaps I can create the new port in ArduinoIDE for use in the Simulink model. Thank you.
0 Comments
Accepted Answer
surya venu
on 13 Jun 2024
Hi,
Your issue arises because the Arduino Uno's primary hardware serial port (Serial) is being used for both the USB communication with your computer and for MODBUS RS485 communication in your project. This creates a conflict, especially when you're trying to use the same serial port for monitoring and tuning your Simulink model via the USB connection.
Simulink models for Arduino typically use the hardware serial port for deploying and debugging the model, which is why you're encountering the error. To work around this, you're correct in thinking about "SoftwareSerial", a library that allows you to create a software-based serial port on almost any digital pins of the Arduino.
Unfortunately, Simulink does not directly support "SoftwareSerial" within its block set for Arduino. However, you can integrate custom code, including "SoftwareSerial", into your Simulink model. This involves writing some parts of your model's logic in C++ and ensuring it interacts correctly with the rest of your Simulink model.
Here is how to create to create a custom device driver for Arduino Library: https://www.mathworks.com/help/simulink/supportpkg/arduino_ug/create-custom-blocks-for-arduino-support-package.html
The above link contains information about:
Hope it helps.
More Answers (0)
See Also
Categories
Find more on Modeling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!