树莓派通过Modbus串口协议读取485接口的9轴角度传感器例程

本教程同样适用维特智能6轴系列其他型号的传感器SINDTHWT901BHWT905HWT605等等。
代码下载地址
维特智能WT901C485产品链接
在这里插入图片描述

1.硬件连接

1.给树莓派 连接电源和网线
在这里插入图片描述

2.连接上树莓派和901的模块。如果使用六合一USB转串口模块,需要将拨码拨到USB转485的模式。

JY901模块跟六合一串口模块引脚需要一一对应。

USB串口模块 901传感器
5V VCC
A A
B B
GND GND

在这里插入图片描述

2.软件使用

1.前往官网下载VNC,官网地址:https://www.realvnc.com/en/
软件安装步骤参考官网安装步骤,在下载的文件夹VNC Viewer中打开vncviewer.exe
在这里插入图片描述

在这里输入树莓派的ip地址(可通过ip搜索器搜索,选择ip名为raspberrypi的ip地址)
(## IP搜索器可自行百度下载)
在这里插入图片描述

2.输入账号密码, 账号:**** 密码:****
账号密码参考自己的树莓派资料
在这里插入图片描述

3.代码展示

将下方代码拷贝并另存为Uart61Demo.cpp文件,保存到树莓派的桌面上。

#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<assert.h>
#include<termios.h>
#include<string.h>
#include<sys/time.h>
#include<time.h>
#include<sys/types.h>
#include<errno.h>

static int ret;
static int fd;

#define BAUD 9600

int uart_open(int fd,const char *pathname)
{
   
    fd = open(pathname, O_RDWR|O_NOCTTY); 
    if (-1 == fd)
    {
    
        perror("Can't Open Serial Port"); 
		return(-1); 
	   } 
    else
		printf("open %s success!\n",pathname);
    /*测试是否为终端设备*/ 
    if(isatty(STDIN_FILENO)==0) 
		printf("standard input is not a terminal device\n"); 
    else 
		printf("isatty success!\n"); 
    return fd; 
}

int uart_set(int fd,int nSpeed, int nBits, char nEvent, int nStop)//串口名、波特率、数据位、奇偶校验位、停止位
{
   
     struct termios newtio,oldtio; 
     /*保存测试现有串口参数设置,在这里如果串口号等出错,会有相关的出错信息*/ 
     if  ( tcgetattr( fd,&oldtio)  !=  0) {
     
      perror("SetupSerial 1");
	   printf("tcgetattr( fd,&oldtio) -> %d\n",tcgetattr( fd,&oldtio)); 
      return -1; 
     } 
     bzero( &newtio, sizeof( newtio ) ); 
     /*步骤一,设置字符大小*/ 
     newtio.c_cflag  |=  CLOCAL | CREAD;  
     newtio.c_cflag &= ~CSIZE;  
/*设置数据位*/ 
     switch( nBits ) 
     {
    

      case 7: 

      newtio.c_cflag |=
  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值