Question

I'm trying to use .Net serialport methods from another program which uses Unity3D engine.

I've a c# class library (dll) that register and implement a callback function when the serial port receives some data.

public CSerialDll()
{         
   _serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived);
}

void serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
   int bytes = _serial.BytesToRead;
   _comBuffer = new byte[bytes];
   _serial.Read(_comBuffer, 0, bytes);
   Console.WriteLine("data recv: " + System.Text.ASCIIEncoding.ASCII.GetString(_comBuffer));
}

And then in my another program, I create an instance of the class.

I want to know whether my call back serial_DataReceived inside the library (dll) will be called or not. I'm trying to read _comBuffer inside the library from another program and it always empty (even though the data are coming when I tested with a console app inside the same app).

Any help is greatly appreciated.

Thanks

Was it helpful?

Solution

<HUNCH>

If I was a class designer, i would put the data into the callback, so if you look at the SerialDataReceivedEventArgs class, you'll see that inside it is you buffer with the data.

So, after you get an event, and you then call read, you actually discard the data that is sent to you and request more.

In other words, yes, the fact that it is an external library doesn't have anything to do with the fact it isn't working as it should.

</HUNCH>

OTHER TIPS

There's typically nothing particularly special about methods being in a separate DLL, as long as access modifiers are set correctly.

However depending on how you set up your solution it can be a pain making sure you have the latest DLL in your bin folder.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top