问题
So I am working on an internship project.
I have to develop a whole system that will gather some data and will then send it through to a server. The system consist of an MSP430G2553 microcontroller so I am coding in pure C without any library's, so no sprintf or malloc functions I can use.
The problem that I am having at the moment is that I have no idea how I am going to send all the data that is in an array to the server at once.
I have the array ADCvalue[20] with 20 values, ranging from 0-350
ADCvalue[20]= {10, 40, 50, 90, 100, 200, 300, 240, 260, 10, 40, 50, 90, 100, 200, 300, 240, 260, 300, 20}
.
Now I want to send these values all at once with the delimiter "," between every value to the server. Like this:
10,40,50,90,100,200,300,240,260,10,40,50,90,100,200,300,240,260,300,20
How can I get the array like this and send it through at once? I already have a print function that can send for example: char property[] = "property"
as a string.
It would be great if somebody could help me with some psuedo code or some algorithmes to get me started.
回答1:
As you stated not to use any library (for whatever reason), I just wrote a simple writeIntValue
-function. See the following code which uses this function to write a sequence of positive integral values into a character array.
Hope it helps.
char* writeIntValue(int val, char* dest) {
// count digits:
int digits = 0;
int valCopy = val;
while (valCopy) {
digits++;
valCopy /= 10;
}
if (digits == 0) // means val has been 0
digits=1; // assume 1 digit to write the '0'-value
for (int i=digits-1; i>=0; i--) {
dest[i] = val%10 + '0';
val/=10;
}
dest[digits] = '\0'; // write string-terminating 0x0-value
return dest + digits; // return a pointer to the end of the value written so far
}
int main() {
int ADCvalue[20]= {10, 40, 50, 90, 100, 200, 300, 240, 260, 10, 40, 50, 90, 100, 200, 300, 240, 260, 300, 20};
char buffer[500] = { '\0' };
char *dest = buffer;
for (int i=0; i<20; i++) {
if (i>0)
*dest++ = ',';
dest = writeIntValue(ADCvalue[i], dest);
}
// buffer here will be "10,40,50,90,100,200,300,240,260,10,40,50,90,100,200,300,240,260,300,20"
return 0;
}
回答2:
Try this code
const char ansitable[10] = {'0','1','2','3','4','5','6','7','8','9'};
void procInt(char* buf, int* loc, unsigned short v) {
if (v == 0) {
buf[*loc] = '0';
*loc++;
return;
}
unsigned short cv = v;
unsigned short r;
unsigned short divid = 1;
if (v>9) divid = 10;
if (v>99) divid = 100;
while (divid > 0) {
r = cv / divid;
buf[*loc] = ansitable[r];
*loc++;
cv %= divid;
divid /= 10;
}
}
int MAX_SIZE_OF_CHUNCK = 4;
void startWrite(unsigned short* input, int sizeinput) {
char* buffer = (char *)calloc(sizeinput * MAX_SIZE_OF_CHUNCK, sizeof(char));
int loc = 0;
for (int i = 0; i < sizeinput; i++) {
procInt(buffer, &loc, input[i]);
if (i < sizeinput-1) {
buffer[loc] = ',';
loc++;
}
}
sendToMPU(buffer, loc); //loc - length
}
回答3:
How can I get the array like this and send it through at once? I already have a print function that can send for example: char property[] = "property" as a string.
Values in the range [0 ... 350] doesn't fit in a single byte, but you can encode those values using pairs of bytes.
An example
short int x = 300; /* 0x012C */
=
unsigned char arr[] = {0x01, 0x2C};
or
short int x[] = {0x000a, 0x0028, 0x0032, 0x005a, 0x0064, 0x00c8, 0x012c, 0x00f0, 0x0104, 0x000a, 0x0028, 0x0032, 0x005a, 0x0064, 0x00c8, 0x012c, 0x00f0, 0x0104, 0x012c, 0x0014};
=
unsigned char arr[] = {0x00, 0x0a, 0x00, 0x28, 0x00, 0x32, 0x00, 0x5a, 0x00, 0x64, 0x00, 0xc8, 0x01, 0x2c, 0x00, 0xf0, 0x01, 0x04, 0x00, 0x0a, 0x00, 0x28, 0x00, 0x32, 0x00, 0x5a, 0x00, 0x64, 0x00, 0xc8, 0x01, 0x2c, 0x00, 0xf0, 0x01, 0x04, 0x01, 0x2c, 0x00, 0x14};
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
short int ADCvalue[] = {10, 40, 50, 90, 100, 200, 300, 240, 260, 10, 40, 50, 90, 100, 200, 300, 240, 260, 300, 20};
size_t sz = sizeof(ADCvalue) / sizeof(*ADCvalue);
unsigned char *arr = malloc(sz * 2);
if (arr == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
/* Encode */
for (size_t i = 0; i < sz; i++) {
arr[i * 2 + 0] = (unsigned char)(ADCvalue[i] >> 8);
arr[i * 2 + 1] = (unsigned char)(ADCvalue[i] & 0xFF);
}
/* Send array */
/* .. */
/* Decode */
for (size_t i = 0; i < sz; i++) {
printf("%d\n", (arr[i * 2] << 8) | (arr[i * 2 + 1]));
}
return 0;
}
来源:https://stackoverflow.com/questions/43853141/how-to-convert-an-integer-array-to-a-string-without-the-use-of-a-library