예제 #1
0
    // Update is called once per frame
    void Update()
    {
        if (serialPort != null)
        {
            if (Input.GetKeyDown(writeSimple))
            {
                //send
                serialPort.WriteCRLF("TestData");
            }
            if (Input.GetKeyDown(writeList))
            {
                List <string> dataArray = new List <string>();
                dataArray.Add("AAA");
                dataArray.Add("BBB");
                dataArray.Add("CCC");
                serialPort.Write(dataArray, ",", "<CR><LF>");
                //AAA,BBB,CCC<CR><LF>
            }
            if (Input.GetKeyDown(writeDictionary))
            {
                Dictionary <string, string> dataArray = new Dictionary <string, string>();
                dataArray.Add("AAA", "BBB");
                dataArray.Add("CCC", "DDD");
                serialPort.Write(dataArray, ",", "<CR><LF>");
                //AAA,BBB,CCC,DDD<CR><LF>
            }
            if (Input.GetKeyDown(writeBinary))
            {
                byte[] bin = new byte[6] {
                    0x00, 0x01, 0x02, 0x03, 0x04, 0xFF
                };
                serialPort.Write(bin);
            }

            //for Arduino micro leonardo
            if (Input.GetKeyDown(KeyCode.R))
            {
                serialPort.Close();

                int saveBaudRate = serialPort.BaudRate;
                serialPort.BaudRate = 1200;                     //Reset Command
                serialPort.Open();
                //Reset
                serialPort.Close();
                serialPort.BaudRate = saveBaudRate;
            }
        }
    }
예제 #2
0
 // Update is called once per frame
 void Update()
 {
     if (serialPort != null)
     {
         if (serialPort.IsOpened())
         {
             if (Input.GetKeyDown(SendKeyCode))
             {
                 byte[] d = System.Text.Encoding.ASCII.GetBytes("test\0\0byte\0\0test");
                 serialPort.Write(d);
             }
             if (Input.GetKeyDown(SendKeyCode2))
             {
                 serialPort.Write("string data");
             }
         }
     }
 }
예제 #3
0
    public void controlTank(float horizontal, float vertical)
    {
        JSONObject json = new JSONObject();

        if (sendMode == SendMode.SERIAL)
        {
            int left  = (int)((vertical + horizontal) * 255 * (maxSpeed / 100));
            int right = (int)((vertical - horizontal) * 255 * (maxSpeed / 100));
            if (left > 255)
            {
                left = 255;
            }
            if (left < -255)
            {
                left = -255;
            }
            if (right > 255)
            {
                right = 255;
            }
            if (right < -255)
            {
                right = -255;
            }
            json.AddField("L", left); json.AddField("R", right);
            if (serialPort)
            {
                serialPort.Write(json.ToString() + "\r\n");
            }
        }
        else
        {
            json.AddField("x", (int)(horizontal * maxSpeed)); json.AddField("y", (int)(vertical * maxSpeed));
            if (sendMode == SendMode.BLUETOOTH)
            {
                if (bluetoothSender)
                {
                    bluetoothSender.sendMessage("t" + json.ToString() + "\r\n");
                }
            }
            else if (sendMode == SendMode.UDP)
            {
                if (udpSender)
                {
                    udpSender.sendMessage("t" + json.ToString() + "\r\n");
                }
            }
        }
    }