Read serial from barcode reader

Hi,

I want to read the serial output of the barcode reader (MARSON Product) (not created using Arduino), every time I press the button on the barcode reader, the output of the barcode reader will be taken through the code via the Unity3D, but I am having problems that can not be read from the COM barcode reader, although I have enter the same COM on barcode reader with COM at my code. In putty barcode scanner displays alphanumerics code, I wonder what happened?

I attach the following code and images

Code :

using UnityEngine;
using System.Collections;
using System.IO;
using System.IO.Ports;

public class Micro : MonoBehaviour
{
    string strIn;
    string FileName;
    StreamReader reader;
    string COM;
    int EnableMicro;
    public static int a1 = 0;
    public static int a2 = 0;
    public static int d1 = 0;
    public static int d2 = 0;
    public static int d3 = 0;

    SerialPort serial;
    private string[] data;

    string[] fungsi;
    string[] nilai;
    float TimerMicro;
    string value;

    void Start()
    {
        serial = new SerialPort("COM14", 9600);
        OpenConnection();
    }

    void Update()
    {
        try
        {
            value = serial.ReadLine();
            try
            {
                a1 = int.Parse(value);
            }
            catch
            {
                a1 = 0;
            }
        }
        catch
        {
            value = ("null");
        }
    }

    void OpenConnection()
    {
        if (serial != null)
        {
            if (serial.IsOpen)
            {
                serial.Close();
                Debug.Log("Closing port, because it was already open!");
            }
            else
            {
                try
                {
                    serial.Open();  // opens the connection
                    //serial.ReadTimeout = 1;  // sets the timeout value before reporting error
                    Debug.Log("Port Opened!");
                }
                catch
                {
                    Debug.Log("com False");
                }
            }
        }
        else
        {
            if (serial.IsOpen)
            {
                // print("Port is already open");
                Debug.Log("Port is already open");
            }
            else
            {
                //print("Port == null");
                Debug.Log("Port == null");
            }
        }
    }
    void OnApplicationQuit()
    {
        serial.Close();
    }

    void OnGUI()
    {
        GUI.Label(new Rect(Screen.width / 2, Screen.height / 2, 100, 50), "" + a1);
    
    }
}

Image

Marson Barcode Reader

16674-dsc_8024.jpg

I’ve just gotten a bar code reader myself and I’m not sure about yours, but mine outputs data just like a keyboard would (e.g. reading a bar code in notepad writes the decoded string +
)

In that case reading the data is trivial:

	public UILabel barCode;
	public float timeDelay = 0.1f;
	private string currentCode;
	private float lastReceivedInput = 0f;
	// Use this for initialization
	void Start () {
		currentCode = "";
	}
	
	// Update is called once per frame
	void Update () {
		if (Time.time > lastReceivedInput + timeDelay){
			currentCode = "";
		}
		if (Input.inputString != ""){
			currentCode += Input.inputString;
			lastReceivedInput = Time.time;
		}
		if (currentCode!="" && (currentCode[currentCode.Length-1] == '

’ || currentCode[currentCode.Length-1] == ‘\r’)){
barCode.text = currentCode;
}

	}

Unity3d dont support com greater than com9, for example com1…com9 are supported and com10, com11… are not supported

Solution to this issue is, you need to rename the port name

Go to Device Manager,
Open Port(COM & LPT)
Right click on the USB of your Scanner and select properties
Select Port Settings tab and click on advance option
new window will open, in that at bottom their is com port number with drop box
Select any com name below COM9 and press OK
Restart you system after changing
and re-verify in device manager its changed or not