Arduino Uno Not Writing To Serial

Hi,

I've been experimenting with the Arduino for a while now and now want to communicate with Unity3D to make it control games that I have made in the past. The way I am trying to do this is by writing two bytes of information to serial (one for the direction the player should move in, and one for the speed the player should move at). This worked perfectly until, without changing anything, the Arduino was no longer writing to serial (by the way, the TX light on the Arduino was also not on). I wrote the Arduino code on my laptop (Windows 10), then plugged the Arduino into my PC (Windows 7) which had Unity on it, which worked perfectly until the Arduino stopped writing to serial after me just unplugging the Arduino for a couple of minutes, then plugging it back in (to the same USB port). When I plug the Arduino back into my laptop, it is writing to serial as it should, and the TX light is on. Do I need to do something on my PC at all? Perhaps change some of the port settings in device properties, or download some driver?

Here is my code on the Arduino:

int upPin = 2;
int rightPin = 3;
int downPin = 4;
int leftPin = 5;
int speedPin = A0;
int currentDirection;

void setup () {
  Serial.begin (9600);
}

void loop () {
  if (Serial.available ()) {
    if (Serial.read () == 'R') {
      currentDirection = 0;
    }
  }
  if (digitalRead (upPin) == HIGH) {
    currentDirection = 0;
  } else if (digitalRead (rightPin) == HIGH) {
    currentDirection = 1;
  } else if (digitalRead (downPin) == HIGH) {
    currentDirection = 2;
  } else if (digitalRead (leftPin) == HIGH) {
    currentDirection = 3;
  }
  Serial.write (currentDirection);
  Serial.write (analogRead (speedPin) / 4);
  delay (30);
}

and here is the code in Unity C# (not really needed since nothing is being written to the serial at all, but here is is):

using UnityEngine;
using System.Collections;
using System.IO.Ports;

public class Player : MonoBehaviour {
string portName = "COM3";
SerialPort serial;
int inByte1;
int inByte2;
enum Direction {Up, Right, Down, Left};
Directions direction = Directions.Up;
float moveSpeed;
[SerializeField] float movespeedMultiplyer;

void Start () {
serial = new SerialPort (portName, 9600);
serial.ReadTimeout = 50;
serial.Open ();
serial.Write (new char[1] { 'R' }, 0, 1);
serial.BaseStream.Flush ();
}

void Update () {
if (serial.BaseStream.CanRead) {
inByte1 = serial.ReadByte ();
inByte2 = serial.ReadByte ();
moveSpeed = inByte1 * moveSpeedMultiplyer;
switch (inByte2) {
case 0:
direction = Directions.Up;
break;
case 1:
direction = Directions.Right;
break;
case 2:
direction = Directions.Down;
break;
case 3:
direction = Directions.Left;
break;
}
}
}
}

I would appreciate any help,
Rob.

I doubt your code does not start writing to the serial port after reconnecting. The TX light is controlled by the 16U2 micro that connects to the pc. If that micro does not 'see' the pc, it will not send data through and hence the TX led will not flash.

Check the device manager on the pc after reconnecting, does it show the Uno?. Did you restart your unity app after reconnecting?

I suspect you need to study the difference between Serial.write() and Serial.print()

Maybe write a short program that uses both to send data to the Serial Monitor.

And then have a look at the examples in Serial Input Basics - simple reliable ways to receive data. The same concepts can be used for a PC program.

…R

sterretje:
I doubt your code does not start writing to the serial port after reconnecting. The TX light is controlled by the 16U2 micro that connects to the pc. If that micro does not 'see' the pc, it will not send data through and hence the TX led will not flash.

Check the device manager on the pc after reconnecting, does it show the Uno?. Did you restart your unity app after reconnecting?

My PC does detect the Arduino and I've checked to make sure that all of the settings match. I've tried restarting Unity, my PC, re-uploading the Arduino code, pretty much everything useful I can think of.

Robin2:
I suspect you need to study the difference between Serial.write() and Serial.print()

Maybe write a short program that uses both to send data to the Serial Monitor.

And then have a look at the examples in Serial Input Basics - simple reliable ways to receive data. The same concepts can be used for a PC program.

...R

I am aware of the differences between Serial.write and Serial.print, and, in this case, I want to write a byte to serial which can be read by Unity later, therefore, Serial.write is what is required. Also, this exact code worked perfectly, so I doubt that is the cause; the Arduino was writing to serial fine, it just didn't after I unplugged it for a while.

Did you re-upload the code using the pc or the laptop. If pc, did it work?

Is the com port still the same on the pc after disconnect/reconnect. Your unity code uses a hard-coded com3.

sterretje:
Did you re-upload the code using the pc or the laptop. If pc, did it work?

Is the com port still the same on the pc after disconnect/reconnect. Your unity code uses a hard-coded com3.

I re-uploaded it with my laptop which has all the code for all of my Arduino projects on, perhaps that is my problem, however, everything worked perfectly with the exact same code and the exact same components on the breadboard, and that baffles me. And yes, it is the exact same port.

Ok, I don't know what happened, but now it's working. I did not change anything other than plugging in and unplugging the arduino a couple of times which seemed to work.