Hi all,
Please consider the following code (tested on Arduino Diecimilla with a 328):
// counttest.pde
// CASE1 - byte array, read back long element and print it
// CASE2 - byte array, dump 100 long elements
// CASE3 - long array, read back element and print it
// CASE4 - long array, dump 100 elements
#define CASE4
static const long SERSPEED=115200;
#define NUMELEM 100
static const int NUMLONG= NUMELEM;
static const int SUL = sizeof(unsigned long);
// in bytes
static const int SIZEB = NUMELEM*SUL;
#if ( defined(CASE1) || defined(CASE2) )
static byte tarray[SIZEB];
#endif
#if ( defined(CASE3) || defined(CASE4) )
static unsigned long tarray[NUMLONG];
#endif
// where are we in tarray? location index..
static unsigned int tli = 0;
// long element variable
static unsigned long telem = 0;
// temporary
static unsigned long tmpl = 0;
void setup()
{
Serial.begin(SERSPEED);
Serial.println("AAAAAA"); // start marker
delay(2000);
}
void loop()
{
getNextElem();
#if defined(CASE1)
tmpl = *(unsigned long *) &tarray[tli];
Serial.println(tmpl, DEC);
#endif
#if defined(CASE3)
tmpl = tarray[tli];
Serial.println(tmpl, DEC);
#endif
#if ( defined(CASE2) )
if (tli == 0) {
Serial.println("------");
Serial.write(tarray, SIZEB);
}
#endif
#if ( defined(CASE4) )
if (tli == 0) {
Serial.println("------");
Serial.write((uint8_t *)tarray, SIZEB);
}
#endif
//delay(10);
}
void getNextElem()
{
telem++;
#if ( defined(CASE1) || defined(CASE2) )
*(unsigned long *) &tarray[tli] = telem;
tli += SUL;
if (tli == SIZEB) {
tli = 0;
}
#endif
#if ( defined(CASE3) || defined(CASE4) )
tarray[tli] = telem;
tli++;
if (tli == NUMLONG) {
tli = 0;
}
#endif
}
Basically, a long counter gets incremented and its value gets stored in an array; afterwards, either the latest value - or a dump of 100 latest values - gets sent via serial.
Cases 1 and 3 return a single value, and they produce the same output:
$ cat /dev/ttyUSB0 # (line 1)
AAAAAA
0
0
0
0
...
0 # (line 100)
0
1
2
3
4
5
6
7
8
9
10
11
...
Apart from the beginning zeroes (and sometimes an extra line end seems to get inserted), it seems that values are shown in proper order as expected.
Cases 2 and 4 return a binary dump, and they also produce the same output:
cat /dev/ttyUSB0 | hexdump -v -e '"%07.1_ax \t"' -e '4/1 "0x%02x, " " " 4/1 "0x%02x, " "\t"' -e '4/1 "%_p " " " 4/1 "%_p "' -e '"\n"'
0 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x0d, 0x0a, A A A A A A . .
8 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0d, 0x0a, - - - - - - . .
10 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, . . . . . . . .
18 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, . . . . . . . .
20 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, . . . . . . . .
28 0x07, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, . . . . . . . .
30 0x09, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, . . . . . . . .
38 0x0b, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, . . . . . . . .
40 0x0d, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, . . . . . . . .
48 0x0f, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, . . . . . . . .
50 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, . . . . . . . .
58 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x15, 0x00, . . . . . . . .
60 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x17, 0x00, . . . . . . . .
68 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x19, 0x00, . . . . . . . .
70 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x1b, 0x00, . . . . . . . .
78 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x1d, 0x00, . . . . . . . .
80 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x1f, 0x00, . . . . . . . .
88 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x21, 0x00, . . . . . ! .
90 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x23, 0x00, . . " . . . # .
98 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x25, 0x00, . . $ . . . % .
...
Now the interesting in these cases is this - note what happens whenever counter is at 0x10 and starts increasing:
38 0x0b, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
40 0x0d, 0x00, 0x00, 0x00,
0x0e, 0x00, 0x00, 0x00,
0x0f, 0x00, 0x00, 0x00,
0x10, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, // 0x11 missing!
0x12, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, // 0x13 missing!
0x14, 0x00, 0x00, 0x00,
0x15, 0x00, 0x00, 0x00,
0x16, 0x00, 0x00, 0x00, 0x17, 0x00, ...
...
some 1000 bytes below:
....
448 0x00, 0x00, 0x0c, 0x01, 0x00, 0x00,
0x0d, 0x01, 0x00, 0x00,
0x0e, 0x01, 0x00, 0x00,
0x0f, 0x01, 0x00, 0x00,
0x10, 0x01, 0x00, 0x00,
0x01, 0x00, 0x00, // 0x11 missing!
0x12, 0x01, 0x00, 0x00,
0x01, 0x00, 0x00, // 0x13 missing!
468 0x14, 0x01, 0x00, 0x00, 0x15, 0x01, 0x00, 0x00,
470 0x16, 0x01, 0x00, 0x00, 0x17, 0x01, 0x00, 0x00,
...
Well, I'm completely puzzled!?
- Cases 1 and 3, which read element by element, show that data is apparently entered correctly in the array - otherwise there would be abrupt changes in values if there are missing bytes
- Cases 2 and 4 have the exact same problem, so it is not due to the how the array is declared?
I'm not aware that 0x11 and 0x13 should have some "special character" meaning - could it be that they maybe get interpreted as commands to the FTDI chip, so it "eats" them during transmission?
Edit: So the questions are:
- Does anyone know why 0x11 and 0x13 are dropped when sending data via Serial.write?
- Does anyone know why in cases 1/3, the very first run shows zeroes?
Thanks,
Cheers!