Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by refsus · Oct 18, 2013 at 04:27 AM · serial

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)
Comment
Add comment · Show 13
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ArkaneX · Oct 18, 2013 at 08:54 AM 0
Share

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);
 }


avatar image meat5000 ♦ · Oct 18, 2013 at 08:59 AM 0
Share

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

avatar image refsus · Oct 21, 2013 at 08:46 AM 0
Share

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?

avatar image ArkaneX · Oct 21, 2013 at 09:00 AM 0
Share

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

avatar image ArkaneX · Oct 21, 2013 at 09:19 AM 1
Share

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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
         }
 
     }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges