Serial comm

This is the technical support forum for users.

Serial comm

Postby hartoksi » Mon Feb 06, 2012 9:44 pm

In my project with Moacon, I need to send a value (say 12.345) from the PC to Moacon and store it in a variable (float or double).

There is no problem about the communication issues but the problem is I do not know how can I put the bytes together into a variable.

because I am sending the bytes to Moacon in order (first "1" and "2" and "3" and "4" and "5"). each bytes stores in an char type variable. But I need to collect them in a float variable to represent the coreect value. What is the way? I know this is a general C lesson but I would be appreciated if hyou can give me information.
hartoksi
SuperDuper
 
Posts: 80
Joined: Mon Jul 04, 2011 2:11 pm
Location: Turkey

Re: Serial comm

Postby Mike » Mon Feb 06, 2012 10:10 pm

If you are transmitting the number as ASCII chars, put them in a char array and then use the atof function to parse the string to a float. To do this, you will need to #include <stdlib.h>. Here's a quick example.

Code: Select all
#include "moacon500.h"
#include <stdlib.h>

void cmain(void)
{
   const char* str_float = "123.45";
   float f = atof(str_float);
   
   while(1)
   {
      printf("%3.2f\r\n", f);
      delay(100);
   }
}


If you get linker errors, try to add the file C:\Program Files (x86)\ComfileTools\MoaconStudio\$CFC files\ARM\io_putchar\syscalls.c to your project using the menu Project --> Add Files to Project...
Mike
Mike
SuperDuperDuper
 
Posts: 374
Joined: Thu Mar 17, 2011 3:54 pm
Location: Seoul, South Korea

Re: Serial comm

Postby hartoksi » Tue Feb 07, 2012 2:56 am

Mike wrote:If you are transmitting the number as ASCII chars,


Is there another way to send a value directly to a Moacon's variable?

On the other hand, Yes I've got a linker error and add the file you indicated but error remained same. See attached screenshot.

When I have added the syscall.c into my project, a new branch has been added to the left-top treewiev but it was without name.::) See attached screenshot also.
Attachments
Linkererror.JPG
Linkererror.JPG (141.43 KiB) Viewed 663 times
hartoksi
SuperDuper
 
Posts: 80
Joined: Mon Jul 04, 2011 2:11 pm
Location: Turkey

Re: Serial comm

Postby Mike » Tue Feb 07, 2012 4:26 pm

Well, I don't know why syscalls.c shows up blank in your project tree. That's pretty weird. Are you adding it from the menu with Project --> Add Files to Project?

Anyway if you want, you can send the number as binary instead of ASCII. This code shows a floating point number displayed byte-by-byte. Then a byte array is created from those bytes and casted back to a floating point number. You can use a technique like this to send the float's bytes over the serial line and then cast the bytes as a float when you receive them.

Code: Select all
#include "moacon500.h"

void cmain(void)
{
   //Declare a floating point number
   float f = 123.45;
   
   //Print out the floating point's binary representation
   u8 *fAdr = &f;
   printf("%#x %#x %#x %#x\r\n", *fAdr, *(fAdr+1), *(fAdr+2), *(fAdr+3));
   
   //Recreate a float from it's binary representation
   u8 floatBytes[] = {*fAdr, *(fAdr+1), *(fAdr+2), *(fAdr+3)};
   float *fFromBytes = (float *)floatBytes;
   
   //Verify it's the same float
   printf("%3.2f", *fFromBytes);
}
Mike
Mike
SuperDuperDuper
 
Posts: 374
Joined: Thu Mar 17, 2011 3:54 pm
Location: Seoul, South Korea

Re: Serial comm

Postby hartoksi » Thu Feb 09, 2012 4:45 pm

Mike wrote:Well, I don't know why syscalls.c shows up blank in your project tree. That's pretty weird. Are you adding it from the menu with Project --> Add Files to Project?


I have found the reason. My Windows is in Turkish and camically the standard directory names of the path to system files (like program files, users, etc) has been converted to Turkish and so some of them includes Turkish characters. And Moacon Studio can not accept any Turkish character in the path.

I have copied the file into my project directory and also moved my project directory to root (c\>) and the problem solved. I hope you are thinking to overcome this problem in the next version.
hartoksi
SuperDuper
 
Posts: 80
Joined: Mon Jul 04, 2011 2:11 pm
Location: Turkey

Re: Serial comm

Postby Mike » Sun Feb 12, 2012 8:53 pm

Ok, Thank you for bringing this to our attention. I have notified our developers.
Mike
Mike
SuperDuperDuper
 
Posts: 374
Joined: Thu Mar 17, 2011 3:54 pm
Location: Seoul, South Korea


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot] and 1 guest