Hi, sorry I didn’t respond to this before. (I don’t check the forums as much as I use to since I’ve gotten a full time job.)
The only thing I see wrong in your script is the update loop doesn’t have a check to see if there is any data in the buffer.
void Update ()
{
if(stream.BytesToRead >0)
{
receivedstring = stream.ReadLine(); //Read the information
stream.BaseStream.Flush(); //Clear the serial information so we assure we get new information.
}
}
That said, I haven’t tested it, but I believe it’s running into an issue because it’s trying to call data that isn’t there, and it’s waiting for it.
If you want more information of the serial port object, check out the MSDN. Mono was made to be pretty much the exact same as .net languages, so for the most part (not 100% always), you can use MSDN as a resource for imported commands you don’t know much about.
That’s the link to the serial port command.
You may also want to set the timeout to something like 20ms, that way if it doesn’t work still, the game engine will actually move on, so you aren’t stuck there without data and in the infinite loop. Serial port connections haven’t really been messed with in unity that I’ve seen except the posts by me initially, and unfortunately, I really don’t know a ton about the commands. I kind of make it up as I go.
For instance, now I do a handshaking system. I send a specific character out to the serial port (for instance, I use 1), and the device waits for that command, then gets data and returns it through serial. Then the device simply waits for another 1. This clears up the need for the flush command, and removes the possibility of only getting half of the string. I’ll also note, I return a specific character length (for instance, x,y,z from an accelerometer value would be sent to the pc as 020,152,253. Always leading with a 0 if it’s less then 100, and always leading with 2 zeros if it’s less then 10. Then in your script you just do an if statement. If bytestoread is greater then or equal to 12, run the update code, otherwise, go on.
It’s a simple handshake, and probably not the “proper” way to do it, but it speeds things up and makes it more accurate, because you aren’t filling/flushing the buffer all the time.
I made a different post with all the handshaking, I think… O-o If not, I can probably write an example up fairly quickly with the arduino.
EDIT:
And just saying, but external processes with a visible command prompt bug me. One on the biggest things that bugs me about blender in fact.