当前位置:网站首页>Simulink与Arduino串口通信
Simulink与Arduino串口通信
2022-07-04 04:38:00 【lihe2021】
本文想实现的目的为:Simulink与Arduino串口通信,进而获取与Arduino连接的压力传感器FSR402的数据。
Simulink与Arduino交互的方式有两种(http://www.51hei.com/arduino/3989.html):
(1)Simulink Arduino Support package
(2)Simulink串口通信
由于已经给Arduino编好了读取FSR402压力值的程序(链接:https://pan.baidu.com/s/1yadaWsIm-4wXirOJWflCLA
提取码:llhh),又希望在Simulink中调用串口,读取数据。此时 Simulink Arduino Support package显然不合适了。这里我们要用到Simulink里的串口模块Serial Receive,从对Serial Receive模块中可以看出其接收的为二进制数据。
在具体实现之前,需要对串口通信的方式进行说明。串口通信一般有两种方式: 1、字符(对应文本方式) 2、字节(对应二进制方式)
| 字符 | 字节 |
|---|---|
| fscanf fprint (Matlab) | fread fwrite (Matlab) |
| Serial. print (Arduino) | Serial.write (Arduino) |
通俗来讲,字符方式针对的是PC端显示,字节方式更多用于与串口设备通信。
所以在Arduino程序中我们可以发现,采用的是print函数对数据进行输出,进而可以在串口监视器查看输出。但是Serial Receive接收的为二进制数据,所以需要利用write函数进行一个字节一个字节的输出,进而将压力值写到Arduino所对应的串口,从而Simulink通过串口接收模块进行读取。
Arduino发送
Arduino中利用union来发送float类型数据。代码段如下
定义union SeFrame
union SeFrame
{
long Long;
byte Byte[4];
};
SeFrame Sefram;
发送数据代码段:
void Send_long(long LONG)
{
Sefram.Long= LONG;
Serial.write(Sefram.Byte[0]);
Serial.write(Sefram.Byte[1]);
Serial.write(Sefram.Byte[2]);
Serial.write(Sefram.Byte[3]);
}
simulink接收设置
Serial Receive模块中:
data type设置为int32,每个数据占用4个字节
data size 设置为1,因为我们要发送的数据只有一个。
最终效果如下:
边栏推荐
- 【MATLAB】通信信号调制通用函数 — 傅里叶逆变换
- 简单g++和gdb调试
- 【MATLAB】MATLAB 仿真模拟调制系统 — FM 系统
- [technology development -25]: integration technology of radio and television network, Internet, telecommunication network and power grid
- cmake
- 附件二:攻防演练保密协议.docx
- EVM proof in appliedzkp zkevm (11)
- Create ASM disk through DD
- 【MATLAB】MATLAB 仿真模拟调制系统 — SSB 系统
- 全国职业院校技能大赛(中职组)网络安全竞赛试题—解析
猜你喜欢

2022年6月总结

Deep understanding of redis -- bloomfilter

Useful plug-ins for vscode

Simulated small root pile

中科磐云—模块A 基础设施设置与安全加固 评分标准

Utiliser des unités de mesure dans votre code pour une vie meilleure

2022G2电站锅炉司炉特种作业证考试题库及答案

Flutter 调用高德地图APP实现位置搜索、路线规划、逆地理编码

Zhongke panyun-2022 Guangdong Trojan horse information acquisition and analysis
![[QT] timer](/img/df/5db6af851ef19f33fd7e7a7ed46586.png)
[QT] timer
随机推荐
Technology Management - learning / practice
Error response from daemon: You cannot remove a running container 8d6f0d2850250627cd6c2acb2497002fc3
Create ASM disk through DD
Yolov6 practice: teach you to use yolov6 for object detection (with data set)
[matlab] communication signal modulation general function - low pass filter
在代码中使用度量单位,从而生活更美好
KMP匹配字符串
Flutter 调用高德地图APP实现位置搜索、路线规划、逆地理编码
[matlab] communication signal modulation general function interpolation function
Electronic components mall and data manual download website summary
中職組網絡安全—內存取證
我们认为消费互联网发展到最后,依然会局限于互联网行业本身
[matlab] general function of communication signal modulation bandpass filter
VSCode的有用插件
We believe that the development of consumer Internet will still be limited to the Internet industry itself
[matlab] matlab simulation - simulate the AM modulation process of the modulation system
中科磐云—数据分析与取证数据包flag
Simulated small root pile
Zhongke Panyun - module a infrastructure setting and safety reinforcement scoring standard
中职组网络安全—内存取证