在设备/卡上写入数据 [英] write data on device/card

查看:76
本文介绍了在设备/卡上写入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始挣扎着我认为不会那么复杂的事情,所以我真的很感激一些建议。我会告诉你我的情况。



我将数据写入设备/卡。写作过程很简单,通常如下:



I started struggling with something I didn't think would be that complicated, so I would really appreciate some advice. I will tell you my situation.

I write data to a device/card. The writing procedure is easy and goes usually like this:

unsigned char sendBuffer[32];

sendBuffer[0] = headerInfo;
sendBuffer[1] = headerInfo;
sendBuffer[2] = data[0];
sendBuffer[3] = data[1];
...
sendBuffer[17] = data[15];





数据通常是16字节因此,存储很容易,我只需要调用: SendData(sendBuffer,length),并完成,存储数据。



1)我的问题是我想在卡片上存储整数517 - 我应该将哪些数据发送到设备?如何将其嵌入 sendBuffer 数组?

2)同样。假设我想将字符串Hello world xyz发送到设备。如何将其嵌入 sendBuffer



一些亮点。我是接收器端。我将回读数据。而且根据我存储的位置,我可能事先知道它是int还是字符串。

我也没有字节序问题。



示例

---------

下面我将提供一个工作示例,它设法写入和读取16个字节的HEX字符串并希望这样做帮助你更好地回答我的问题。



我想写这个字符串: 00000000000000000000AABBEEAABBEE



我们将其转换为字节数组:





The data I write is usually 16 bytes, or multiple of 16. So, storing is easy, I just call: SendData(sendBuffer, length), and done, data is stored.

1) My question is say I want to store now integer 517 on the card - what data shall I send to the device? How to embed it in the sendBuffer array?
2) Similarly. Say I want to send string "Hello world xyz" to the device. How to embed it in the sendBuffer?

Some highlights. I am the receiver end. I will be reading the data back. And also depending where I stored it I may in advance know whether it is an int or string.
I also don't have endianness issues.

Example
---------
Below I will just provide working example which manages to write and read 16 byte HEX strings and hope this will be helpful to you to answer my questions better.

Say I want to write this string: "00000000000000000000AABBEEAABBEE"

We convert it to byte array:

for(int i=0,m=0; i < size; i+=2,m++)
{
        char ch1, ch2;
        ch1=(char)str[i]; // str is our hex string
        ch2=(char)str[i+1];
        int dig1, dig2;
        if(isdigit(ch1)) dig1 = ch1 - '0';
        else if(ch1>='A' && ch1<='F') dig1 = ch1 - 'A' + 10;
        else if(ch1>='a' && ch1<='f') dig1 = ch1 - 'a' + 10;
        if(isdigit(ch2)) dig2 = ch2 - '0';
        else if(ch2>='A' && ch2<='F') dig2 = ch2 - 'A' + 10;
        else if(ch2>='a' && ch2<='f') dig2 = ch2 - 'a' + 10;
       
 array1[m] = (char)(dig1*16 + dig2); // output byte array
}





现在,我们将此字节数组 array1 复制到 sendBuffer ,如上所示例如,





Now, we copy this byte array array1 to the sendBuffer as above, e.g.,

for(int i=0;i<16;i++)
     sendBuff[i]=array1[i];





如前所述,我们调用发送命令。



阅读也很简单,我打电话:接收(receiveBuffer)现在 receiveBuffer 包含我之前写的字节数组。然后我需要将它转换回十六进制,如下所示:





and done we call send command as I mentioned before.

Reading is also easy, I call: Receive(receiveBuffer) and now receiveBuffer contains the byte array I wrote previously. Then I need to convert it back to hex like this:

for(k=0;k<16;k++)
 {
      data[k*2]=hexval[((receiveBuffer[k]>>4)&0xF)];
      data[(k*2)+1]=hexval[receiveBuffer[k]&0x0F];
 }





完成后,数据现在包含我的初始十六进制string:00000000000000000000AABBEEAABBEE。



And done, data now contains my initial hex string: "00000000000000000000AABBEEAABBEE".

推荐答案

嵌入整数或(短)字符串很简单。

你可以使用4(或2) )用于存储每个整数的字节。假设你选择4个字节的整数,那么

Embedding a integer or a (short) string is simple.
You may use 4 (or 2) bytes for storing each integer. Suppose you choose 4 bytes an integer, then
int k = whatever; // assuming 32-bit integers
sendbuffer[2] = (unsigned char)(k >> 24);
sendbuffer[3] = (unsigned char)(k >> 16);
sendbuffer[4] = (unsigned char)(k >> 8);
sendbuffer[5] = (unsigned char) k;





虽然是一个简单的字符串



While for a simple string

const char * s = "hello";
int n;
// TODO: check if it fits
for (n=0; n=strlen(s); ++n) 
{
  sendbuffer[2+n] = s[n];
}
s[n] = '\0';





当然你必须以某种方式指明什么 sendbuffer 包含(我猜想你可以使用headerinfo字节)。



Of course you have to specify somehow what sendbuffer contains (you could use the headerinfo bytes for the purpose, I suppose).


这篇关于在设备/卡上写入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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