zitron-git / KSPSerialIO

Kerbal Space Program Serial Port IO plugin
60 stars 44 forks source link

Serial for Mac / *ix #5

Closed ghost closed 9 years ago

ghost commented 9 years ago

Not an issue, but I just found this thread: http://forum.kerbalspaceprogram.com/threads/107815-Arduino-Direct-Serial-communications-working!-MAC-*NIX-and-possibly-WIN

Might be worth testing and starting a collaboration with.

unixunion commented 9 years ago

Hi! I am the author of that post! :D I was just coming here to ask Zitron if he could try use it with KSPSerialIO. :D

/K

zitron-git commented 9 years ago

Nice, have you tested this on windows? If this works it will be pretty amazing!

unixunion commented 9 years ago

I don't have a windows box to attempt building this. Perhaps you could try compile it and see? I have attempted integrate KSPSerialIO with my PsimaxSerial library, however it seems that DataReceived event is never triggered due to the Arduino lacking some signalling on the Serialport, namely RTS, DTS and so on.

I am looking through serialportnet code to see how it is implemented in there.

/K

ghost commented 9 years ago

Unfortunatelly I don't have a working hardware controller at the moment (had to disassemble my test-rig and YAKK Mk-1 still waits completion) so I can't help testing this at the moment.

Will get back to you guys once my test-rig is back in business.

zitron-git commented 9 years ago

My Port_ReceivedEvent is not triggered by RTD or DTS, I think it's just triggered by x byte being received. Where x is set in the VS IDE. I think this is the standard thing in system.port.

unixunion commented 9 years ago

I have tried writing a simple C# app which sets up a port, and then adds a ReceivedEvent method to the port, It never fires on inbound packets from my Arduino, so I think it is a method relating to Mono.

Did you modify serialportnet source at all?

Here is a GIST you can try on your arduino, just load up a sketch that spews out some text in a loop. Then run this: https://gist.github.com/unixunion/45e4d9d3e261d6ef5593

/K

zitron-git commented 9 years ago

Since KSP mono does not have system.IO.ports that won't work in KSP. Of course it will work fine if you use normal .net in windows, "/dev/tty.usbmodem621" needs to be changed to "COM1" or whatever. The SerialPort.dll I'm using only supports windows, so it will not work on Mac/linux. I don't have a Mac/Linux computer and you don't have a PC, it will be kind of difficult to test things...

unixunion commented 9 years ago

The test just shows that the System.IO.Port code on Mac at least does not work. My Code is a re-namespace of System.IO.Ports, so it suffers from the same issue. Can you see if it does fire under windows?

zitron-git commented 9 years ago

Wait are you saying the System.IO.Port in Mono does not work on Mac?

I'm not using Mono, but System.IO.Port in MS VS C# definitely works fine in windows, I use it all the time e.g.: https://github.com/zitron-git/XPlaneHWITLConnect

However, KSP does not have System.IO.Port at all so I have to use serialportnet. Which definitely does not work on Mac/linux, and is also incomplete.

Which one do you want me to test?

unixunion commented 9 years ago

System.IO.Ports works for general Read and Write to the port on MAC with an Arduino, but like I said, if you attach a delegate function to the DataReceived event listener, it doesn't seem to ever be called when my arduino spews data onto the Serial bus.

Its very weird. I am deep digging inside mono's sources now to see what actually calls the OnReceiveData function within the serial classes.

zitron-git commented 9 years ago

Oh OK that is weird. In visual studio you just attach the function to the port's data received event and that's it. Have you played with the received bytes threshold and read time out..?

unixunion commented 9 years ago

ReceivedBytesThreshold throws unimplemented immediately if you try use it. This is very bizarre. I have searched all of Mono source code, there are ZERO references to the OnDataReceived functions within SerialPort.cs.

My c# is not so great, so I am struggling to get to grips with how serialportnet actually achieves it. Basically if we can implement ReceivedBytesThreshold and then somehow attach a monitor onto it and call OnDataReceived, then I think it will be solved.

I am just curious if the GIST I pasted above will actually call the delegate on windows, because if it does, then I know that the issue is related to *nix and not .NET.

edit: see this: https://antanas.veiverys.com/mono-serialport-datareceived-event-workaround-using-a-derived-class/

It seems to poll and call the OnDataReceived method.

/K

unixunion commented 9 years ago

Using that last link I posted, I have managed to Proof-Of-Concept a patch for PsimaxSerial to fix OnDataReceived! I will hopefully get it all polished in the next day or two.

unixunion commented 9 years ago

Hi Again!

So I have been testing my fix, and though it works with console apps, once the Serial class is loaded and used by Unity, the events don't seem to fire. I have found a simple workaround though and seem to have KSPSerialIO running and talking to my board. changes:

Use SerialPort.GetPortNames()

This should get the portlst available from *nix and Windows. The code in SerialPort.cs checks the registry keys as you do in KSPSerialIO.cs

Added a config boolean NoDetect

All attempts to allow discovery of my board have failed so far, so I added a config to force KSPSerialIO to use the config.xml settings.

Added Update() to KSPSerialPort class

This is called on every frame and in turn calls Port_ReceivedEvent from which I stripped the args. This will process the SerialPort on every frame, so around 30~50 times per second which is OK I think.

I have pushed my hacked version up to https://github.com/unixunion/KSPSerialIO I have only tested that my arduino gets updated from the game state, RCS and SAS and so forth, I don't have any potentiometers to test input at this stage.

/Kegan

zitron-git commented 9 years ago

Very nice! I'm surprised mono's serial code is broken. There will be quite a few people interested in this in the thread: http://forum.kerbalspaceprogram.com/threads/68642-Arduino-Addon-for-Mac

The nodetect thing is kind of odd, by default it should try to use the port in the cfg file first, it will search only if that fails...

I would say don't bother with trying to make it cross platform, it would make testing even more difficult than it already is. You should maintain your own fork, I will link to it in the first post in the main thread when you are ready.

unixunion commented 9 years ago

Fixed the handshake! For some reason, I just had to send some additional Handshake packets.

unixunion commented 9 years ago

Hi Zitron,

I've seen at least one person able to use my port on Linux, so I think its a success! Link to it at your convenience. :D

/K

unixunion commented 9 years ago

Tested in Windows now too! :)

zitron-git commented 9 years ago

Hey do you have a direct link to the mac/linux binaries I can link to in the first post? Also you may consider making a kerbalstuff page which I can also link to in mine: https://kerbalstuff.com/mod/385/KSP%20Serial%20IO

unixunion commented 9 years ago

Hej!

Binaries are available here:

https://github.com/unixunion/KSPSerialIO/tree/master/release/0.17.0/

Ill look into setting up a kerbalstuff page soon.

/K

On 25 January 2015 at 20:08, zitron-git notifications@github.com wrote:

Hey do you have a direct link to the mac/linux binaries I can link to in the first post? Also you may consider making a kerbalstuff page which I can also link to in mine: https://kerbalstuff.com/mod/385/KSP%20Serial%20IO

— Reply to this email directly or view it on GitHub https://github.com/zitron-git/KSPSerialIO/issues/5#issuecomment-71386616 .

zitron-git commented 9 years ago

OK added to the first post. 0.17 is not the current release version, but it's fine for now. If you can have a kerbal stuff page or some other permanent link it would be great, otherwise I have to change the link manually every version!

unixunion commented 9 years ago

Kerbalstuff page is up: https://kerbalstuff.com/mod/540/KSPSerialIO%20Mac/Linux/Win

/K

unixunion commented 9 years ago

I guess we can close this case now, lets stay in touch for when you release the next version and If you can go straight to 0.17.1+ then we can sync up the release versions.

zitron-git commented 9 years ago

OK updating to 0.17.1 soon. issue closed. Good job!