Search Unity

Serial Port (Communication port) not working. Please Help

Discussion in 'Scripting' started by GavinCkw, Apr 23, 2019.

  1. GavinCkw

    GavinCkw

    Joined:
    Mar 23, 2019
    Posts:
    6
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5. using System.IO.Ports;
    6. using UnityEngine.UI;
    7.  
    8. public class ComPortScript : MonoBehaviour {
    9.  
    10.     public string COMNAME;
    11.     private SerialPort sp = new SerialPort();
    12.  
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         sp.PortName = COMNAME;
    17.         sp.BaudRate = 9600;
    18.         sp.Parity = Parity.None;
    19.         sp.DataBits = 8;
    20.         sp.StopBits = StopBits.One;
    21.         sp.ReadTimeout = 50;
    22.         sp.Open();
    23.         sp.DiscardInBuffer();
    24.         //sp.DiscardOutBuffer();
    25.     }
    26.  
    27.     private void OnDisable()
    28.     {
    29.         sp.Close();
    30.         Debug.Log("port is closing");
    31.     }
    32.     void findComPorts()
    33.     {
    34.         List<string> ports = new List<string>() { SerialPort.GetPortNames() + "" };
    35.         Debug.Log(ports + "this is ports");
    36.     }
    37.     // Update is called once per frame
    38.     void Update () {
    39.  
    40.         List<byte[]> DataList = new List<byte[]>();
    41.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2A, 0x20, 0x03, 0x0C });
    42.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2A, 0x21, 0x03, 0x0D });
    43.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2A, 0x22, 0x03, 0x0E });
    44.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2A, 0x23, 0x03, 0x0F });
    45.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2A, 0x24, 0x03, 0x08 });
    46.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2A, 0x25, 0x03, 0x09 });
    47.         //B
    48.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2B, 0x20, 0x03, 0x0D });
    49.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2B, 0x21, 0x03, 0x0C });
    50.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2B, 0x22, 0x03, 0x0F });
    51.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2B, 0x23, 0x03, 0x0E });
    52.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2B, 0x24, 0x03, 0x09 });
    53.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2B, 0x25, 0x03, 0x08 });
    54.         //C
    55.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2C, 0x20, 0x03, 0x0A });
    56.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2C, 0x21, 0x03, 0x0B });
    57.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2C, 0x22, 0x03, 0x08 });
    58.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2C, 0x23, 0x03, 0x09 });
    59.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2C, 0x24, 0x03, 0x0E });
    60.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2C, 0x25, 0x03, 0x0F });
    61.         //D
    62.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2D, 0x20, 0x03, 0x0B });
    63.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2D, 0x21, 0x03, 0x0A });
    64.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2D, 0x22, 0x03, 0x09 });
    65.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2D, 0x23, 0x03, 0x08 });
    66.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2D, 0x24, 0x03, 0x0F });
    67.         DataList.Add(new byte[] { 0x02, 0x20, 0x27, 0x2D, 0x25, 0x03, 0x0E });
    68.  
    69.         if (sp.IsOpen)
    70.         {
    71.             if (Input.GetKey(KeyCode.A))
    72.             {
    73.                 sp.DiscardInBuffer();
    74.                 sp.DiscardOutBuffer();
    75.                 Debug.Log("its open");
    76.                 sp.Write(new byte[] { 0x02, 0x20, 0x27, 0x2B, 0x20, 0x03, 0x0D } + "");
    77.                 sp.WriteLine("02 20 27 2A 20 03 0C");
    78.                 sp.WriteLine(DataList[1] + "");
    79.                 sp.WriteLine(new byte[] { 0x02, 0x20, 0x27, 0x2B, 0x20, 0x03, 0x0D } + "");
    80.  
    81.              
    82.                 sp.DiscardOutBuffer();
    83.             }
    84.         }
    85.     }
    86.  
    87.  
    88. }
    89.  

    Help me please, i could not get the serial port to work, it is connected to a vending machine that supposed to dispense item after i press a certain key on the keyboard. Any way to fix this?
     
    Last edited: Apr 24, 2019
  2. ThySpektre

    ThySpektre

    Joined:
    Mar 15, 2016
    Posts:
    362
    The first step I would try is to connect this to another pc running hyperterminal and see what is being sent.
     
    Joe-Censored likes this.
  3. GavinCkw

    GavinCkw

    Joined:
    Mar 23, 2019
    Posts:
    6
    Actually i just found out how to fix it, instead of those sp.writes i did. It should be "sp.write(datalist, 0, datalist.length)" i hope i can help someone that is trying to work with this. Previously i was only sending a short part of the datalist as i did not clarify the length of lines being send through the communication port or serial port.