专栏
标签
Modelica_DeviceDrivers仿真出错
一般问题
发布于 2025-07-24 11:50:50
查看 18过去339天

从众创共享中下载的MDD库,使用VC2017编译器,32位,modelica3.2.3,试图与串口通讯时报错:

This log was created by MWSolver at Thu Jul 24 11:43:01 2025.

MWSolver started ...

Error:MDDSerialPort.h: CreateFileA of serial port failed with error: 3

Error:Failed to initialize Solver!

Simulation finished at time = 0 (stopTime = 1)
CPU time for Integration: 0.062s
Number of Time Events: 0
Number of State Events: 0
Number of Step Events: 0
Number of Grid Points: 0
Minimum integration stepsize: 0.002
Maximum integration stepsize: 0.002

仿真失败。初始化失败。

我当前的代码为

model Model7
"Test case to send two integer values using serial interface"
extends Modelica.Icons.Example;
Modelica_DeviceDrivers.Blocks.Communication.SerialPortSend serialSend(
autoBufferSize = true,
baud = Modelica_DeviceDrivers.Utilities.Types.SerialBaudRate.B115200,
Serial_Port = "COM1",
sampleTime = 0.1) annotation(Placement(transformation(
extent = {{-10, -10}, {10, 10}},
rotation = -90,
origin = {-46, -90})));
Modelica_DeviceDrivers.Blocks.Packaging.SerialPackager.Packager packager
annotation(Placement(transformation(extent = {{-58, 42}, {-38, 62}})));
Modelica_DeviceDrivers.Blocks.OperatingSystem.SynchronizeRealtime synchronizeRealtime
annotation(Placement(transformation(origin = {21.6514, 43.0459},
extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Sources.IntegerExpression integerExpression(y = integer(3 * sin(
time) + 3))
annotation(Placement(transformation(extent = {{-100, 0}, {-74, 20}})));
Packaging.SerialPackager.PackUnsignedInteger packInt(width = 10, nu = 1)
annotation(Placement(transformation(extent = {{-56, 0}, {-36, 20}})));
Packaging.SerialPackager.AddString addString(data = stringEx.y, nu = 1)
annotation(Placement(transformation(extent = {{-56, -30}, {-36, -10}})));
Utilities.StringExpression stringEx(y = "An example String\n")
annotation(Placement(transformation(extent = {{-94, -28}, {-66, -10}})));
Packaging.SerialPackager.AddInteger addInteger(nu = 1)
annotation(Placement(transformation(extent = {{-56, -66}, {-36, -46}})));
Modelica.Blocks.Sources.IntegerExpression integerExpression1(
y = integer(3 * sin(
time) + 3))
annotation(Placement(transformation(extent = {{-100, -66}, {-72, -46}})));
equation
connect(integerExpression.y, packInt.u) annotation(Line(
points = {{-72.7, 10}, {-58, 10}},
color = {255, 127, 0}));
connect(packInt.pkgOut[1], addString.pkgIn) annotation(Line(
points = {{-46, -0.8}, {-46, -9.2}}));
connect(packager.pkgOut, packInt.pkgIn) annotation(Line(
points = {{-48, 41.2}, {-48, 31.6}, {-46, 31.6}, {-46, 20.8}}));
connect(addString.pkgOut[1], addInteger.pkgIn) annotation(Line(
points = {{-46, -30.8}, {-46, -45.2}}));
connect(addInteger.pkgOut[1], serialSend.pkgIn) annotation(Line(
points = {{-46, -66.8}, {-46, -79.2}}));
connect(integerExpression1.y, addInteger.u[1]) annotation(Line(
points = {{-70.6, -56}, {-58, -56}},
color = {255, 127, 0}));
annotation(Documentation(info = "

Example for serial port support

Hardware setup

In order to execute the example an appropriate physical connection between the sending and the receiving serial port needs to be established, (e.g., by using a null modem cable between the two serial port interfaces http://en.wikipedia.org/wiki/Null_modem). In fact a minimal mull modem with lines (TxD, Rxd and GND) is sufficient. Next, the SerialPortReceive and SerialPortSend blocks parameters must be updated with the device filenames corresponding to the connected physical serial ports. Now, the example can be executed.

Alternative: Using virtual serial port devices for test purposes

To run the example without serial port hardware, it is possible to resort to virtual serial ports. Possible ways of doing this are described in the following.

On Linux, make sure that socat is installed, e.g., on an Ubuntu machine do

sudo aptitude install socat

Now open a console and create two virtual serial port interfaces using socat:

socat -d -d pty,raw,echo=0 pty,raw,echo=0

The socat program will print the device file names that it created. The output will resemble the following:

2013/11/24 15:20:21 socat[3262] N PTY is /dev/pts/1
2013/11/24 15:20:21 socat[3262] N PTY is /dev/pts/3
2013/11/24 15:20:21 socat[3262] N starting data transfer loop with FDs [3,3] and [5,5]

Use them in the Send and Receive block. E.g., for the output above you would use "/dev/pts/1" in SerialPortReceive and "/dev/pts/3" in SerialPortSend.

You may have also have a look at the discussion about virtual serial port devices on stackoverflowhttp://stackoverflow.com/questions/52187/virtual-serial-port-for-linux.

On Windows, make sure that the null modem emulator com0com is installed.

Start the Setup for com0com and check the device names of the created virtual port pair. E.g. you could type "COM6" in SerialPortReceive and "COM7" in SerialPortSend.

"), experiment(Algorithm = Dassl, InlineIntegrator = false, InlineStepSize = false, NumberOfIntervals = 500, StartTime = 0, StopTime = 1, Tolerance = 0.0001)); end Model7;

是在例程里作了修改而来的,看起来是找不到该串口的路径,但我能够在串口调试助手或arduino IDE中正常打开该串口并通讯,设备管理器中的设备端口号和状态也是正常的,请问该怎么解决

所属专栏:Syslab基础平台
产品信息:Sysplorer系统建模仿真环境
通信系统建模

全部回答 1

发布于 2025-07-24 17:52:27

您好,该问题有4种常见原因,请您参考。
1.串口名称错误:提供的串口设备名称不正确。

  • 正确格式应为:"COMx”(例如COM1、COM2等)。注意:大于9的串口号应写为"\.\COM10”。
  • 错误示例:"COM0"(windows通常从COM1开始)、"com1"(大小写不敏感但通常大写)、"COM"(缺少编号)。
    2.串口号不存在:尝试打开的串口在系统中不存在。
  • 检查设备管理器中可用的串口列表(Ports (COM & LPT))。
    3.串口被占用:其他程序已打开该串口。
  • 关闭其他可能使用该串口的程序(如串口调试助手、终端程序等)
    4.权限问题:用户没有权限访问该串口。
  • 以管理员权限运行程序。
用户
和原帖交流更多问题细节吧,去
我要发帖 我要发帖
资料中心 资料中心
查看更多>
热门帖子 热门帖子
主要贡献者 主要贡献者
过去7天