Search code examples
c#serial-portglobal-variablescircular-buffer

How can I read and calculate serial port with circular buffer c#


I know, someone of you, will give minus to me because of this title. But just listen and help please. If you show me the title same at my title, I will delete this topic. But I don't know how can I find the problem at here. I'm sorry because of my English.

I want to read and do some proccess with my reads from serialport into my circular buffer.

I can read serialport. İt's done at the DataReceived class.

I can set my serialport bytes into my circular buffer which I have downloaded here. But, I can not do any proccess with this buffer. For example, I take bytes from port like that:

0000F9:F0F0003044343A008453240000000F90000F9:F0F0002044789A008098740000000F90000F9:F0F0002114563A008225890000000F90000F9:F0

But I want to see it like that firstly:

0000F9    //discard this, because this data is lacking
:F0F0003044343A008453240000000F90000F9  //take this
:F0F0002044789A008098740000000F90000F9  //take this
:F0F0002114563A008225890000000F90000F9  //take this
:F0     //discard this because of lack

And secondly, I want to parse it like this;

: = start character
F0 = it means start the program
000 = Power of machine 
blablabla

At the end of this section, I want to show theese at my forms. So I want help for theese titles from you. Or anyone can show me the right asked question in this forum.

At first, I don't know how can I do a (global) public the circular buffer at the all of the program. When I define my buffer class at the data_received void like that,

var buffer = CircularBuffer<byte>(256);

I can use the buffer just in to dataReceived void. But I want to use this everywhere at the program. So, when I try to define it top of the my program.cs/main class like this:

var buffer = new CircularBuffer<byte>(256);

It, fails. I do not know how can I define it into my program. This was my first question.

Secondly, If I solve my first question, I must parse and control my buffer. First, I must verify my data stream. My serialport sends me 38 characters at the one data stream. My formulas is simple; every data stream must start with : and, must end with F9. Near the two character of F9 must be equals to sum of the some values of the middle data stream. So, I just want to know this, how can I procces with my circular buffer?

If anyone wants to see my codes, I can mail.

Thanks a lot from now.


Solution

  • Firstly, I changed CircularBuffer class without static;

    Secondly, I defined my buffer as global with this:

    CircularBuffer<byte> buffer = new CircularBuffer<byte>(256);
    

    Thirdly, I read my data stream into my buffer in a loop. I set a control is_full and is_empty in this loop.

    Forthly I create a void which parse and calculate it. And have called into my loop. If anyone wants, I can mail the codes.

    Thank you @Theraot for your help.