0
votes

Project largely working. Have a 125Khz RFID module on an Arduino Uno, with SD card module and and RTC, all working nicely and passing data via PLX-DAQ to Excel and storing data to SD card.

I need a way of working out when the Uno is connected via PLX-DAQ to USB/serial, or when the Uno is just on battery.

So I thought to set a particular cell on Excel with the PLX-DAQ form macro in VBA to 1 (when connected) or 0 (disconnected) then read that in the Arduino code to determine whether to pass data via serial to excel or pull stored data off SD card.

The cell J4 toggles 0 or 1 according to whether disconnected / connected.

I then use the GET function of PLX-DAQ to read a cell from the Arduino sketch.

To upload the sketch I have to disconnect the connection between the RFID Tx and Arduino Rx or I get an error, which is normal, and if I run the sketch with that wire disconnected GET works fine.

void setup() {

  // open serial connection
    Serial.begin(9600);
    CLOCKSetup();
    RFIDSetup();
    SDSetup();
 
    Serial.println("CELL,GET,J4"); 
    int iniFlag = Serial.readStringUntil(10).toInt(); 
    Serial.println( (String) "Value of cell iniFlag is: " + iniFlag);
}

gives me this output in PLX-DAQ debug window

Value of cell iniFlag is: 1
=> Sending value '1' from cell 'J4'
CELL,GET,J4
SD card is ready to use.
RTC ready

But if I re-connect the wire from RFID Tx to Uno Rx the same sketch gives me this output

Value of cell iniFlag is: 0
=> Sending value '1' from cell 'J4'
CELL,GET,J4
SD card is ready to use.
RTC ready

There's no data going down the wire to Uno Rx at this stage, I've not scanned anything, and the Rx LED on the Uno doesn't light, so how is it affecting the GET function?

The GET command is handled by this code in PLX-DAQ in the VBA

 Case "CELL"
            Select Case UCase(DataVal(1))
                Case "GET"
                    '## Get Cell from active sheet or from named sheet
                    Select Case UCase(DataVal(2))
                        '##                         dataval    0    1      2        3    4 5
                        '## NOTE syntax to be Serial.println("CELL,GET,FROMSHEET,MySheet,C,9");
                        Case "FROMSHEET"
                            CommWrite cboPort.Text, Sheets(DataVal(3)).Cells(DataVal(5), DataVal(4)).Value
                            txtStatus2 = "Getting Cell " & DataVal(4) & DataVal(5) & " from sheet " & DataVal(3)
                            Call postToDirectDebug("Sending value '" & Sheets(DataVal(3)).Cells(DataVal(5), DataVal(4)).Value & "' from cell '" & DataVal(4) & DataVal(5) & "' of sheet '" & DataVal(3) & "'", DebugLevel.Outgoing)
                        '## NOTE syntax to be Serial.println("CELL,GET,C9");
                        Case Else
                            CommWrite cboPort.Text, WStoUse.Range(DataVal(2)).Value
                            Call postToDirectDebug("Sending value '" & WStoUse.Range(DataVal(2)).Value & "' from cell '" & DataVal(2) & "'", DebugLevel.Outgoing)
                            txtStatus2 = "Getting Cell " & DataVal(2)
                    End Select
                Case "SET"
                    '## Set Cell on active sheet or on named sheet
                    Select Case UCase(DataVal(2))
                        '##                         dataval    0    1     2       3    4 5     6
                        '## NOTE syntax to be Serial.println("CELL,SET,ONSHEET,MySheet,C,9,Any value");
                        Case "ONSHEET"
                            Sheets(DataVal(3)).Cells(DataVal(5), DataVal(4)).Value = ReplaceData(DataVal(6))
                            txtStatus2 = "Setting Cell " & DataVal(4) & DataVal(5) & " on sheet " & DataVal(3) & " with: " & DataVal(6)
                        '## NOTE syntax to be Serial.println("CELL,SET,C9,Any value");
                        Case Else
                            WStoUse.Range(DataVal(2)).Value = ReplaceData(DataVal(3))
                            txtStatus2 = "Setting Cell " & DataVal(2) & " with: " & ReplaceData(DataVal(3))
                    End Select
            End Select
2

2 Answers

1
votes

I assume you leave the arduino TX wire connected to the PC-RX. Thats why your PLX-DAQ still has the input. And as you suspect nothing will be going back.

First I thought, since nothing will come back, so your code will be stuck on

int iniFlag = Serial.readStringUntil(10).toInt(); 

As far as I can see the RFID.read does not send a LineFeed(10). However per arduino documentation (Serial.setTimeout()) should default to 1000ms. And thus should timeout. You can verify this by taking the read out of the setup and initializing the iniFlag in your loop as 0.

I think your actual problem is the local definition of iniFlag: int iniFlag = Serial.readStringUntil(10).toInt();

I assume in your loop you have something like.

void loop() {
       if rf.available {
         rf.read;
           if (iniFlag) {
             Serial.println("DATA, .....")
           } else {
             SD.write....
           }
      }
   }

So instead of doing the local definition of iniFlag, you should make this global by bringing it outside of the setup.

Like this:

    int iniFlag = 0;
    void setup() {
      ...
      iniFlag = Serial.readStringUntil(10).toInt(); 
      ...  
    }

And make sure you do not have an "int iniFlag" in your loop, which would redeclare it as local.

0
votes

Well after lots of head scratching I have a working fix, though still dont know why.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(8, 9);

into the Arduino sketch, then the TX wire from the RFID module to pin 8 of Arduino, and not only can I upload sketches without pulling the RFID wire every time, but the CELL, GET function in PLX-DAQ now works.