When using Simlink real-time (MATLAB 2020a), the following error occurs. Does anyone know the solution?

29 次查看(过去 30 天)
When using Simlink real-time (MATLAB 2020a), the following error occurs. Does anyone know the solution?
Warning: An error occurred while executing the listener callback for the event ConnectCompleted defined for class MathWorks.xPCTarget. FrameWork.xPCTarget PC:
Incorrect use of SimulinkRealTime.target/getRunDirectory
Message: Error opening file
Source: HostCommandObjects

回答(1 个)

Umar
Umar 2025-9-23,14:31

Hi @子谦,

I saw your post about the Simulink Real-Time error you're experiencing with MATLAB 2020a. The error message you're getting with SimulinkRealTime.target/getRunDirectory and the ConnectCompleted callback failure is a known issue related to host-target communication problems.

What Your Error Indicates:

Your error suggests that MATLAB is having trouble establishing proper communication with your target computer during the connection process. The getRunDirectory function is failing to access the target's file system, which prevents the connection from completing successfully.

Immediate Steps to Try:

1. Network Connection Check * Verify your target computer's IP address and network settings * Ensure stable Ethernet connection between host and target PCs * Test basic network connectivity (ping the target IP)

2. Run Diagnostic Tests * Execute xpctest in MATLAB to identify specific communication failures * Check which test fails to pinpoint the exact issue

3. Clean Installation Approach * MATLAB 2020a sometimes has residual files from previous installations that cause this error * Try running MATLAB as administrator * Refresh the "Simulink Real-Time Target Support Package"

4. File Path Issues * Move your Simulink model to a short, local directory path * Ensure you have full write access to the project directory * Avoid long file paths or special characters

Version-Specific Considerations:

Since you're using MATLAB 2020a specifically, there might be compatibility issues if your target computer software is from a different version. You may need to use the Target Migration Tool if there's a version mismatch.

If These Don't Work: 1. Contact MathWorks technical support directly - they have access to specific bug reports for 2020a 2. Consider temporarily testing with a newer MATLAB version to isolate if it's version-specific 3. Check your firewall/antivirus settings that might block MATLAB-target communication

Additional Resources: * MathWorks documentation on Simulink Real-Time connectivity troubleshooting * xPC Target migration guides for version transitions I hope this helps resolve your issue. If you try these steps and still encounter problems, feel free to share the results of xpctest and any additional error messages.

P.S. - If you find a solution that works, please consider sharing it on the forum to help others who might face the same issue.

  4 个评论
子谦
子谦 2025-9-25,5:35
slrttest
### Simulink Real-Time Test Suite
### Host-Target interface is: TcpIp
### Test 1, Ping target computer 'TargetPC1' using system ping: ... OK
### Test 2, Ping target computer 'TargetPC1' using SLRTPINGTARGET: ... OK
### Test 3, Software reboot the target computer 'TargetPC1': ..................... OK
### Test 4, Build and download a Simulink Real-Time application using model slrttestmdl to target computer 'TargetPC1': ...警告: 执行为类 MathWorks.xPCTarget.FrameWork.xPCTargetPC 定义的事件 Connected 的侦听程序回调时出现错误:
错误使用 SimulinkRealTime.target/getRunDirectory
消息: Error opening file
来源: HostCommandObjects
帮助链接:
> In xpctarget.AutoConnect
In SimulinkRealTime.target/load
In slrttest (line 212)
FAILED: TargetPC1: Error writing file
Umar
Umar 2025-9-25,5:53

Hi @子谦,

Thank you for running the slrttest diagnostic and providing the detailed output. This gives us very specific information about what's happening with your Simulink Real-Time setup.

Analysis of Your Test Results

Looking at your test output chronologically:

Tests 1-3: All Successful * Network connectivity (ping tests) - working perfectly * Target PC reboot capability - functioning normally * Basic host-target communication - established and stable

Test 4: Failed at File Operations

The failure occurs specifically during the build and download phase with these key error messages:

  • Error opening file from getRunDirectory
  • Error writing file during the target load process

This confirms that your network setup and target PC communication are working correctly, but there's a file system issue on the target PC side.

Root Cause Analysis

Based on the error sequence in your diagnostic:

1. The warning about MathWorks.xPCTarget.FrameWork.xPCTargetPC event listener callback

2. The specific getRunDirectory error from HostCommandObjects

3. The final "Error writing file" message

This indicates the target PC cannot create or access its run directory where Simulink Real-Time stores downloaded models and temporary files.

Targeted Solutions (In Order of Likelihood)

1. Target PC Directory Access Issues (Most Likely) The getRunDirectory error suggests the target can't access its default run directory. Try:

% First, check what directory the target is trying to use
tg = slrealtime('TargetPC1');
try
  runDir = tg.getRunDirectory();
  fprintf('Target run directory: %s\n', runDir);
catch ME
  fprintf('Cannot get run directory: %s\n', ME.message);
end

2. Target PC File System Permissions

On your target PC (TargetPC1): * Navigate to C:\ and look for SimulinkRealTime or xPCTarget folders * Right-click → Properties → Security → ensure "Full Control" for current user * If folders don't exist, create: C:\SimulinkRealTime\temp with full permissions

3. Target PC Disk Space Check

Verify available space on target PC's C: drive: * Need at least 1-2 GB free for model compilation and temporary files * Clear any old model files from previous sessions

4. MATLAB 2020a Specific Fix

Since you're using MATLAB 2020a, try this sequence:

% Clear any cached target connections
clear tg
clear all
% Recreate target object with explicit configuration
tg = slrealtime('TargetPC1');
tg.connect();
% Try a simple ping before building
if tg.isConnected()
  fprintf('Connection established\n');
else
  fprintf('Connection failed\n');
end

5. Target Software Refresh

If the above don't work, the target runtime may need refreshing: 1. On target PC, restart the Simulink Real-Time services 2. Or consider reinstalling the Simulink Real-Time target software

Next Steps

Please try solutions 1 and 2 first, as they address the most common causes of the getRunDirectory error.

Can you also run this diagnostic command and share the output?

tg = slrealtime('TargetPC1');
tg.connect();
status = tg.TargetSettings

This will show us the target's current configuration and might reveal additional clues about the directory access issue.

Let me know what you find, and we can drill down further based on the results.

Note: The fact that tests 1-3 passed completely rules out network issues, so we can focus specifically on the file system problem on the target PC.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by