发送数据帧 [英] sending a data frame

查看:75
本文介绍了发送数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好,

我正在尝试开发一个应用程序,允许我创建一个自动化系统。

我对订单有一点问题;操作员将输入一个十进制数,例如100000,它是十六进制0x186A0,所以只需将它分成3部分0x01 0x86 0xA0

你可以帮助我推进我的项目吗?

您将找到附带的程序代码:

good evening,
I am trying to develop an application allows me to create an automation system.
I have a little problem about an order; the operator will input a decimal number eg 100000 which is in hex 0x186A0 so just divide it into 3 parts 0x01 0x86 0xA0
can you help me to advance in my project ?
You will find attached the program code:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <string>        
#include <sstream> 
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	uint16_t res1 = 0xFFFFFF;
	int D = 0;
	uint8_t tab[3];
	printf("donner valeur decimal: ", D);
	cin >> D;
	std::stringstream ss;
	ss << std::hex << D;
	std::string res(ss.str());
	std::cout << res;

	res1 = (uint16_t)res.c_str();
	
	tab[0] = res1 & 0xFF;
	printf("\n val 1: %x", tab[0]);
	tab[1] = (res1 & 0xFF, res1 >> 8) ;
	printf("\n val 2: %x", tab[1]);
	tab[2] = (res1 & 0xFF, res1 >> 16);
	printf("\n val 3: %x", tab[2]);

	return 0;
}

推荐答案

您可以使用 union

You may just use a union:
#include <iostream>
#include <stdint.h>

union IntByte
{
  uint32_t i;
  uint8_t b[4];
};

int main()
{
  IntByte ib;

  std::cout << "donner valeur decimal: ";
  std::cin >> ib.i;

  for (int i=0; i<3; ++i)
  {
    std::cout << "val[" << i << "] = " << std::hex << (uint32_t) ib.b[2-i] << std::endl;
  }
}


这篇关于发送数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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