I created a WindowsForm with C# in Visual Studio that sends a serial message to my Arduino Uno over the Arduino Com port. In this case it is Com Port 7.
I verified with PuTTy that my C# program is in fact sending real data and is working when using virtual ports.
The strange part is that when i try and send data to my Arduino over Port 7 it does not work. I can open the port and wen i try to send data the rx light on my Arduino flashes but nothing happens.
I have it so that when i send a 1 from my C# program it should light up the on board LED on the Uno.
Here is the weird part
I created a virtually split port of the Arduino port 7. My C# program, the Arduino IDE serial monitor and the Arduino are all connected together on the same port with a software called Virtual Serial Port Tools.
I can see the sent message from my C# program in the Arduino IDE serial monitor and the lights flashes as i wanted. I can send messages from the Arduino IDE in the serial monitor and the light flashes. I can even close the serial monitor and continue to send messages from my C# program and the light flashes. It continues to work until i reset the Arduino and then it seems like the serial no longer works between the Arduino and the C# program.
i tested things even further and found that, disregarding everything i tried above with the virtual ports, that when i open the serial monitor in the IDE i can send a message and the light with flash. i close out of the serial monitor and then try to send a message from my C# program and the serial does not seem to work on the Arduino and the light does not flash.
i have no idea what to do now.
Here is my Arduino code:
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
Serial.print(incomingByte);
if (incomingByte == '1')
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(2000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(2000);
}
}
}
Each time your PC program opens it's serial connection, the Arduino is reset. You need to wait for all that to happen and the new serial connection to be established. So add a delay before the pinMode(...); in your code. See if that helps.
Hello,
I tried your solution without success.
After more testing it almost seems as though Arduino IDE serial monitor initializes or "wakes up" my Uno when it connects that my C# program is not doing. It's like it hears my serial messages by flashing the rx light but it isn't actually listening to what my C# program is saying.
the C# code is very simple:
if (serialPort1.IsOpen)
{
dataOUT = textBoxDATAOUT.Text;
serialPort1.Write(dataOUT);
}
it takes the text from a text box and sends it over serial.
there is other code that handles port, parity, baud and etc that i tested to be working with PuTTy.
I can not help. Below some miscellaneous thoughts.
My first question is why you need two applications to communicate with the Arduino? If it's for debugging I would suggest that you add some functionality to your C# application for that.
And after that you removed your Virtual Serial Port Tools setup and tested with your C# application only and it did work / did not work?
Is your C# code hardened and provide feedback about the success of opening the port and the success of sending. You would not be the first one that puts everything in a try/catch and does not display an error message to the user if it fails (no offence intended).
In the below you do have some hardening.
Do you have an else
with that that tells you that the port is not open? Did you put a breakpoint on the if
and single step to check if the Write
is actual executed.
Do you still see the Rx light on the board flash when you send data from your C# application under this condition?
Play with the DtrEnable (SerialPort.DtrEnable Property (System.IO.Ports) | Microsoft Learn) and RtsEnable in your C# application. The DTR signal is used on the Uno to reset the 328P; opening the serial monitor (or performing an upload) will assert that signal; I suspect that your application does not.
It however should not have an effect on the communication.
Note
Not familiar with Virtual Serial Port Tools.
Add a ',HEX' In you serial print ( maybe you are receiving some unprintable chars ).
Also add a serial print of how many chars availeble/received.
Serial.print(incomingByte,HEX);
Just thought I'd follow up to close this issue out. It turns out that I was accidentally setting the baud rate twice in my C# program due to a copy paste error. I was setting the baud rate but in the following line of code I was writing the data bits to the baud rate as well causing all sorts of issues.
31 is hex for 49 decimal which is ascii '1'.