Help with UART DataReceived

Yeah… I checked my code and made some tweaks in the way I using threads with UARTs and I am now also seeing the same 10X.

What am I doing?

I am working(slowly) on a new buss concept for sensors.

To test, I am using six UARTs on a portal, wired in series. TX->RX->TX… with final UART wired back to the first. Basically a ring configuration.

I send a 256 message around the loop, and measure how long it take to get back to the first UART.

With events, running the UARTs at 2,000,000HZ I get a round trip of ~55ms. With threads the roundtrip is ~5ms.

1 Like

Can anyone give some example code on using uart with a thread?

Check out the UART tutorial in documentation area of the GHI website.

Omg, did you just write that? :face_with_raised_eyebrow:
Please tell me where in docs is uart with a thread

No, it was a fake me… :face_with_symbols_over_mouth:

A link would help. UART

It is easy to find anyway

Seriously? this is getting childish…

Not sure if you are directing this at me to Mike but everything you need us easy to find. We are here to help in you can’t find it.

For example

Docs->TinyCLR->tutorials->bus->uart

If you meant Mike, I am sure he is just being playful since you are looking for something that is easy to find. Anyway, we are here to help.

I am not sure what you want.

The documentation shows you how to start a thread. The documentation shows you how to read a comm port without events.

Put the comm reading code into a thread, or into the program’s main method, which is a thread.

Perhaps you want me to copy the code from the tutorials and post it for you?

As far as being childish… I am not. My wife had me tested.

5 Likes

Here is the documentation for starting a thread
Multithreading (ghielectronics.com)

The thing you will need to be mindful of when you read the data in a separate thread is to ensure that you correctly synchronize access to shared data.

1 Like

Please do not point me to docs, the amount of time ive spend there i practically memorized all the pages.

How do i read when the data is received in the thread?
do i just receive all the time?
with a thread.sleep?
without a thread.sleep?
how do i know when all data is received?
when do i consume the data? upon when DataReceived is fired?

i need some sample code how to receive full data (not in chunks) so i can finally properly do with data what i want and add functionality to my app, and not invent low level boilerplate for UART

do not point me to docs, what i need is not in the docs, or samples, or drivers, or demo apps, ive looked at them all, thoroughly

As per documentation:

    //var txBuffer = new byte[] { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46 }; //A, B, C, D, E, F
    var rxBuffer = new byte[10];

    var myUart = UartController.FromName(SC20100.UartPort.Uart6);

    var uartSetting = new UartSetting()
    {
        BaudRate = 9600,
        DataBits = 8,
        Parity = UartParity.None,
        StopBits = UartStopBitCount.One,
        Handshaking = UartHandshake.None,
    };

    myUart.SetActiveSettings(uartSetting);

    myUart.Enable();
    //myUart.Write(txBuffer, 0, txBuffer.Length);

    while (true)
    {
        if (myUart.BytesToRead > 0)
        {
            var bytesReceived = myUart.Read(rxBuffer, 0, myUart.BytesToRead);
            Debug.WriteLine(BitConverter.ToString(rxBuffer, 0, bytesReceived));
        }

        Thread.Sleep(20);
    }

This is the the output:

AA-55-1A-01-00-00-00-00-00-32
AA-55
1B-01-00-00-00-00-00-32
AA-55-1C-01-00-00-00-00-00-32
AA-55-1D-01-00-00-00-00-00-32
AA-55-1E
01-00-00-00-00-00-32
AA-55-1F-01-00-00-00-00-00-32
AA-55-20-01-00-00-00-00-00-32
AA-55-21-01-00-00
00-00-00-32
AA-55-22-01-00-00-00-00-00-32
AA
55-23-01-00-00-00-00-00-32
AA-55-24-01-00-00-00-00-00-32
AA-55-25-01-00-00-00-00-00-32
AA-55
26-01-00-00-00-00-00-32
AA-55-27-01-00-00-00-00-00-32
AA-55-28-01-00-00-00-00-00-32
AA-55-29
01-00-00-00-00-00-32
AA-55-2A-01-00-00-00-00-00-32
AA-55-2B-01-00-00-00-00-00-32
AA-55-2C-01
00-00-00-00-00-32
AA-55-2D-01-00-00-00-00-00-32
AA-55-2E-01-00-00-00-00-00-32
AA-55-2F-01-00-00
00-00-00-32
AA-55-30-01-00-00-00-00-00-32
AA-55
31-01-00-00-00-00-00-32
AA-55-32-01-00-00-00-00-00-32

Each line should be like the 1st line.
I am asking for help how to get that.

Then the problem was in the question. It was not clear on what you need.

I am double checking one with with @Dat_Tran and will get back to you

Why would it be?! UART is a stream of data. You may get one byte in the first read, 453 in the second and 28 in the third. You will never get the exact count of data in every read. What you need is a marker in the stream to tell you where each chunk starts and where it ends.

I have a marker, 0xAA is constant first byte, and its always exactly 10 bytes

Your buffer is 10 bytes but you always read what is available, which maybe more than 10! I am surprised this even worked for you.

Do you have ground connected to the other side? What is in the other side?

It is working according to documentation. You are the problem.

You are expecting the entire message to be received with one read. This is a false assumption. You have to receive the data from the UART and assemble each message. The number of reads or events necessary for each message is unknown. It can vary from one to the number of bytes in message.

Treat the data stream from a UART the same as a data stream from a TCP socket.

And you say it is must be the same string. I clearly see in incrementing number!! Is it the same or there is an incrementing number?!

I am still not seeing the problem

@Mike, yes i know how it functions, as we discussed earlier in the post, datareceived fires maximum 50 times at 20ms cycles, i know that. I just don’t know how to properly assemble the data

@Gus, of course its not the same byte array what would be the point if its the same?

I need it to come in like this:

AA-55-1A-01-00-00-00-00-00-32 - receive 1 -> process data
AA-55-1B-01-00-00-00-00-00-32 - receive 2 -> process data
AA-55-1C-01-00-00-00-00-00-32 - receive 3 -> process data
AA-55-1D-01-00-00-00-00-00-32 - receive 4 -> process data
AA-55-1E-01-00-00-00-00-00-32 - receive 5 -> process data
AA-55-1F-01-00-00-00-00-00-32 - receive 6 -> process data
AA-55-20-01-00-00-00-00-00-32 - receive N -> process data

the other data 00-00-00-00-00 also changes all the time, its just not shown here, what is constant is first byte AA

You still never see what you are asking for. Assume a continuous stream and break it at AA, your marker