找回密码
 立即注册
楼主: 枯木叶

[脚本] 有没有懂串口通信的,快点戳进来

[复制链接]
发表于 2014-7-26 16:53 | 显示全部楼层 |阅读模式
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 com : MonoBehaviour   
{   
public GameObject target;
    private SerialPort sp;   
    private Thread recvThread;   
    float x,y,z;
    // Use this for initialization   
    void Start()   
    {   
       // Debug.Log("start");   
        sp = new SerialPort("COM6", 115200, Parity.None, 8, StopBits.One);   
         
        if (!sp.IsOpen)  
        {  
            sp.Open();   
        }  
        recvThread = new Thread(ReceiveData);   
        recvThread.Start();   
    }   
  
  
    // Update is called once per frame   
    void Update()   
    {   
        Debug.Log(x+"&&"+y+"&&"+z);  
        
        //target.transform.eulerAngles=new Vector3(x,y,z);  
    }  
      
    private void ReceiveData()   
    {   
        char[] spil="#".ToCharArray();  
        char[] sbuf = new char[1024];  
        try   
        {   
            Byte[] buf = new Byte[1];   
            string sbReadline2str = string.Empty;   
            if (sp.IsOpen) sp.Read(buf, 0, 1);   
              
            if (buf.Length == 0)   
            {   
                return;   
            }  
            else if(buf[0]==33){
                int i=0;  
                while(true){
                    sp.Read(buf, 0, 1);
                    if(buf[0]!=33)
                        {  
                           
                            sbuf[0]=(char)buf[0];  
                            i++;  
                        }else{  
                            String s=new String(sbuf,0,i);  
                            String []ss=s.Split(spil);  
                            i=0;  
                           
                            if(ss.Length==3)  
                            {  
                                x=float.Parse(ss[0]);  
                                y=float.Parse(ss[1]);  
                                z=float.Parse(ss[2]);  
                            }  
                             
                        }  
                }  
              
            }  
  
        }  
        catch (Exception ex)   
        {   
            Debug.Log(ex);   
        }  
    }   
  
     
private void SendSerialPortData(string data)   
{   
     if(sp.IsOpen)   
     {   
         sp.WriteLine(data);   
     }   
}   
   
    void OnApplicationQuit()   
    {   
        sp.Close();   
    }   
}  

这是我在网上找的一个关于串口通信代码修改后的,都是接受ARDUNIO板子发送的数据(一个手柄的XYZ的模拟电压值),我要就收的是由感叹号开头,#隔开的三个数据一组的模拟电压值,每个值都在0~1023之间。为什么我在UNITY运行的时候总在控制台总是显示0&&0&&0
 楼主| 发表于 2014-7-26 16:56 | 显示全部楼层
发表于 2017-3-5 20:44 | 显示全部楼层
很不错
发表于 2017-3-5 20:38 | 显示全部楼层
好帖就是要顶
发表于 2017-3-5 20:46 | 显示全部楼层
顶顶多好
发表于 2017-3-5 20:33 | 显示全部楼层
真心顶
发表于 2017-3-5 21:04 | 显示全部楼层
说的非常好
发表于 2017-6-6 14:52 | 显示全部楼层
楼主是超人
发表于 2017-6-6 14:45 | 显示全部楼层
好帖就是要顶
发表于 2017-6-6 14:46 | 显示全部楼层
顶顶多好
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-3 20:36 , Processed in 0.070584 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表