I have to send an integer counter as 'num' through XBee (in API mode) from Arduino. For this purpose I have to convert this integer to String and then to uint8_t array as it is needed in API frame to transmit.
Apart from other details I've converted my integer to string like
String str;
int num = 0;
char cstr[16];
void loop() {
num++;
str = String(num);
str.toCharArray(cstr,16);
Serial.println(cstr); // this shows the correct result, means int is converted to String correctly
uint8_t data[] = {cstr}; // passing String to uint8_t array
XBeeAddress64 addr64 = XBeeAddress64();
addr64.setMsb(0x00000000);
addr64.setLsb(0x00000000);
ZBTxRequest zbTx = ZBTxRequest(addr64, data, sizeof(data));
xbee.send(zbTx);
int count = sizeof(data);
for (int i = 0; i < count; i++) {
if (i == (count-1)) {
Serial.print(data[i]); // here it prints "104" of ASCII which is equal to "h"
}
}
}
It also transmit it as like that Serial print i.e. ('104').
Please point out if I am making any mistake and guide me if I didn't do it as required.
First get rid of those cstr and String:
int num = 0;
//char cstr[16];
char data[16] = {'\0'}; // Initialize data array
void setup() {
}
void loop() {
num++;
//str = String(num);
//str.toCharArray(cstr,16);
itoa (num, data, 10);
Serial.println(data);
// To get the number of elements in the array use
uint8_t datalen = strlen(data);
// Black box see lib for details
XBeeAddress64 addr64 = XBeeAddress64();
addr64.setMsb(0x00000000);
addr64.setLsb(0x00000000);
ZBTxRequest zbTx = ZBTxRequest(addr64, data, datalen);
xbee.send(zbTx);
// Print test
Serial.println("Content of data: ");
Serial.print(data);
Serial.print(" datalen: ");
Serial.print(datalen);
}
This should work as you expect. To copy an array you would use strcpy (or memcopy with malloc and free), to get the number of elements use strlen.
EDIT:
Here is a tested code (just copy andpaste and first replace only the include to your xbee lib:
#include "Xbee.h" //-> Change to your xbee lib
uint16_t num = 0;
char xdata[16] = {'\0'};// Initialize data array
char numbuf[16] = {'\0'}; // initialize number buffer
long currentMillis = 0;
void setup() {
Serial.begin(115200);
currentMillis = millis();
}
void loop() {
if (millis() - currentMillis > 1000) { // 1 sec timer to prevent overflow due to hammering xbee with data -> tune to your stable transmission
num++;
itoa (num, numbuf, 10); // Num buffer
strcpy(xdata, numbuf); // Copy content of num buffer to data array
Serial.println(xdata);
// To get the number of elements in the array use
uint16_t datalen = strlen(xdata);
// Black box see lib for details
XBeeAddress64 addr64 = XBeeAddress64();
addr64.setMsb(0x00000000);
addr64.setLsb(0x00000000);
ZBTxRequest zbTx = ZBTxRequest(addr64, data, datalen);
xbee.send(zbTx);
// Print test
Serial.print("Content of xdata: ");
Serial.print(xdata);
Serial.print(" datalen: ");
Serial.println(datalen);
currentMillis = millis(); // reset timer
}
}
The result in serial monitor is:
1
Content of xdata: 1 datalen: 1
2
Content of xdata: 2 datalen: 1
3
Content of xdata: 3 datalen: 1
4
Content of xdata: 4 datalen: 1
....
864
Content of xdata: 864 datalen: 3
865
Content of xdata: 865 datalen: 3
866
Content of xdata: 866 datalen: 3
So all works as expected, no 104 or similar. Please first copy paste - test it - and then your additions
Related
I have stored light sensor values in comb1, as an array in an unsigned int. I am trying to send these values to a microprocessor. The values are taken from a sensor stored in an array. Then I am trying to send the commands through this:
for(int i = 0; i < 6; i++) {
Serial1.print("AT+SEND=488,3,%u\r\n", comb1[i]);
}
The entire code compiles fine until I add this line of code. I receive this error message:
Energia: 1.8.11E23 (Windows 10), Board: "MSP-EXP430FR2355LP"
C:\Users\turne\OneDrive\Documents\Energia\M_Station\M_Station.ino: In function 'void loop()':
M_Station:137:55: error: call of overloaded 'print(const char [19], unsigned int&)' is ambiguous
C:\Users\turne\OneDrive\Documents\Energia\M_Station\M_Station.ino:137:55: note: candidates are:
C:\Users\turne\OneDrive\Documents\energia-1.8.10E23\hardware\energia\msp430\cores\msp430/Print.h:63:12: note: size_t Print::print(unsigned char, int) <near match>
C:\Users\turne\OneDrive\Documents\energia-1.8.10E23\hardware\energia\msp430\cores\msp430/Print.h:63:12: note: no known conversion for argument 1 from 'const char [19]' to 'unsigned char'
C:\Users\turne\OneDrive\Documents\energia-1.8.10E23\hardware\energia\msp430\cores\msp430/Print.h:64:12: note: size_t Print::print(int, int) <near match>
C:\Users\turne\OneDrive\Documents\energia-1.8.10E23\hardware\energia\msp430\cores\msp430/Print.h:64:12: note: no known conversion for argument 1 from 'const char [19]' to 'int'
C:\Users\turne\OneDrive\Documents\energia-1.8.10E23\hardware\energia\msp430\cores\msp430/Print.h:65:12: note: size_t Print::print(unsigned int, int) <near match>
C:\Users\turne\OneDrive\Documents\energia-1.8.10E23\hardware\energia\msp430\cores\msp430/Print.h:65:12: note: no known conversion for argument 1 from 'const char [19]' to 'unsigned int'
C:\Users\turne\OneDrive\Documents\energia-1.8.10E23\hardware\energia\msp430\cores\msp430/Print.h:66:12: note: size_t Print::print(long int, int) <near match>
C:\Users\turne\OneDrive\Documents\energia-1.8.10E23\hardware\energia\msp430\cores\msp430/Print.h:66:12: note: no known conversion for argument 1 from 'const char [19]' to 'long int'
C:\Users\turne\OneDrive\Documents\energia-1.8.10E23\hardware\energia\msp430\cores\msp430/Print.h:67:12: note: size_t Print::print(long unsigned int, int) <near match>
C:\Users\turne\OneDrive\Documents\energia-1.8.10E23\hardware\energia\msp430\cores\msp430/Print.h:67:12: note: no known conversion for argument 1 from 'const char [19]' to 'long unsigned int'
Multiple libraries were found for "Wire.h"
Used: C:\Users\turne\OneDrive\Documents\energia-1.8.10E23\hardware\energia\msp430\libraries\Wire
exit status 1
call of overloaded 'print(const char [19], unsigned int&)' is ambiguous
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
This is my whole code:
#include <Wire.h>
#include <msp430.h>
#define VEML7700 0x10
// digital pin 40 has a pushbutton attached to it:
const int Switch = 24;
String DataIn; // Variable to store in the String value
int Sendback = 0;
int Sendreq = 0;
int maintain = 0;
unsigned int comb [6];
unsigned int comb1 [6];
void setup() {
Serial1.begin(115200); // Set up Serial to 115200 baud
pinMode(Switch, OUTPUT);
digitalWrite(Switch, LOW); // turn off switch
// I2C protocol set up
Wire.begin();
Wire.beginTransmission(VEML7700);
Wire.write(0x00);
Wire.write(0x00);
Wire.write(0x08);
Wire.endTransmission();
Wire.beginTransmission(VEML7700);
Wire.write(0x01);
Wire.write(0x00);
Wire.write(0x00);
Wire.endTransmission();
Wire.beginTransmission(VEML7700);
Wire.write(0x02);
Wire.write(0xFF);
Wire.write(0xFF);
Wire.endTransmission();
Wire.beginTransmission(VEML7700);
Wire.write(0x03);
Wire.write(0x04);
Wire.write(0x00);
Wire.endTransmission();
}
void loop() {
if (Serial1.available()) { // Serial is receiving?
DataIn = Serial1.readString(); // Reading from Serial
if (DataIn.indexOf("Wake!") > 0) { // Checking for "Wake!" String
}
//storing light sensor values to be sent to 'M' Station
for (int i = 0; i < 3; i++) {
int Cup = Wire.read(); // receive a byte as character
int Cdown = Wire.read(); // receive a byte as character
if (Cdown < 200) {
maintain = 1;
delay(10);
}
else {
maintain = 0;
delay(10);
}
comb[i] = Cdown * 256 + Cup; // array to get assigned values in each position
}
digitalWrite(Switch, HIGH); // turn on switch////////////////////////////////////////////////////////////////////////////
delay(10);
//storing light sensor values to be sent to second set 'M' Station
{
for (int i = 3; i < 6; i++) {
int Cup = Wire.read(); // receive a byte as character
int Cdown = Wire.read(); // receive a byte as character
if (Cdown < 200) {
maintain = 1;
delay(10);
}
else {
maintain = 0;
delay(10);
}
comb[i] = Cdown * 256 + Cup; // array to get assigned values in each position
}
}
for (int i = 0; i < 6; i++) {
//Serial.println(comb[i]);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (Serial1.available()) { // Serial is receiving?
DataIn = Serial1.readString(); // Reading from Serial
//storing light sensor values to be sent to 'M' Station
for (int i = 0; i < 3; i++) {
int Cup = Wire.read(); // receive a byte as character
int Cdown = Wire.read(); // receive a byte as character
if (Cdown < 200) {
maintain = 1;
delay(10);
}
else {
maintain = 0;
delay(10);
}
comb1[i] = Cdown * 256 + Cup; // array to get assigned values in each position
}
digitalWrite(Switch, LOW); // turn off switch/////////////////////////////////////////////////////////////////
delay(10);
//storing light sensor values to be sent to 'M' Station
for (int i = 3; i < 6; i++) {
int Cup = Wire.read(); // receive a byte as character
int Cdown = Wire.read(); // receive a byte as character
if (Cdown < 200) {
maintain = 1;
delay(10);
}
else {
maintain = 0;
delay(10);
}
comb1[i] = Cdown * 256 + Cup; // array to get assigned values in each position
}
Sendback = 2;
Sendreq = 0;
maintain = 0;
for (int i = 0; i < 6; i++) {
Serial1.print("AT+SEND=488,3,%u\r\n", comb1[i]);
}
}
if (Sendback == 1) {
Serial1.print("AT+SEND=488,3,Up!\r\n"); // Send AT command with String "Up!"
delay(10);
Sendback = 0;
Sendreq = 1;
}
if (Sendreq == 1) {
Wire.beginTransmission(VEML7700);
Wire.write(0x04);
Wire.write(0x04);
Wire.endTransmission(false);
Wire.requestFrom(VEML7700, 2, true);
while (Wire.available()); // slave may send less than requested
}
if (maintain == 1) {
Serial1.print("AT+SEND=488,4,req!\r\n"); // Send AT command with String "req!"
delay(10);
maintain = 0;
}
if (Sendback == 2) {
Serial1.print("AT+SEND=488,5,Down!\r\n"); // Send AT command with String "Down!"
delay(10);
Sendback = 0;
Sendreq = 0;
maintain = 0;
}
delay(100);
}
Please let me know if you need any more information. I think overloading means that there are different kinds of information stored on comb1 and the compiler does not know which to choose from? But comb1 is declared as an unsigned int and nothing but integers are in the value. I do not know if I have the wrong understanding of what overloaded means, or if I am assigning multiple kinds of data in comb1. Please let me know what you think.
Serial1.print("AT+SEND=488,3,%u\r\n", comb1[i]);
print doesn't work with formatted strings. You need to use printf.
I've done a C program that writes an integer array into the Arduino:
// ...
FILE* file;
file = fopen("/dev/ttyuSB0","w");
for (int i = 0; i < 3; i++) {
fprintf(file, "%d ", rgb[i]);
}
fclose(file);
// ...
How can I do from my arduino code (.ino) to catch the three integers from the file?
while (Serial.available() > 0) {
// What can I do here ???
}
You need to read the data and put it into a buffer. After you encounter a ' ' character you terminate the string inside the buffer and convert it into an int.
When you do this three times you have read all three ints.
const uint8_t buff_len = 7; // buffer size
char buff[buff_len]; // buffer
uint8_t buff_i = 0; // buffer index
int arr[3] = {0,0,0}; // number array
uint8_t arr_i = 0; // number array index
void loop() {
while (Serial.available() > 0) {
char c = Serial.read();
if (buff_i < buff_len-1) { // check if buffer is full
if (c == ' ') { // check if got number terminator
buff[buff_i++] = 0; // terminate the string
buff_i = 0; // reset the buffer index
arr[arr_i++] = atoi(buff); // convert the string to int
if (arr_i == 3) { // if got all three numbers
arr_i = 0; // reset the number array index
// do something with the three integers
}
}
else if (c == '-' || ('0' <= c && c <= '9')) // if negative sign or valid digit
buff[buff_i++] = c; // put the char into the buffer
}
}
// maybe do some other stuff
}
Or if you don't mind blocking code[1] you can use builtin ParseInt.
void loop() {
while (Serial.available() > 0) {
arr[0] = Serial.parseInt();
arr[1] = Serial.parseInt();
arr[2] = Serial.parseInt();
Serial.read(); // read off the last space
// do something with the three integers
}
// maybe do some other stuff, but might be blocked by serial read
}
[1] If your computer has a hiccup and doesn't send all the data at once, your Arduino code will just wait for data and won't be doing anything else. Read more here.
I've read up on accessing PROGMEM for days now, and combed through several other questions, but I still can't get my code working. Any help would be appreciated.
I've included a full test sketch for Arduino below. The majority of it works, but when I loop through each byte of an "alpha" character, as pointed to by "alphabytes", I'm just getting garbage out so I'm obviously not accessing the correct memory location. The problem is, I can't figure out how to access that memory location.
I've seen several other examples of this working, but none that have different sizes of data arrays in the pointer array.
Please see line beginning with ">>>> Question is..."
// Include PROGMEM library
#include <avr/pgmspace.h>
// Variable to hold an alphabet character row
char column_byte;
// Used to hold LED pixel value during display
char led_val;
// Used to hold the screen buffer for drawing the screen
char matrix_screen[64];
/*
Define Alphabet characters. This should allow for characters of varying byte lengths.
*/
const char alpha_A[] PROGMEM = {0x06, 0x38, 0x48, 0x38, 0x06};
const char alpha_B[] PROGMEM = {0x7E, 0x52, 0x52, 0x2C};
const char alpha_C[] PROGMEM = {0x3C, 0x42, 0x42, 0x24};
/*
The "alphabytes" contains an array of references (pointers) to each character array.
Read right-to-left, alphabytes is a 3-element constant array of pointers,
which points to constant characters
*/
const char* const alphabytes[3] PROGMEM = {
alpha_A, alpha_B, alpha_C
};
/*
This array is necessary to list the number of pixel-columns used by each character.
The "sizeof" function cannot be used on the inner dimension of alphabytes directly
because it will always return the value "2". The "size_t" data
type is used because is a type suitable for representing the amount of memory
a data object requires, expressed in units of 'char'.
*/
const char alphabytes_sizes[3] PROGMEM = {
sizeof(alpha_A), sizeof(alpha_B), sizeof(alpha_C)
};
/**
* Code Setup. This runs once at the start of operation. Mandatory Arduino function
**/
void setup(){
// Include serial for debugging
Serial.begin(9600);
}
/**
* Code Loop. This runs continually after setup. Mandatory Arduino function
**/
void loop(){
// Loop through all alphabet characters
for( int a = 0; a < 3; a++) {
// Reset screen
for (int r = 0; r < 64; r++) {
matrix_screen[r] = 0;
}
// This line works to read the length of the selected "alphabyte"
int num_char_bytes = pgm_read_byte(alphabytes_sizes + a);
for (int b = 0; b < num_char_bytes; b++){
// Based on alphabytes definition,
// Examples of desired value for column_byte would be:
//
// When a=0, b=0 -> column_byte = 0x06
// When a=0, b=1 -> column_byte = 0x38
// When a=0, b=2 -> column_byte = 0x48
// When a=0, b=3 -> column_byte = 0x38
// When a=0, b=4 -> column_byte = 0x06
// When a=1, b=0 -> column_byte = 0x7E
// When a=1, b=1 -> column_byte = 0x52
// When a=1, b=2 -> column_byte = 0x52
// When a=1, b=3 -> column_byte = 0x2C
// When a=2, b=0 -> column_byte = 0x3C
// When a=2, b=1 -> column_byte = 0x42
// When a=2, b=2 -> column_byte = 0x42
// When a=2, b=3 -> column_byte = 0x24
// >>>>> Question is... how to I get that? <<<<<<<
// column_byte = pgm_read_byte(&(alphabytes[a][b])); // This doesn't work
// Thought: calculate offset each time
// int offset = 0;
// for(int c = 0; c < a; c++){
// offset += pgm_read_byte(alphabytes_sizes + c);
// }
// column_byte = pgm_read_byte(&(alphabytes[offset])); // This doesn't work
// column_byte = (char*)pgm_read_word(&alphabytes[a][b]); // Doesn't compile
column_byte = pgm_read_word(&alphabytes[a][b]); // Doesn't work
// Read each bit of column byte and save to screen buffer
for (int j = 0; j < 8; j++) {
led_val = bitRead(column_byte, 7 - j);
matrix_screen[b * 8 + j] = led_val;
}
}
// Render buffer to screen
draw_screen();
// Delay between frames
delay(5000);
}
}
/**
* Draw the screen. This doesn't have the correct orientation, but
* that's fine for the purposes of this test.
**/
void draw_screen(){
for (int a = 0; a < 8; a++) {
for (int b = 0; b < 8; b++) {
Serial.print((int) matrix_screen[a * 8 + b]);
Serial.print(" ");
}
Serial.println();
}
Serial.println();
}
Note that alphabytes it is array, which each element contains a REFERENCE (i.e. address) where corresponding characters are stored. So, you should access it in two steps.
First step is to know address in the progmem of the required item. Addresses are 16bits wide (unless you are using 128+k device).
PGM_VOID_P ptr = (PGM_VOID_P) pgm_read_word(&alphabytes[a]);
and, if you want to access it byte by byte, you can just read using this pointer, and then increment it:
for (int b = 0; b < num_char_bytes; b++) {
uint8_t column_byte = pgm_read_byte(ptr++);
...
}
After much research (and quite frankly a lot of trial and error), I have come across a solution which is working. I don't know if this the the most correct or most elegant solution, but it works.
column_byte = pgm_read_byte(pgm_read_byte(&alphabytes[a]) + b);
The inner call to pgm_read_byte():
pgm_read_byte(&alphabytes[a])
returns a value, which is the address of the character being evaluated (notice the leading "address-of" operator "&").
The outer pgm_read_byte reads that memory at an offset of "b".
This solution can also be broken down into two parts:
int memory_loc = pgm_read_byte(&alphabytes[a]);
column_byte = pgm_read_byte(memory_loc + b);
My C skills are not good enough to really explain if "memory_loc" needs to be an int (I tried it as a "char" and it also worked).
How to fill array with values from analogRead on arduino. every second arduino reads value from analog0 and i want to put these readings to array.
Let's say you want to read up to 100 values, do this:
1. Poor technique (uses blocking code with delay()):
//let's say you want to read up to 100 values
const unsigned int numReadings = 100;
unsigned int analogVals[numReadings];
unsigned int i = 0;
void setup()
{
}
void loop()
{
analogVals[i] = analogRead(A0);
i++;
if (i>=numReadings)
{
i=0; //reset to beginning of array, so you don't try to save readings outside of the bounds of the array
}
delay(1000); //wait 1 sec
}
Note: you cannot make the number in brackets too large. Ex: analogVals[2000] will not work because it takes up too much RAM.
PS. This is pretty basic stuff that Arduino covers in their help on the website. Please reference here from now on for questions like this, and give it a shot yourself first: http://arduino.cc/en/Reference/HomePage --> click on "array" under "Data Types."
2. Alternate method (also a poor technique, since it uses blocking code with delay()):
//let's say you want to read up to 100 values
const unsigned int numReadings = 100;
unsigned int analogVals[numReadings];
void setup()
{
}
void loop()
{
//take numReadings # of readings and store into array
for (unsigned int i=0; i<numReadings; i++)
{
analogVals[i] = analogRead(A0);
delay(1000); //wait 1 sec
}
}
UPDATE: 6 Oct. 2018
3. Best technique (non-blocking method--no delay!):
//let's say you want to read up to 100 values
const unsigned int numReadings = 100;
unsigned int analogVals[numReadings];
unsigned int i = 0;
void setup()
{
Serial.begin(115200);
}
void loop()
{
static uint32_t tStart = millis(); // ms; start time
const uint32_t DESIRED_PERIOD = 1000; // ms
uint32_t tNow = millis(); // ms; time now
if (tNow - tStart >= DESIRED_PERIOD)
{
tStart += DESIRED_PERIOD; // update start time to ensure consistent and near-exact period
Serial.println("taking sample");
analogVals[i] = analogRead(A0);
i++;
if (i>=numReadings)
{
i = 0; //reset to beginning of array, so you don't try to save readings outside of the bounds of the array
}
}
}
4. Professional-type approach (non-blocking method, avoids global variables by passing around pointers instead, uses C stdint types, and uses static variables to store local, persistent data):
// Function prototypes
// - specify default values here
bool takeAnalogReadings(uint16_t* p_numReadings = nullptr, uint16_t** p_analogVals = nullptr);
void setup()
{
Serial.begin(115200);
Serial.println("\nBegin\n");
}
void loop()
{
// This is one way to just take readings
// takeAnalogReadings();
// This is a way to both take readings *and* read out the values when the buffer is full
uint16_t numReadings;
uint16_t* analogVals;
bool readingsDone = takeAnalogReadings(&numReadings, &analogVals);
if (readingsDone)
{
// Let's print them all out!
Serial.print("numReadings = "); Serial.println(numReadings);
Serial.print("analogVals = [");
for (uint16_t i=0; i<numReadings; i++)
{
if (i!=0)
{
Serial.print(", ");
}
Serial.print(analogVals[i]);
}
Serial.println("]");
}
}
// Function definitions:
//---------------------------------------------------------------------------------------------------------------------
// Take analog readings to fill up a buffer.
// Once the buffer is full, return true so that the caller can read out the data.
// Optionally pass in a pointer to get access to the internal buffer in order to read out the data from outside
// this function.
//---------------------------------------------------------------------------------------------------------------------
bool takeAnalogReadings(uint16_t* p_numReadings, uint16_t** p_analogVals)
{
static const uint16_t NUM_READINGS = 10;
static uint16_t i = 0; // index
static uint16_t analogVals[NUM_READINGS];
const uint32_t SAMPLE_PD = 1000; // ms; sample period (how often to take a new sample)
static uint32_t tStart = millis(); // ms; start time
bool bufferIsFull = false; // set to true each time NUM_READINGS have been taken
// Only take a reading once per SAMPLE_PD
uint32_t tNow = millis(); // ms; time now
if (tNow - tStart >= SAMPLE_PD)
{
Serial.print("taking sample num "); Serial.println(i + 1);
tStart += SAMPLE_PD; // reset start time to take next sample at exactly the correct pd
analogVals[i] = analogRead(A0);
i++;
if (i >= NUM_READINGS)
{
bufferIsFull = true;
i = 0; // reset to beginning of array, so you don't try to save readings outside of the bounds of the array
}
}
// Assign the user-passed-in pointers so that the user can retrieve the data if they so desire to do it this way
if (p_numReadings != nullptr)
{
*p_numReadings = NUM_READINGS;
}
if (p_analogVals != nullptr)
{
*p_analogVals = analogVals;
}
return bufferIsFull;
}
Sample Serial Monitor output from the last code just above:
Begin
taking sample num 1
taking sample num 2
taking sample num 3
taking sample num 4
taking sample num 5
taking sample num 6
taking sample num 7
taking sample num 8
taking sample num 9
taking sample num 10
numReadings = 10
analogVals = [1023, 1023, 1023, 1023, 1023, 687, 0, 0, 0, 0]
taking sample num 1
taking sample num 2
taking sample num 3
taking sample num 4
taking sample num 5
taking sample num 6
taking sample num 7
taking sample num 8
taking sample num 9
taking sample num 10
numReadings = 10
analogVals = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
More reading/study:
My thorough answer on how to do time-stamp-based bare-metal cooperative multi-tasking: Best way to read from a sensor that doesn't have interrupt pin and requires some time before the measurement is ready
Arduino's very basic but extremely useful "Blink Without Delay" tutorial: https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
Just some premilinary info, the below code is part of a user space HID driver Im building, so Im going to try my best
to skip over the sections relating to device conectivity, etc...
I have two main arrays that I focus on which is the buf[] array and the buffer[] array.
The buffer[] array will be comprised of ranges of hex values that are taken from the buf[] array and appended on to it.
With the purpose of the buffer[] basically holding a large number of bytes that can then be consumed else where.
But I seem to be running into issues with buffer size growing past 1400, I also feel as though my logic as far as adding
to the buffer is flawed and not working as I think it is. Is there another way that I should be going about trying to acomplish this end goal?
If this code does not show enough PLEASE let me know and I will make the neccesary edits, Im desperate for the help
and still very new to C. Thank you!
full code found here if necessary http://pastebin.com/cyawpXCq
int int main(int argc, char const *argv[])
{
unsigned char buf[64];
unsigned char buffer[1400]; //1400 is the max size this should ever be
int i;
size_t buffer_length = 0;
for (i = 0; i < 24; i++)
{
// in this loop I add elements to the buf[]
// these elements are sent to the device
// which then returns the buf[] with new elements
// in it.
// I then want to push a range of these elements
// into the buffer which I am attempeting like so
// I skip the first 7 elements and take the
// next 16 elements which follow and append those to
// the buffer[].
memcpy(buffer + buffer_length, buf + 8, 16);
printf("\nStored Buffer - \n");
for (size_t i= 0; i < sizeof(buffer); i++) {
printf("%02X ", buffer[i]);
}
// add another 16 to the length
buffer_length += 16;
// loop breaks based on another condition
break;
}
// This condition comes next and does a similar
// thing as above
if(true)
{
// buf[] gets returned to
// me containing hex values from a device. I now
// want to append these values into the buffer[]
// and take the 30 elements which come after the
// first 7.
memcpy(buffer + buffer_length, buf + 8, 30);
printf("\nStored Buffer - \n");
for (size_t i= 0; i < sizeof(buffer); i++) {
printf("%02X ", buffer[i]);
}
// add another 30 to the length
buffer_length += 30;
}
// this loop can can return up to about 200 unique buf[] arrays
// and again is doing a similar thing where buf[] is filled with device
// data
for( int address = 0x0370; address < 0x0370 + 1400 + 1400; address += 28)
{
// I want to append 28 elements following the 7th element from buf[]
// to the end of the buffer[] on each iteration
memcpy(buffer + buffer_length, buf + 8, 28);
printf("\nStored Buffer - \n");
for (size_t i= 0; i < sizeof(buffer); i++) {
printf("%02X ", buffer[i]);
}
// add 28 to length each time.
buffer_length += 28;
printf("buffer_length = %zu", buffer_length);
}
return 0;
}