Skip to content
Snippets Groups Projects
Commit b908c34a authored by Kyrre Ness Sjobaek's avatar Kyrre Ness Sjobaek
Browse files

Add timeout for SPC data reading

parent a467e7bd
No related branches found
No related tags found
No related merge requests found
......@@ -53,6 +53,7 @@ IPAddress subnet(255, 255, 0, 0);
// ***** OTHER CONFIG ***************************
const unsigned long update_interval = 1000; //[ms]
const unsigned long SPC_timeout = 5; //[ms] 260 us per 4-bit word, 13 words, plus some buffer
// ***** GLOBAL VARIABLES ***********************
......@@ -260,6 +261,8 @@ void loop() {
void readSPCinstrument(uint8_t ch) {
unsigned long SPC_timeout_time = millis();
digitalWrite(pinREQ[ch],HIGH);
byte spcdata[13]; // The raw data sent by instrument
......@@ -273,8 +276,18 @@ void readSPCinstrument(uint8_t ch) {
for (uint8_t j = 0; j < 4; j++) {
//WARNING: If instrument is not connected,
// the Arduino may become unresponsive.
while( digitalRead(pinCLK[ch]) == LOW) { } // hold until clock is high
while( digitalRead(pinCLK[ch]) == HIGH) { } // hold until clock is low
while( digitalRead(pinCLK[ch]) == LOW && millis() < SPC_timeout_time ) { } // hold until clock is high
while( digitalRead(pinCLK[ch]) == HIGH && millis() < SPC_timeout_time ) { } // hold until clock is low
if (millis() >= SPC_timeout_time) {
Serial.print("SPC timeout on ch=");
Serial.print(ch);
Serial.print(", i=");
Serial.print(i);
Serial.print(", j=");
Serial.println(j);
return;
}
bitWrite(k, j, (digitalRead(pinDAT[ch]) & 0x1));
......@@ -350,6 +363,9 @@ void readSPCinstrument(uint8_t ch) {
measDataIsMm[ch] = isMM;
measDataFresh[ch] = true;
}
return;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment