Go Down

Topic: C# Console seems to send 0xF0 to Arduino on Serial port opening (Read 734 times) previous topic - next topic

docdockstader

For some reason... my c# console app seems to send a series of bytes set to 0xF0 when opening the arduino serial port.  Is this expected?  I've also tried opening the port and then checking for data.  It does seem to send/receive less bytes of 0xF0, but still does send one or two every time.

Arduino code (simply returns what is recieved):

Code: [Select]

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

void loop() {
  // put your main code here, to run repeatedly:
}

void serialEvent() {
  int len = Serial.available();
  byte data[len];
  Serial.readBytes(data, len);
  Serial.write(data, len);
}


c# code (simply opens the port and wires up a data received method:

Code: [Select]

static void Main(string[] args)
{
    com = new SerialPort("COM6");
    com.BaudRate = 115200;
    com.DataBits = 8;
    com.StopBits = StopBits.One;
    com.Parity = Parity.None;
    com.DataReceived += onDataReceived;
    com.Open();

    while (true) { }
}

private static void onDataReceived(object sender, SerialDataReceivedEventArgs e)
{
    while (com.BytesToRead > 0)
    {
        int data = com.ReadByte();
        Console.WriteLine(string.Format("Data Received: 0x{0:X2}", data));
    }
}


The output on the console is always:

Data Received: 0xF0
Data Received: 0xF0
Data Received: 0xF0
Data Received: 0xF0

docdockstader

Another peculiar thing on this project is if I update setup() to the following, the 0x05 is not received unless I open the port, and then manually press the reset button on the Uno board.

Code: [Select]

void setup() {
  Serial.begin(115200);
  Serial.write(0x05);
}

Robin2

When a PC program opens the Serial port it causes the Arduino (assuming an Uno or Mega or nano) to reset. When I write PC programs I get them to wait for a message from the Arduino before the PC tries to send any data to the Arduino. I have a line like
Code: [Select]
Serial.println("Arduino is Ready");
in setup()

This Simple Python - Arduino demo illustrates the process. You can implement the equivalent to the Python code in any PC language.



Separately, your Arduino code to receive data is not robust - there is no guarantee that when a call is made to Serial.available() the full message will be available.

Also, I would not be comfortable with dynamically creating the char array - it could cause problems in the small memory of an Arduino.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)
Code: [Select]
Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker



...R
Two or three hours spent thinking and reading documentation solves most programming problems.

docdockstader

Thanks for the reply Robin!  Did you see my second post?  I added a Serial.write to setup(), but it's never received by the c# console app unless I manually press the reset button on the Uno board...

I definitely agree with your second point.  The code I posted is a very short test to demonstrate the issue by itself. 

I'd be thrilled if the c# console app simply waited for the Serial.write(0x05);... but it's never received without a manual reset of the board. 

Robin2

Thanks for the reply Robin!  Did you see my second post?
I did, but it did not make much sense to me.

I am not familiar with C# (it's years since I used Windows). Does your C# program have code that specifically waits for a message from the Arduino before proceeding? (The equivalent of my Python waitForArduino() function).

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

ieee488

He does not have a specific wait time per se.

He has an event handler which "fires" when something is received on the serial port.
That event handler appears to be receiving 0xf0 (the letter 'p').



Robin2

He does not have a specific wait time per se.
IMHO he should have one for the initial "ready" message from the Arduino

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

sumguy

A change in the DTR signal will give you OxF0.

If you add the following two lines around the com.Open it should reset the arduino then open the port without the values you are seeing.

Code: [Select]
            com.DtrEnable = true;
            com.Open();
            com.ReadExisting();

docdockstader

com.DtrEnable = true; was all that was needed.  It now works perfectly!  Thank you!  I can now open the port, wait until 0x05 is received, and then wire up the data received event.  Awesome!

ieee488

A change in the DTR signal will give you OxF0.

If you add the following two lines around the com.Open it should reset the arduino then open the port without the values you are seeing.

Code: [Select]
            com.DtrEnable = true;
            com.Open();
            com.ReadExisting();

We don't do anything when communicating with Arduino Mega with regards to DTR. We use VB.NET

I know that the OP reports that setting DTR Enable to true works but why is it necessary?

.

sumguy

It is necessary because the OP's test code has no protocol to eliminate erroneous data, that's basically what Robin2 was trying to explain. You say you don't do anything ieee488 but I am guessing you have a protocol of some kind. Setting DTR true is not a bad option at times anyway, it clears the serial buffers and syncs the PC and arduino to a clean start.

Hal_Posoyan

Going off topic a little, but can someone explain to me please where DTR fits in a communications system like USB which as far as I know has only the 2x data lines + and -. DTR was a line in an RS232 bundle, and there is no such wire in USB.

Is DTR implemented in software as a sort of virtual line in USB?


ieee488

It is necessary because the OP's test code has no protocol to eliminate erroneous data, that's basically what Robin2 was trying to explain. You say you don't do anything ieee488 but I am guessing you have a protocol of some kind. Setting DTR true is not a bad option at times anyway, it clears the serial buffers and syncs the PC and arduino to a clean start.
Thank you for the explanation, but I must admit I still don't understand why setting DTR to true was necessary for the OP.

The PC setting DTR to true before the COM port is opened causes the Arduino not to send 0x0f ; otherwise, it does ?


.


docdockstader

The PC setting DTR to true before the COM port is opened causes the Arduino not to send 0x0f ; otherwise, it does ?
The Arduino isn't sending the 0x0F, the PC is.  The code written to the Arduino simply sends back whatever it receives.  If I remove that code, nothing is received on the PC when the serial port is opened.  I'm not sure why the PC sends 0x0F, but I suspect it has something to do with the way .NET has implemented it's serial port libraries.

This became an issue because I wrote some code on the Arduino to handle commands via the serial port.  Every time I opened the port, the Arduino would report several bad commands.  I had previously just sent a communication test to the Arduino and ignored everything received until the test was successful, but that didn't feel like a good way to get the job done.

Not sure if this helps at all... but glancing at the Uno's schematic, it appears the serial hardware is actually an ATMEGA8U2-MU, which has a USB controller.  Pin 13 is labeled as DTR, and connects to the ATMEGA328 reset pin.  I'm guessing it's all software.  The USB tells the ATMEGA8U2-MU that the data terminal is ready, which in turn sends the reset signal to the ATMEGA328.  Complete guess on my part though.

ieee488

The Arduino isn't sending the 0x0F, the PC is.  
You wrote in your very first post
The output on the console is always:

Data Received: 0xF0
Data Received: 0xF0
Data Received: 0xF0
Data Received: 0xF0


and this information is right below the C# code snippets!


So, what you wrote is not what you meant.

I must have missed the part where you wrote that you were echoing the PC.
Stupid me for not reading your Arduino code.

No more bothering with this again!

.

Go Up