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