Unity3D 之 串行口

Unity3D使用串口与单片机等进行数据传输


一个简单的例子:(接收一个字节)
using UnityEngine; 
using System.Collections; 
using System; 
using System.Threading; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.IO.Ports; 
using System.Text.RegularExpressions; 
using System.Text; 


public class tmp : MonoBehaviour 
{ 
    private SerialPort sp; 
    private Thread recvThread; 
    // Use this for initialization 
    void Start() 
    {  
        sp = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); 
		//串口初始化
        if (!sp.IsOpen)
        {
            sp.Open(); 
        }
        recvThread = new Thread(ReceiveOneByte); //该线程接收一个字节
        recvThread.Start(); 
    } 
    void Update() 
    {
		//...
    }
	
    private void ReceiveOneByte() 
    { 
	
        try 
        { 
           	Byte[] buf = new Byte[1]; 
            string sbReadline2str = string.Empty; 
            if (sp.IsOpen) 
				sp.Read(buf, 0, 1); 
        }
        catch (Exception ex) 
        { 
            Debug.Log(ex); 
        }
    } 

	/* private void SendSerialPortData(string data) //发送一个字节
	 { 
		 if(sp.IsOpen) 
		 { 
			 sp.WriteLine(data); 
		 } 
	 } 
	*/
	void OnApplicationQuit() 
	{ 
	    sp.Close(); 
	} 
}

应用:

Arduino+MPU6050提供姿态数据由Unity3D实时显示


Arduino端:

1.准备:
与MPU6050的物理连接:A4---SDA A5---SCL VCC---3V3 GND---GND GND---ADD

Arduino IDE(我这用的是1.0.5)
库文件:https://github.com/jrowberg/i2cdevlib  下载后本次需要的是MPU6050和I2Cdev两个文件夹,将它们拷贝进IDE的安装目录下的libraries\文件夹下

2.代码:
代码可由MPU6050库自带的Example中的“MPU6050_DMP6”改写(该Example还可用于测试连接或元件是否正常工作)
改写内容有:
1.选择输出欧拉角功能
2.去除其它不必要输出和输入
3.修改输出格式如下: 每一组欧拉角前加“!”,欧拉角内以“#”分隔
注:由该Example可获得MPU6050经DMP处理后的数据,包括四元数,欧拉角,加速度,具体获得那种数据,靠预处理来选择
最终代码:
// I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class using DMP (MotionApps v2.0)
// 6/21/2012 by Jeff Rowberg <[email protected]>
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
//
// Changelog:
//      2013-05-08 - added seamless Fastwire support
//                 - added note about gyro calibration
//      2012-06-21 - added note about Arduino 1.0.1 + Leonardo compatibility error
//      2012-06-20 - improved FIFO overflow handling and simplified read process
//      2012-06-19 - completely rearranged DMP initialization code and simplification
//      2012-06-13 - pull gyro and accel data from FIFO packet instead of reading directly
//      2012-06-09 - fix broken FIFO read sequence and change interrupt detection to RISING
//      2012-06-05 - add gravity-compensated initial reference frame acceleration output
//                 - add 3D math helper file to DMP6 example sketch
//                 - add Euler output and Yaw/Pitch/Roll output formats
//      2012-06-04 - remove accel offset clearing for better results (thanks Sungon Lee)
//      2012-06-01 - fixed gyro sensitivity to be 2000 deg/sec instead of 250
//      2012-05-30 - basic DMP initialization working

/* ============================================
I2Cdev device library code is placed under the MIT license
Copyright (c) 2012 Jeff Rowberg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值