- Home /
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
Very specific question, and most probably totally unrelated to Unity. I can only suggest to log actual exception (Debug.LogException), so ins$$anonymous$$d of
catch
{
Debug.Log("com False");
}
please use
catch(Exception ex)
{
Debug.LogException(ex);
}
Have you tested the device's operation? (ie outside of Unity)
sorry for the late response, I have a little mistake, unity cannot read CO$$anonymous$$14, then I tried change CO$$anonymous$$ into CO$$anonymous$$2. and now work perfectly. But I have another problem everytime I use ReadLine() or ReadBytes() function for read the data from serial, Unity suddenly "hang"
void Update()
{
try
{
value = serial.ReadLine();
try
{
a1 = int.Parse(value);
}
catch
{
a1 = 0;
}
}
catch
{
value = ("null");
}
}
I guess ReadLine() will block (not return) until it finds an EOL. what should I do?
I think you have to use different method to read data then. For example ReadByte/ReadChar or ReadExisting to get all available bytes.
One more thought - ins$$anonymous$$d retrieving bytes manually, it would be a good idea to just use DataReceived event. Use it carefully though, because according to documentation, it is raised in another thread.
Answer by Benoit Dufresne · Jan 16, 2014 at 07:51 PM
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 + \r\n)
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] == '\n' || currentCode[currentCode.Length-1] == '\r')){
barCode.text = currentCode;
}
}
Answer by PSDfriends · Oct 29, 2018 at 09:02 AM
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613120140im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to revert back to older Unity3d Pro licensed version 3.4? 2 Answers
where do I find my serial number? 1 Answer
Secure Unity app (WinOS) with product key 1 Answer
Bluetooth serial link on Android 0 Answers
Why deviceUniqueIdentifier changes? 1 Answer