Click here to Skip to main content
15,887,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello guys!
this is my brief avr source code :

Objective-C
int main(void)
{
    sei();//Enable global interrupt
    USART_Init(MYUBRR );
    DDRB |=(0<<PINB0)|(1<<PINB1);
    Init_mpu6050(0,0,0,2000,16,off,0);
    ACCEL_Sensitivity_set(16);
    GYRO_Sensitivity_set(2000);
    char buff[500]; //important : attention to the size of buffer and sure its enough.
    stdout = &mystdout;
    while(1){
        ACCEL_in_radian();
        sprintf(buff,"<%6.3f>,<%6.3f>,<%6.3f>",ACCEL_XOUT,ACCEL_YOUT,ACCEL_ZOUT);
puts(buff);
    }
    }
}


with this code i can get the accelerometer float data in 3 axis and convert them to string and save to buff[0,1,...]

and my question is how can i send this string Burst then i cut any data one by one in c# ?
i study somthing about getbytes method in MSDN but it doesnt help me about this !
i think i must send bytes one by one somthing like this :
Objective-C
while(1){
    ACCEL_in_radian();
    sprintf(buff,"<%6.3f>,<%6.3f>,<%6.3f>",ACCEL_XOUT,ACCEL_YOUT,ACCEL_ZOUT);
    for (int x=0;x<8;x++){
    printf("%c",buff[x]);
}

}
is this the standard way to send them ?
its only for 1 axis for others i must make 2 for loop again ... !
then with one sign like "<" i receive bytes and with ">" stop receiving ! :-?
is there any way to send data In ASCII ? How can i receive and separate the axis data singly and one by one in c#?
thx a lot.
Posted

You should decide on a format that works for you.
The first choice you have is to decide between a binary or ASCII protocol.
Both have their pros and cons, but simply put

  • ASCII is more readable, easier to debug and good if you want to hook your device up with HyperTerminal.
    It is a bit more difficult to create a parser for a text based protocol, though.
  • A binary protocol is more compact, good if you need to transfer large BLOBs, such as images, and easier to parse.
    It requires a well defined spec and for small data transfers the overhead can be large comapred to the data.


In your case where you seem to only receive data from your device and the data is numeric, I would go for an ASCII protocol.

This is an example built on your data. If I understand correctly you have three axis with three values each.

Axis start[ (left square bracket)I used this character due to HTML formatting.
Axis end] (right square bracket)
Value separator, (comma)
End of line char\n (LineFeed)This is the default value for the NewLine property in SerialPort



[10.333,11.444,12.555][10.333,11.444,12.555][10.333,11.444,12.555]\n


Sending data:
C++
ACCEL_in_radian();
printf("[%6.3f,%6.3f,%6.3f]", ACCEL_XOUT, ACCEL_YOUT, ACCEL_ZOUT); // First axis
printf("[%6.3f,%6.3f,%6.3f]", ACCEL_XOUT, ACCEL_YOUT, ACCEL_ZOUT); // Second axis
printf("[%6.3f,%6.3f,%6.3f]", ACCEL_XOUT, ACCEL_YOUT, ACCEL_ZOUT); // Third axis
printf('\n');

Not sure how your micro controller works. Can you print a whole string?


On the c# side you can now use the method ReadLine in order to get all data in one chunk.

For interpreting your received string you can can use this regular expression.
C#
Regex protocolExpression = new Regex(@"\[(?<x>\d+\.\d+),(?<y>\d+\.\d+),(?<z>\d+\.\d+)\]");</z></y></x>


C#
// Set this variable on the main thread
bool stopReading = false;

// Do this part inside a child thread
string strRecieve = "";
double x = 0.0;
double y = 0.0;
double z = 0.0;

while (!stopReading)
{
    strRecieve = serialPort1.ReadLine();
    
    MatchCollection mc = protocolExpression.Matches(strRecieve);
    foreach (Match m in mc)
    {
        // Get data for each axis one by one
        x = double.Parse(m.Groups["x"].Captures[0].Value);
        y = double.Parse(m.Groups["y"].Captures[0].Value);
        z = double.Parse(m.Groups["z"].Captures[0].Value);

        // Present data to the user
    }
}
 
Share this answer
 
v4
Getting the data in C# is simple: just set up a SerialPort class instance, and handle the DataRecieved event. In that, you can fetch data byte-by-byte or as a group, and do with it what you will.
Alternatively, set up a background worker thread to read data byte-by-byte from the SerialPort by using the ReadByte command - which will lock the thread until a byte is available. You can do what you like with the data from there on.


"thx for your reply ! i know how to work with serialport and i can receive data ! my problem is how can i separate data for any axis ?"


If you just assemble it into a string as a complete message (which you would need to do to make sure you have the whole message and not just a fragment) you can just pass it through a regex which will "pull out" the bits you need.
<(?<X>\d+(\.\d+)?)>,<(?<Y>\d+(\.\d+)?)>,<(?<Z>\d+(\.\d)?)>
Should do it.
 
Share this answer
 
v2
Comments
Member 11075997 15-Sep-14 14:44pm    
thx for your reply ! i know how to work with serialport and i can receive data ! my problem is how can i separate data for any axis ?
OriginalGriff 15-Sep-14 14:58pm    
Answer updated.
Member 11075997 15-Sep-14 15:16pm    
unfortunately my accel data is Change in time very fast and i dont know receive what number of bytes its gonna be like 12.234... 123... -1234... its what i receive in my serial terminal and when i do it :

int strRecieve = serialPort1.ReadByte();

it receive anything exist in serial port ! i use this while loop for separate but its crashhed :

while(strReceive != ">")
{
strRecieve = serialPort1.ReadByte();
}

"": i use this ">" for knows when i Reach to the end of my axis data !
is this a good way for what i want ?
im really confused !
i find something like union method but it so complex ! i dont get it what i must do :(
http://stackoverflow.com/questions/8827649/fastest-way-to-convert-int-to-4-bytes-in-c-sharp

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900