Hello,
I have a function with do-while loop that reads lines from SD card file then send it to async stream object. this function returns true or false according to SD card read status. I have also main timer runs in background to display date\time and some parameters. every thing runs very well, but if this function returns false the time completely stops(timer temporarily stops when loop runs), but the software runs OK, only the timer stops! any ideas what to do.
Thanks
I have a function with do-while loop that reads lines from SD card file then send it to async stream object. this function returns true or false according to SD card read status. I have also main timer runs in background to display date\time and some parameters. every thing runs very well, but if this function returns false the time completely stops(timer temporarily stops when loop runs), but the software runs OK, only the timer stops! any ideas what to do.
Thanks
B4X:
private Sub CommandHandler(Buffer () As Byte, StreamSource As Byte)
If ReadRFIDbusy = True Or CommandBusy = True Then
Return
End If
IndicateAll(False,False,False)
CommandBusy = True
IndicateBusy(True)
StartDisplayTimeout
Dim BufferLength As Byte = Buffer.Length
Dim BufferHeader As Byte = Buffer(0)
Dim Command As Byte = Buffer(1) 'read = 1 write = 0
Dim RegisterID As Byte = Buffer(2) 'register id, i.e. register 0x01 = date time register
Dim FrameLength As Byte = Buffer(3) 'length of frame
Dim BufferEndofTransmission As Byte = Buffer(BufferLength - 1)
End If
Select RegisterID
Case REG_DATE_TIME
If Command = CMD_WRITE Then
SetRTCDateTime(Buffer(4) + 2000,Buffer(5),Buffer(6),Buffer(7),Buffer(8),Buffer(9),Buffer(10))
SendStreamMessage(ERR_PC_NO_ERR,StreamSource)
Else IF Command = CMD_READ Then
SendStreamMessage(Array As Byte (0x02,CMD_READ,REG_DATE_TIME,0x0C,Years - 2000,Months,DayOfMonth,DayOfWeek,Hours,Minutes,Seconds,0x04),StreamSource)
End If
NotifyCommand(True)
Return
Case REG_FILE '<<<<< this will call upload file
Dim StringStart As Byte = Buffer(4)
If StringStart <> 0x24 Then 'ASCII $
SendStreamMessage(ERR_PC_DATA_START_CHR,StreamSource)
NotifyCommand(False)
Return
End If
Dim fnl As Byte = BufferLength - 6 'length of file name = buffer length - 6 bytes
Dim fnx As Byte = 5 ' file name starts from byte 5 in buffer
Dim filename (fnl) As Byte
BC.ArrayCopy2(Buffer,fnx,filename,0,fnl)
If Command = CMD_READ Then
Dim upl As Boolean
upl = UploadFile(BC.StringFromBytes(filename),True,StreamSource)
If upl Then
NotifyCommand(True)
Return
Else
SendStreamMessage(ERR_PC_STREAM,StreamSource)
NotifyCommand(False)
Return
End If
End If
B4X:
Sub UploadFile (FileName As String,SendFileInfo As Boolean,StreamSource As Byte) As Boolean
Dim BytesPerLine As Byte = 0x2A ' bytes per packet in log file excluding line feed chars
Delay(1000)
Dim success As Boolean = True
If Sd.OpenRead(BC.StringFromBytes(FileName)) Then
Dim size As ULong = Sd.CurrentFile.Size
UPLOAD_FILE_SIZE = size
Dim Tmp(15) As Byte 'compose file header array
Dim RAF As RandomAccessFile
RAF.Initialize(Tmp,False)
RAF.WriteByte(0x02,RAF.CurrentPosition)
RAF.WriteByte(CMD_READ,RAF.CurrentPosition)
RAF.WriteByte(REG_FILE,RAF.CurrentPosition)
RAF.WriteByte(0x0F,RAF.CurrentPosition)
RAF.WriteBytes(NumberFormat(size+5,6,0).GetBytes,0,6,RAF.CurrentPosition) 'file size
RAF.WriteBytes(NumberFormat(BytesPerLine,4,0).GetBytes,0,4,RAF.CurrentPosition) 'bytes per line
RAF.WriteByte(Asc("$"),RAF.CurrentPosition)
If SendFileInfo Then SendStreamMessage(Tmp,StreamSource)
Dim counter As ULong = 0
Dim Buffer(BytesPerLine) As Byte
Do While counter < size
UPLOAD_PERCENTAGE = (counter / size) * 100
Dim read As Int = Sd.Stream.ReadBytes(Buffer, 0, Min(Buffer.Length, size - counter))
If read = 0 Then
success= False
Exit
End If
counter = counter + read
SendStreamMessage((BC.SubString2(Buffer,0,Buffer.Length-2)),StreamSource)
Loop
If SendFileInfo Then SendStreamMessage(Array As Byte (0x04),StreamSource)'Log(0x04) 'end of transmission
Sd.Close
Else
success = False
End If
If success Then
Return True
Else
Return False
End If
End Sub
B4X:
Sub DS1320timer_Tick()
Dim currenttime As DSTime
currenttime = DS1302.CurrentTime
Years = currenttime.years
Months = currenttime.Months
DayOfMonth = currenttime.DayOfMonth
Hours = currenttime.Hours
Minutes = currenttime.Minutes
Seconds = currenttime.Seconds
DayOfWeek = currenttime.DayOfWeek
TimeTicks = TimeTicks + 1
MinutesSerial = GetMinutsSerialNumber(Years,Months,DayOfMonth,Hours,Minutes)
Blink = Not(Blink) 'toggle blink status, all blinking leds will blink at same time
If TimeTicks > (TimeTicksLast + 5) Then
For m = 0 To 13
BufferLast(m) = 0 'reset comparison array
Next
EnableRFID(True)
End If
'If readRFIDbusy = False Or KeypadBusy = False Or CommandBusy = False Then ' no reading, start count
If ReadRFIDbusy = False Or CommandBusy = False Then ' no reading, start count
LCDdisplayTicks = LCDdisplayTicks + 1
If LCDdisplayTicks >= SYS_LCD_TIMEOUT Then
Sleep = True
SetBacklightLevel(0x00,False)
LCD.Clear
LCD.DisplayOn = False
LCD.CursorOn =False
LCD.Blink = False
IndicateAll (False,False,False)
'ScreenType = SCREEN_TYPE_MAIN '******************* change this in the future, but now we return to main screen if timeout occure at any place in code
StandbyPowerLED.DigitalWrite(Blink) 'blink standby led
End If
End If
If RFIDmode <> RFID_MODE_LOGGER Then
OKReadyLED.DigitalWrite(Blink)
End If
If ScreenType = SCREEN_TYPE_MAIN Then 'update date time on main screen only
PutString(0,0,DayOfWeekArr(DayOfWeek - 1),False)
'PutString(0,0,DayOfWeekArr(DayOfWeek-1),False)
PutString (4,0,NumberFormat(Years,4,0),False)
PutString (8,0,"-",False)
PutString (9,0,NumberFormat(Months,2,0),False)
PutString (11,0,"-",False)
PutString (12,0,NumberFormat(DayOfMonth,2,0),False)
PutString (5,1,NumberFormat(Hours,2,0),False)
PutString (7,1,":",False)
PutString (8,1,NumberFormat(Minutes,2,0),False)
PutString (10,1,":",False)
PutString (11,1,NumberFormat(Seconds,2,0),False)
PutString (0,3,SigningArray(SigningMode-1),False)
PutString (4,3,ReasonCodeArray(ReasonCode-1),False)
PutString (8,3,LoadTypeArray(LoadType-1),False)
PutString (11,3,RFIDmodeArr(RFIDmode-1),False)
End If
End Sub