Search Unity

c# and serial port

Discussion in 'Scripting' started by DjVic, Sep 4, 2012.

  1. DjVic

    DjVic

    Joined:
    Sep 4, 2012
    Posts:
    1
    Good Evening,
    i use unity for programmin with c# a applicaton with the serial port.

    The problem is that the call handler read the serial does not work. I place the code.
    The serial not read never and often unity crash.
    Exist a solution???

    Thank you


    using UnityEngine;
    using System.IO.Ports;
    using System.Threading;
    using System.Collections.Generic;
    using System.Timers;
    using System.ComponentModel;
    using System.Text;
    using System;




    public class NewBehaviourScript : MonoBehaviour
    {



    string SerialRx;

    //Creo la Porta Seriale
    static SerialPort mySerialPort;

    //Timer
    System.Timers.Timer t = new System.Timers.Timer();


    void Start ()
    {


    //Imposto i valori della seriale e la "apro"
    mySerialPort = new SerialPort("COM4");
    mySerialPort.BaudRate = 9600;
    mySerialPort.Parity = Parity.None;
    mySerialPort.StopBits = StopBits.One;
    mySerialPort.DataBits = 8;
    mySerialPort.Handshake = Handshake.None;
    mySerialPort.DtrEnable = true;
    mySerialPort.RtsEnable = true;
    mySerialPort.ReadBufferSize = 8000;
    mySerialPort.WriteBufferSize = 8000;
    mySerialPort.ReadTimeout = 100;
    mySerialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(DataReceivedHandler);


    mySerialPort.Open();



    }


    static void DataReceivedHandler(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) <------ not work

    {
    SerialPort sp = (SerialPort)sender;
    string indata = sp.ReadExisting();
    print("Data Received:");
    print(indata);
    }



    void Update ()
    {
    }


    void FixedUpdate()
    {

    }

    }


    :confused::confused::confused::confused::confused::confused::confused::confused:
     
  2. andy78flavia28

    andy78flavia28

    Joined:
    Jun 25, 2015
    Posts:
    3
    Have you solved?
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    that user hasn't logged in since about 2 weeks from posting this thread 3 years ago...