Are Data n and Data m coming from two sources? How are you separating Data n's bits from Data m's bits?
With the way your Available class keeps a reference to the PretendStream and looks at theStream.charactersInBuffer every time operator== is called, that means you could save and use an "available" variable like this:
auto av = strm.available();
// Wait until there is 1 or more bytes available.
// This example suggests we could also overload `operator!=` to mean "less than".
while (!(av == 1)) {}
(But like you said, obfuscation. Not that you should, but you can do it this way.)
If you can guarantee that there is a significant time gap between the messages, you could set up a system to act on the gap.
But then the system would be a sort of Finite State Machine, and you might as well follow the advice of and use one of schemes in @sterretje #3 and @UKHeliBob #6.
In particular, see @Robin2's Serial Example 6:
.... but if you don't have markers and you can count on the timing since the last byte of a message, keep track of when you add bytes and when you timeout, process the message and reset.
Very slick.
The sun has literally and figuratively emerged from behind a cloud.
Sry to have caused/added to any confusion.
Obvsly in the absence of tricky tricks
f() == 2
is only true if f returns 2.
No trout here but I will find a damp surrogate and slap myself wit it.
a7
Hi, Guys I have updated my code.
Now it works for my task.
#include <wiring_private.h>
#include "jtag.h"
#define TDI 12
#define TDO 15
#define TCK 13
#define TMS 14
#define MB_INT 28
#define MB_INT_PIN 31
#define SIGNAL_OUT 41 //B5 L16
#define SIGNAL_IN 33 //B2 N2
#define CLK 0
#define ENABLE 1
#define DATAPIN 2
#define no_data 0xFF, 0xFF, 0xFF, 0xFF, \
0xFF, 0xFF, 0xFF, 0xFF, \
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
0xFF, 0xFF, 0xFF, 0xFF, \
0x00, 0x00, 0x00, 0x00 \
#define NO_BOOTLOADER no_data
#define NO_APP no_data
#define NO_USER_DATA no_data
__attribute__ ((used, section(".fpga_bitstream_signature")))
const unsigned char signatures[4096] = {
//#include "signature.ttf"
NO_BOOTLOADER,
0x00, 0x00, 0x08, 0x00,
0xA9, 0x6F, 0x1F, 0x00, // Don't care.
0x20, 0x77, 0x77, 0x77, 0x2e, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x65, 0x73, 0x2d, 0x65, 0x6d, 0x62, 0x61, 0x72, 0x71, 0x75, 0x65, 0x73, 0x2e, 0x66, 0x72, 0x20, 0x00, 0x00, 0xff, 0xf0, 0x0f,
0x01, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, // Force
NO_USER_DATA,
};
__attribute__ ((used, section(".fpga_bitstream")))
const unsigned char bitstream[] = {
#include "Kurzschluss1.h" // Hier wird die FPGA file geladen.
};
int FPGAVal=HIGH;
const int SPEED=5;
const int FPGALED=6;
/// myCode
uint8_t incomingByte1 = 0;
uint8_t myarray[32]= {255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255}; //
uint32_t delaytime=50;
uint16_t Vset = 0;
uint16_t Iset = 0;
uint16_t v_ist = 0;
uint16_t i_ist = 0;
int n = 0;
int k = 31;
// the setup function runs once when you press reset or power the board
void printBin(uint32_t aByte) {
for (int16_t aBit = 31; aBit >= 0; aBit--)
Serial.write(bitRead(aByte, aBit) ? '1' : '0');
}
void printBin2(byte aByte) {
for (int8_t aBit = 7; aBit >= 0; aBit--)
Serial.write(bitRead(aByte, aBit) ? '1' : '0');
}
void setup() {
int ret;
uint32_t ptr[1];
//enableFpgaClock();
pinPeripheral(30, PIO_AC_CLK);
clockout(0, 1);
delay(1000);
//Init Jtag Port
ret = jtagInit();
mbPinSet();
// Load FPGA user configuration
ptr[0] = 0 | 3;
mbEveSend(ptr, 1);
// Give it delay
delay(1000);
// Configure onboard LED Pin as output
pinMode(LED_BUILTIN, INPUT);
// my code
pinMode(CLK,OUTPUT);
pinMode(ENABLE,OUTPUT);
pinMode(DATAPIN,OUTPUT);
///////////////////////////////////
// Disable all JTAG Pins (usefull for USB BLASTER connection)
pinMode(TDO, INPUT);
pinMode(TMS, INPUT);
pinMode(TDI, INPUT);
pinMode(TCK, INPUT);
// Configure other share pins as input too
pinMode(SIGNAL_IN, INPUT); // oSAM_INTstat
pinMode(MB_INT_PIN, INPUT);
pinMode(MB_INT, INPUT);
Serial.begin(9600);
pinMode(SPEED,INPUT);
pinMode(FPGALED,INPUT);
pinMode(18,OUTPUT); //A3
pinMode(19,OUTPUT); //A4 Analoger ausgang
//digitalWrite(SPEED,FPGAVal);
pinMode(16,INPUT); // A1
pinMode(17,INPUT); // A2
digitalWrite(CLK,LOW);
digitalWrite(ENABLE,LOW);
//digitalWrite(LED_BUILTIN,LOW);
digitalWrite(DATAPIN,LOW);
////////////////////////////
// while (!Serial);
//Serial.println("Press any key to toggle blink rate");
}
void loop() {
while(Serial.available()>0){
delay(10); // to avoid timing issues
incomingByte1 = Serial.read();
myarray[k] = incomingByte1;
k = k - 1 ;
n = n + 1;
// Serial.println(n,'DEC');
}
if ( n == 4 ){
Vset = myarray[31] << 8 | myarray[30] ;
Iset = myarray[29] << 8 | myarray[28] ;
analogWriteResolution(10);
analogWrite(18,Vset); // A3 ausgang
analogWriteResolution(10);
analogWrite(19,Iset); // A4 ausgang
//Serial.println("test2");
analogWrite(15,Vset);
n = 0 ;
k = 31 ;
}
if ( n == 32) {
// Serial.println("test1");
digitalWrite(ENABLE,LOW); // Start des protocol
delay(10);
for (int i=0; i<32; i++) {
for (int j=0; j<8; j++) {
digitalWrite(DATAPIN,bitRead(myarray[i],j));
delayMicroseconds(delaytime);
digitalWrite(CLK,HIGH);
delayMicroseconds(delaytime);
digitalWrite(CLK,LOW);
}
}
digitalWrite(ENABLE,HIGH); // Ende des protocols
delay(delaytime);
digitalWrite(DATAPIN,LOW);
//delay(5000);
// clear incoming buffer
Serial.read();
n = 0 ;
k = 31 ;
}
}
Data n is to control a voltage source
Data m will be send to the FPGA.
The Data sending is controlled via a Matlab GUI.
At first Data n are set. Then the voltage source is activated via toggle switch with Matlab GUI.
The Voltage builts up in the lab capacitor.
After that I send Data m via Matlab push button to fire a double pulse via FPGA.
The code even not compile, opening and closing braces doesn't match.
Procedure loop() not found.
Maybe a wrong brace is removed by deleting out commented parts.
The code wouldn't compile ether, because it needs Kurzschluss1.h (program of the FPGA).
I think I deleted void loop() { accidentally.
So fix the code BEFORE showing it to the forum.
As about reading the data, the current code is essentially little different from the very first one. Distinguishing data packets by the number of elements is a bad idea, completely unreliable in real use. Any delay in transmission and your entire algorithm will break.
You have already been told multiple times (posts #6, #7, #18 and #23) how to solve this problem correctly - develop your own protocol with a header, data type and checksum.