void Start() { try{ helper = SerialHelper.CreateInstance("COM7"); helper.setTerminatorBasedStream("\n"); //delimits received messages based on '\n' char // helper.setLengthBasedStream(); /* * will received messages based on the length provided, this is useful in transfering binary data * if we received this message (byte array) : * {0x55, 0x55, 0, 3, 'a', 'b', 'c', 0x55, 0x55, 0, 9, 'i', ' ', 'a', 'm', ' ', 't', 'o', 'n', 'y'} * then its parsed as 2 messages : "abc" and "i am tony" * the first 2 bytes are the length data writted on 2 bytes * byte[0] is the MSB * byte[1] is the LSB * * on the unity side, you dont have to add the message length implementation. * * if you call helper.SendData("HELLO"); * this API will send automatically : * 0x55 0x55 0x00 0x05 0x68 0x65 0x6C 0x6C 0x6F |________| |________| |________________________| * preamble Length Data * * * when sending data from the arduino via usb cable, there's no preamble added. * this preamble is used to that you receive valid data if you connect to your arduino and its already send data. * so you will not receive * on the arduino side you can decode the message by this code snippet: * char * data; * char _length[2]; * int length; * * if(Serial.avalaible() >2 ) * { * _length[0] = Serial.read(); * _length[1] = Serial.read(); * length = (_length[0] << 8) & 0xFF00 | _length[1] & 0xFF00; * * data = new char[length]; * int i=0; * while(i<length) * { * if(Serial.available() == 0) * continue; * data[i++] = Serial.read(); * } * * * ...process received data... * * * delete [] data; <--dont forget to clear the dynamic allocation!!! * } */ helper.OnConnected += () => { Debug.Log("Connected"); text.text = "Connected"; //if(text3d != null) // text3d.text = "Connected"; }; helper.OnConnectionFailed += () => { Debug.Log("Failed"); text.text = "failed"; //if(text3d != null) text3d.text = "failed"; }; helper.OnDataReceived += () => { text.text = helper.Read(); value = text.text; //if(text3d!= null) text3d.text = value; }; helper.OnPermissionNotGranted += () => { //.... }; helper.Connect(); }catch (Exception ex) { text.text = ex.Message; //if(text3d != null) text3d.text = ex.Message; } }