B4R Question rSd lib dont work when adding rSoftwareSerial lib

kohle

Active Member
Licensed User
Longtime User
Hi,

when I use the rsd lib all works fine. When I only add with the the rsoftwareserial lib
with the lib manager, without putting any new code in the sketch,
sd.openreadwrite always return FALSE.

I want to use the Softwareserial lib for accessing a bluetooth modul.

What I do wrong ?


<code>

filename = "test7.dat"
If sd.OpenReadWrite(filename)=True Then
....
end if

</code>
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It works fine here when rSoftwareSerial library is checked.

Tested with this code:
B4X:
Sub Process_Globals
  Public Serial1 As Serial
  Private sd As SD
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   sd.Initialize(4) '4 is the CS pin when using the ethernet shield
   Log("OpenReadWrite: ", sd.OpenReadWrite("test.txt"))
   sd.Stream.WriteBytes("abcde", 0, 5)
   sd.Close
   Log("Open: ", sd.OpenRead("test.txt"))
   Log("Size: ", sd.CurrentFile.Size)
   sd.Close
   Log("Free memory: ", AvailableRAM)
End Sub
 
Upvote 0

kohle

Active Member
Licensed User
Longtime User
Your code works,

but dont work with this code, softwareserial is checked, and LiquidCrystal_I2C lib :

B4X:
Sub Process_Globals
  
   Public sd As SD
   Public Serial1 As Serial
   Private lcd As LiquidCrystal_I2C
  
   Private bc As ByteConverter
  
End Sub

Private Sub AppStart
   
    Delay(3000)
   
    Dim filename As String
   
    Serial1.Initialize(115200)
   
    Log("AppStart")
   
    lcd.Initialize(0x27, 16, 2) 'based on the example from the project.
      lcd.Backlight=True
      lcd.Write("Test")

   
     sd.Initialize(10) '4 is the CS pin when using the ethernet shield
        Log("OpenReadWrite: ", sd.OpenReadWrite("test.txt"))
     sd.Stream.WriteBytes("abcde", 0, 5)
     sd.Close
     Log("Open: ", sd.OpenRead("test.txt"))
     Log("Size: ", sd.CurrentFile.Size)
     sd.Close
     Log("Free memory: ", AvailableRAM)
   
   
    filename = "test10.dat"
   
    writeSD(filename,"Zeile A")
    writeSD(filename,"Zeile B")
   
   
   
End Sub



Sub writeSD (filename As String, line As String)
   
    If sd.OpenReadWrite(filename)=True Then
           
'            Log("BytesAvaible:",sd.Stream.BytesAvailable)
'            Log("CurrentSize: ",sd.CurrentFile.Size)

            Dim te As String
            Dim wB As UInt
            Dim b() As Byte
   
            te = JoinStrings(Array As String(line,CRLF))
           
            b=bc.StringToBytes(te)
           
            sd.Position = sd.CurrentFile.Size
            wB = sd.Stream.WriteBytes(b, 0  ,b.Length)
           
           
           
            Log("Written:",wB," ",te)
           
            sd.Close   
           
Else
            Log("Cant open/create")
End If   
End Sub


Result:
AppStart
OpenReadWrite: 0
Open: 0
Size: 0
Free memory: 275
Cant open/create
Cant open/create
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that your code wastes a lot of memory.

You shouldn't use JoinStrings at all.
Just write the line and then write CRLF.

B4X:
sd.Stream.WriteBytes(line, 0, line.Length)
sd.Stream.WriteBytes(CRLF, 0, 2)

Try to add a delay after the call to lcs.write.

You can use a board such as Mega that have multiple hardware serial ports.
Additional Hardware Serial Ports
 
Upvote 0

kohle

Active Member
Licensed User
Longtime User
1) adding lcd I2C lib still cause no writing to sd card.
I will write the app it in the Adruino IDE to see it works there. I will inform you. Normally it shouldnt.

2) timer tick problem
It took some time to figure out, but I am not a specialist.

In the appstart , I call a sub which write to the sd card, like writeSD(filename,text) . After this I inialize a timer with 10sec and enable the timer.
In this timer sub I write to the sd Card too, but I had an issue (described in 3) that results an SD.oprenreadwrite = false.

What happened when the program started on the UNO
Neither data was written to the sd card in the appstart sub, nor in the timer sub.

My conclusion:
Fact is that when the first SD.oprenreadwrite returns FALSE, after this other SD.oprenreadwrite return FALSE too.
So the timer tick started before my first writing to the sd card in appstart sub, but I enabled the timer after.
I had a log("timer started") in the sub timer tick and it showed correct. So I was confused.
I dont know if the timer tick was called once internal, before starting correct.

When I correct the problem in the sub timer tick (described 3) , the app runs correct. In appstart was written to the sdcard and in the timer tick too.

I dont know if this is ok, maybe it be solved with some delays.
This can be a missunderstanding. I thought in the moment I use timer1.enabled = true , the timer will be activated.
It looks that "timer_ticks" runs one time before.



3) without lcd, it runs but I had an issue that looks like timing problems.

if I first write in a sub timer tick to the sdcard and than to a astream than writing to the sd card failed. It works with max.6 bytes, more didnt work.
If I do writing to a astream before writing to sd card than it works.



4) Some strange behaviors, where b4r dont shows erros :

- An error in the code, what is not recognized as error from b4r results to that the app is getting into an endless loop on the Uno
- Using pin 0/1 for RX/TX and transfering app to board, looks all fine, but start old program on the board. Disconnect pins before transfer, works
- What I didnt understand is :


5) Today I get the W5100 shield and will try this.
 
Upvote 0

kohle

Active Member
Licensed User
Longtime User
Hi Erel,

remember, my sd card dont work in combination softwareserial + lcdI2C + sd
in b4r

I wrote the sketch now in the Arduino IDE and it works fine.
So I think there is a problem in b4r or with the lib versions.

One problem is that I dont know what lib versions you integrated in b4R.
The libs in the Arduino IDE are often updated.

For the LCD I2C I use the libs I attached.

Maybe you have an idea.


Here my Arduino Code :


B4X:
#include <Wire.h>
#include <DHT.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
#include <SD.h>


#define DHTPIN A2
#define DHTTYPE DHT11


int ledPin = 5;    
int dhtPin = A2;
           
int value = LOW;               
long previousMillis = 0;       
long interval = 3000;

String sendLine = "";

File myFile;
String filename = "temp12.txt";


DHT dht(DHTPIN,DHTTYPE);
SoftwareSerial mySerial(2, 3);
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7,3, POSITIVE);
//LiquidCrystal_I2C lcd(0x27,16,2); 


void setup()
{ 

  Serial.begin(115200); 
  mySerial.begin(9600);

  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  
 
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  pinMode(dhtPin, OUTPUT);   
 
 

lcd.begin(16,2); 
lcd.setBacklight(HIGH);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Starting"); 
}

void loop()
{ 


  if (millis() - previousMillis > interval)
  {    previousMillis = millis();   // remember the last time we blinked the LED
    // if the LED is off turn it on and vice-versa.   
    if (value == LOW)      value = HIGH;    else      value = LOW;
    digitalWrite(ledPin, value);   // sets the LED on 

    float h = dht.readHumidity(); 
    float t = dht.readTemperature(); 

    sendLine = String("Temp: ")+ t ;

    delay(10);
    mySerial.print(sendLine+"\n");

   
    lcd.setCursor(0,0);
    lcd.print(sendLine);

    sdWrite(filename,sendLine);
   
 
   
   }

} 


void sdWrite(String file, String line)
{
  myFile = SD.open(file, FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    myFile.println(line);
    // close the file:
    myFile.close();
    Serial.println("Write ok");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  } 
 
}
 

Attachments

  • LiquidCrystal_V1.2.1.zip
    485.3 KB · Views: 324
Upvote 0

kohle

Active Member
Licensed User
Longtime User
Ok,

I see I, am writing here for my own, but it´s ok.

Last tests :

UNO + W5100 shield dont work with b4r, but Arduino IDE works

MEGA + W5100 works in b4R and Ard. IDE

Finally there has to be a problem.
 
Upvote 0
Top