• 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 astk · Dec 07, 2010 at 04:13 PM · serialport

Serial communication problem

I've a .net (not mono) c# class library (dll) that register and implement a callback function when the serial port receives some data. I used .net because mono serialport doesn't support this callback feature as far as I understand.

It looks something like this.

public CSerialDll() { //constructor create new instance, register callback and open the port... ..... _serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived); ..... }

void serial_DataReceived(object sender, SerialDataReceivedEventArgs e) { int bytes = _serial.BytesToRead; _comBuffer = new byte[bytes]; //fill this var which is a public property of dll _serial.Read(_comBuffer, 0, bytes); Console.WriteLine("data recv: " + System.Text.ASCIIEncoding.ASCII.GetString(_comBuffer)); }

In my Unity, I used that dll like this.

using UnityEngine; using SerialDll; using System.Collections;

public class TestSerialDLL : MonoBehaviour { //serial connection CSerialDll comm = new CSerialDll();

 // Use this for initialization
 void Start () {
 }

 // Update is called once per frame
 void Update () {
     byte[] recvBytes = comm.ComBuffer;
     if (recvBytes != null) {
         if (recvBytes.Length > 0)
             print("Recevie Data: " + recvBytes);
     }
 }

}

But it doesn't work. As you can see in my above code, that callback fills _comBuffer which has a property to read from outside programs. And then in my update loop of my client program, I tried to read that property. This property is always empty.

But when I make the dll project as a console app project and wrote a main method to test the code, I can receive the data from serial.

Any ideas? Thanks

Comment
Add comment · Show 4
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 Statement · Dec 07, 2010 at 08:02 PM 0
Share

I am not 100% sure, but won't your dll get loaded into (and run through) mono either way? Another option would be to make a C/C++ wrapper.

avatar image astk · Dec 08, 2010 at 03:06 AM 0
Share

Hi... I don't have Unity Pro to do c\c++ plug-in.

avatar image astk · Dec 08, 2010 at 03:29 AM 0
Share

I tried to implement serial connection in $$anonymous$$ono. But my Unity either crash or hang whenever I call Read* methods on serialport. That's why I'm trying this .net class library dll approach. Thanks

avatar image DaveA · Apr 10, 2011 at 02:24 AM 0
Share

Did you ever get that working?

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Arduino Unity3d FPS drop after twisting Potentiometer 0 Answers

The Semaphore Timeout Period Has Expired 0 Answers

Problems with serialport on Android! Unhandled Exception: System.DllNotFoundException: MonoPosixHelper 2 Answers

Using Arduino as controller in Unity is giving me bad framerate 1 Answer

Multiple SerialPorts delay (C#) 0 Answers


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