使用TIVA C系列单片机从发送数据到电脑 [英] sending data from microcontroller to the computer using tiva series c

查看:193
本文介绍了使用TIVA C系列单片机从发送数据到电脑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用全凭静脉麻醉C系列TM4C123GH6PM与code Composer Studio的从另一个同类型的微控制器获取数据。好吧,我得到字节的数据。项目的目标是组装每8个字节,并将其转换为加倍然后把这些双到计算机。我必须使用USART将数据发送到计算机。

I am using Tiva C Series TM4C123GH6PM with Code Composer Studio for getting data from another microcontroller of the same type. Well, I get the data in bytes. The Goal of project is to assemble each 8 bytes and convert them to double then send these double to the computer. I have to use USART to send the data to the computer.

当我使用USART,我有两个方法:

When I use USART, I have two Methods:


    UARTCharGet(UART1_BASE)
  1. 一个从USART_x获取数据
  2. 其他超过USART_x发送的数据:UARTCharPut(UART0_BASE,D)

问题在于有两个方法只接受字符。因此,使用这些方法对双重是不可能的,因为双为8个字节和一个字符是一个字节。我想送双打到我的电脑。任何想法?

The problem consists in having two methods that accept only characters. So using these methods for double is impossible since a double is 8 bytes and a character is one byte. I am trying to send doubles to my computer. Any idea?

的第二个问题是写入从计算机的侧面的程序,以从微控制器获取数据(字节)。

The second Problem is to write a program from the side of the computer to get the data (bytes) from the microcontroller.

我应该写在C此程序(另一种方法是使用MATLAB,因为我会用这些数据进行模拟)?
如何访问COM口在C (或在MATLAB如果可能的话)?

Should I write this program in c (an alternative is using matlab because I will use these data for simulation)? How can I access com-Port in c (or in matlab if it is possible)?

推荐答案

一般来说,你所面临的系列化此问题,最好的办法使用图书馆专门用于这一目的。

Generally speaking you are facing here serialization problem and the best approach would be using specialized library for this purpose.

在一个肮脏的,但简单的方法就可以解决这样的。 您必须正确刚刚组装你的数据在另一边回一倍。

In a dirty but simple way you can solve it like that. You have to just properly assemble your data on the other side back to double.

typedef union _MyDouble {
    double d;
    unsigned char bytes[sizeof(double)];
} MyDouble;


MyDouble my_double;
my_double.d = 1.234;

for(i=0; i < sizeof(double); i++) {
    UARTCharPut(UART0_BASE, (char)my_double.bytes[i]).
}

另一件事是,如果你使用的UART你应该封装这个联盟为一些框架例如为了成功地重新组装这在另一边。

The other thing is that if you are using UART you should encapsulate this union into some frame e.g. in order to successfully reassemble this on the other side.

 _____________ ________ __________ ___________ ________
|             |        |          |           |        |
| FRAME_BEGIN | LENGTH | MyDouble | FRAME_END | CRC-8? |
|_____________|________|__________|___________|________|

接下来的事情是,你应该执行这个结构是由编译器指令进行字节对齐。它可以确保你的结构有没有填充字节等,并分配最低要求的尺寸。这在一些二进制对象将串行接口上​​传输的嵌入式应用使用情况非常普遍 - 这正是你的情况。这样一个简单的解决方案可能会在特定的硬件+软件的配置工作,但可以肯定它不是从专业角度便携和良好的解决方案。

The next thing is that you should probably enforce byte alignment on this struct which is done by the compiler directives. It ensures that your struct has no padding bytes etc. and allocates minimum requires size. This is very popular in embedded applications use-cases where some binary object is going to be transmitted on the serial interface - which is exactly your case. Such a simple solution might work on specific hw+sw configuration but for sure it is not portable and good solution from professional point of view.

这篇关于使用TIVA C系列单片机从发送数据到电脑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆