Jump to content

SerialPort declaration failure


Recommended Posts

Sorry for my English (I don't speak it fluently).

OS : Windows 10 - KSP version : 1.3.0.1804

Hi !

I made this program that works fine :

using UnityEngine;
using System.IO.Ports;

namespace DashboardDriver
{
    [KSPAddon (KSPAddon.Startup.MainMenu, false)]
    class DashboardDriver : MonoBehaviour
    {
        public void Start()
        {
             Debug.Log ("ADDON WORKS !");
        }
    }
}

Even if I had the line SerialPort port; before Debug.Log ("ADDON WORKS !");

But, when I declare a SerialPort outside a method like this :

using UnityEngine;
using System.IO.Ports;

namespace DashboardDriver
{
    [KSPAddon (KSPAddon.Startup.MainMenu, false)]
    class DashboardDriver : MonoBehaviour
    {
        private SerialPort port; //here

        public void Start()
        {
             Debug.Log ("ADDON WORKS !");
        }
    }
}

The addon doesn't works (I don't see [AddonLoader]: Instantiating addon 'DashboardDriver' from assembly 'Dashboard driver' or ADDON WORKS ! in the console).

I never see a thing like this before and I don't know how to declare the SerialPort. So I ask you for an answer.

Thanks.

Edited by TheRedColossus
Link to comment
Share on other sites

Yes.

I already tried to use OpenNETCF.IO.Ports but when I use it, I can send data to my Arduino but not receive data from the Arduino (port.BytesToRead is always equals at 0). Same problem when I use the addon KSPSerialIO by zitronen : no data is received. I heard that the problem can come from Windows 10. So is there a solution (without change the OS) ?

Edited by TheRedColossus
Link to comment
Share on other sites

I am certain the folks in the arduino thread can be more help - you might want to just post there?   If you can't just use the arduino mod as a basis for what you're trying to do?   

Just guessing from the name of your mod -dashboard driver - I'm assuming you're building a hardware interface?   The other projects that do that might be informative.  If not... you could also use KRPC to get the KSP data you need IN and OUT of the game and then write your own standalone application that processes data and handles the IO?  

But beyond that, I'd ask the arduino guys.  They can probably just paste you 5 lines that does it. :)

Link to comment
Share on other sites

On 28/07/2017 at 10:34 PM, TheRedColossus said:

I already tried to use OpenNETCF.IO.Ports but when I use it, I can send data to my Arduino but not receive data from the Arduino (port.BytesToRead is always equals at 0). Same problem when I use the addon KSPSerialIO by zitronen : no data is received. I heard that the problem can come from Windows 10. So is there a solution (without change the OS) ?

The problem KSPSerialIO has is that it subscribes to the SerialPort.dataReceived event, which is never called on Windows 10. As best I can tell from my testing, that's due to a driver issue with the serial driver used for official Arduino boards. You've got a couple of options here:

  • Buy an Arduino board that doesn't use the ATmega16U2 driver for serial communication. Lots of Arduino-compatible clone boards use the cheaper CH340G serial chip, and there's been reports of that working fine with KSPSerialIO, I imagine it will work OK with other code using OpenNETCF.
  • Buy a separate CH340G serial adaptor, and connect that to your existing board. You can buy one of these through ebay or aliexpress for a few dollars.
  • Use the System.IO.Ports code from the mono project. The SerialPort.dataReceived event still won't work, but I can assure you that the SerialPort.BytesToRead property does. In my project on Windows I start a separate polling thread that uses BytesToRead to check for new data, and that's been reliable in Windows 10 on all of my test hardware.
Link to comment
Share on other sites

Quote

Glad I could help!   I've been using the python client library, but I'm pretty familiar with KRPC so if you get stuck, just shout at me!

Thanks again ! :D

Quote

SerialPort.dataReceived event, [...] is never called on Windows 10

Yes but even if I use ByteToRead() it still doesn't works (return value is always 0).

Quote

Use the System.IO.Ports code from the mono project

I don't understand what you mean. If I use the Mono IDE it will works ?

In all cases when I declare outside of a method a SerialPort from System.IO.Ports my addon does't start.

Edited by TheRedColossus
Link to comment
Share on other sites

5 hours ago, TheRedColossus said:

I don't understand what you mean. If I use the Mono IDE it will works ?

No, sorry if that wasn't clear. I mean I ripped the System.IO.Ports class out of the mono sources, modified them slightly so that they would build standalone, and included that in my project.

Link to comment
Share on other sites

Yeah, that's the code I'm talking about. The high level way to do that is:

  • Copy all of those source files in to a directory of your project.
  • Change the namespace to something that makes a little more sense.
  • Fix the handful of compile errors that will crop up.

I realise at this point that using kRPC with your preferred language is probably the more attractive option. But if you want to play around with doing serial yourself, it might be easier to have a look at my KerbalSimpit.IO.Ports class. I've been meaning to split that out to a separate KSP mod, but you can probably just drop that folder in to your project, and use `KerbalSimpit.IO.Ports` exactly the same way you'd use System.IO.Ports.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...