Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.01 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.IO.Ports;
  5.  
  6. public class Comunicacao : MonoBehaviour
  7. {
  8.     public string mensagem;
  9.  
  10.     public static SerialPort sp = new SerialPort("COM3", 9600);
  11.    void Awake()
  12.     {
  13.         sp.Open();
  14.     }
  15.     void Start()
  16.     {
  17.        
  18.         OpenConnection();
  19.         foreach (string str in SerialPort.GetPortNames())
  20.         {
  21.             Debug.Log(string.Format("Existing COM port: {0}", str));
  22.         }
  23.     }
  24.     void OpenConnection()
  25.     {
  26.         if(sp != null)
  27.         {    
  28.                 if (sp.IsOpen)
  29.                 {
  30.                     print("Porta aberta");
  31.                 }
  32.                 else
  33.                 {
  34.                     print("Porta == null");
  35.                 }  
  36.         }
  37.     }
  38.  
  39.     void OnApplicationQuit()
  40.     {
  41.         sp.Close();
  42.     }
  43.     // Update is called once per frame
  44.     void Update()
  45.     {
  46.         Debug.Log(sp.ReadLine());
  47.         mensagem = sp.ReadLine();
  48.        
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement