由于项目需求,要求用485通讯方式与 4 个激光位移传感器进行通讯,遂写了这样一个示例:
- 使用的传感器为博亿精科的BL系列激光位移传感器
- 按照说明书将 4 个激光位移传感器的地址分别设置为:0x01 、0x02 、0x03 、0x04
.pro文件
QT += core gui
QT += serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
crc.cpp \
main.cpp \
mainwindow.cpp \
rs485com.cpp
HEADERS += \
crc.h \
mainwindow.h \
rs485com.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
本文福利,莬费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击莬费领取↓↓
CRC 校验 crc.h 和 crc.cpp
/// CRC校验码计算
unsigned short CRC16(unsigned char* pBuf, unsigned short len);
/// CRC校验码计算
unsigned short CRC16(unsigned char* pBuf, unsigned short len)
{
unsigned short uCRC = 0xFFFF;
int i, j;
for (i = 0; i < len; i++)
{
uCRC ^= (*(pBuf + i));
for (j = 0; j < 8; j++)
{