x


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

alt text

Marson Barcode Reader

alt text

log.png (59.9 kB)
dsc_8024.jpg (188.6 kB)
more ▼

asked Oct 18, 2013 at 04:27 AM

avatar image

refsus
127 27 26 29

Very specific question, and most probably totally unrelated to Unity. I can only suggest to log actual exception (Debug.LogException), so instead of

 catch
 {
     Debug.Log("com False");
 }

please use

 catch(Exception ex)
 {
     Debug.LogException(ex);
 }


Oct 18, 2013 at 08:54 AM ArkaneX

Have you tested the device's operation? (ie outside of Unity)

Oct 18, 2013 at 08:59 AM meat5000

sorry for the late response, I have a little mistake, unity cannot read COM14, then I tried change COM into COM2. 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?

Oct 21, 2013 at 08:46 AM refsus

I think you have to use different method to read data then. For example ReadByte/ReadChar or ReadExisting to get all available bytes.

Oct 21, 2013 at 09:00 AM ArkaneX

One more thought - instead 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.

Oct 21, 2013 at 09:19 AM ArkaneX
show all comments (comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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;
         }
 
     }
 
more ▼

answered Jan 16, 2014 at 07:51 PM

avatar image

Benoit Dufresne
216 8 10 13

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x34

asked: Oct 18, 2013 at 04:27 AM

Seen: 944 times

Last Updated: Jan 16, 2014 at 07:51 PM