当前位置:网站首页>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] matlab simulates digital baseband transmission system eye diagram of bipolar baseband signal (cosine roll off forming pulse)
- Zhongke Panyun - module a infrastructure setting and safety reinforcement scoring standard
- Technology Management - learning / practice
- LeetCode136+128+152+148
- Roles of rollup components
- Annex I: power of attorney for 202x XXX attack and defense drill
- 练习-冒泡排序
- Maui introductory tutorial series (5.xaml and page introduction)
- 【MATLAB】通信信号调制通用函数 — 傅里叶逆变换
- 【MATLAB】MATLAB 仿真 — 窄带高斯白噪声
猜你喜欢

Capturing and sorting out external Fiddler -- Conversation bar and filter

KMP匹配字符串

2022年6月总结

在代碼中使用度量單比特,從而生活更美好

Fault analysis | mongodb 5.0 reports an error, and the legal instruction solves it

Create ASM disk through DD

中科磐云—2022广东木马信息获取解析

【兴趣阅读】Adversarial Filtering Modeling on Long-term User Behavior Sequences for Click-Through Rate Pre

6-4 vulnerability exploitation SSH banner information acquisition
![[技术发展-25]:广播电视网、互联网、电信网、电网四网融合技术](/img/87/e0469e280365ed0261e2b551ebd888.png)
[技术发展-25]:广播电视网、互联网、电信网、电网四网融合技术
随机推荐
Simulated small root pile
在代码中使用度量单位,从而生活更美好
laravel 中获取刚刚插入的记录的id
Headache delayed double deletion
COMP1721 Creating Classes
小程序毕业设计---美食、菜谱小程序
简单g++和gdb调试
网络设备应急响应指南
【MATLAB】通信信号调制通用函数 — 傅里叶逆变换
Annex I: power of attorney for 202x XXX attack and defense drill
海力士EMMC5.0及5.1系列对比详解
2022年6月总结
【MATLAB】通信信号调制通用函数 — 窄带高斯白噪声的生成
RPC - grpc simple demo - learn / practice
【MATLAB】MATLAB 仿真数字带通传输系统 — QPSK 和 OQPSK 系统
中職組網絡安全—內存取證
Annexe VI: exposé sur les travaux de défense. Docx
Deep understanding of redis -- bloomfilter
红队视角下的防御体系突破之第一篇介绍、阶段、方法
抓包整理外篇fiddler———— 会话栏与过滤器