Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:58 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Grey Cells need checking

Author Message
OA47

Guru

Joined: 11/04/2012
Location: Australia
Posts: 986
Posted: 10:47pm 31 Jan 2019
Copy link to clipboard 
Print this post

I have a sub routine that is not working as expected and I was wondering if someone could point out my error.

I have a star network of LoRa units attached to a MM and the controlling radio broadcasts a time stamp regularly to keep the remote unit clocks up to date for reporting purposes. The time stamp is 10 characters in length and is made up of DDMMYYHHMM. The remote units react to any string ending in a CR but occasionally I will get a unit that reboots and as the console port is connected to the radio RS232 it broadcasts the reboot characters. This should not be a problem except the other units are rebooting in sympathy as they are seeing some of this code as an incorrect date format and rebooting on Error: Invalid date (the string sent out is "__________" and the code is trying to set the date as "__/__/__". I thought that my code would exclude this sort of string.

Sub SetClock
' N$ Should look like "2512191645"
If Val(Mid$(N$,5,2)) > 18 Then If Val(Right$(N$,4)) < 2400 Then
D$=Left$(N$,2)+"/"+Mid$(N$,3,2)+"/"+Mid$(N$,5,2)
Print " Date$ set to "; D$
Date$=D$
T$=Mid$(N$,7,2)+":"+Right$(N$,2)+":30"
Print " Time$ set to "; T$
Time$=T$
Else
Print Err$(5)
End If
End Sub



Help

OA47
 
disco4now

Guru

Joined: 18/12/2014
Location: Australia
Posts: 1003
Posted: 11:09pm 31 Jan 2019
Copy link to clipboard 
Print this post

I am not sure this is a valid structure for the IF THEN on a single line.


If Val(Mid$(N$,5,2)) > 18 Then If Val(Right$(N$,4)) < 2400 Then
....



What about


If Val(Mid$(N$,5,2)) > 18 AND Val(Right$(N$,4)) < 2400 Then
...



An initial test that its length is 10 would also help filter out non dates


If (len(N$)=10) AND (Val(Mid$(N$,5,2)) > 18) AND (Val(Right$(N$,4)) < 2400) Then
...



Latest F4 Latest H7 FotS
 
OA47

Guru

Joined: 11/04/2012
Location: Australia
Posts: 986
Posted: 11:31pm 31 Jan 2019
Copy link to clipboard 
Print this post

Thanks disco4now,
  Quote  An initial test that its length is 10 would also help filter out non dates


The string is tested to be 10 chars long before it proceeds to the sub.

Changed the code from a "Then If " to an "And" and I haven't got the Invalid date error but I also haven't seen the Err$(5) printed.

OA47
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 12:01am 01 Feb 2019
Copy link to clipboard 
Print this post

  Quote   N$ = "2512191645"

setclock
n$ =
"__________"

setclock

SUB SetClock
' N$ Should look like "2512191645"
IF VAL(MID$(N$,5,2)) > 18 THEN
IF VAL(RIGHT$(N$,4)) < 2400 THEN
D$=
LEFT$(N$,2)+"/"+MID$(N$,3,2)+"/"+MID$(N$,5,2)
PRINT " Date$ set to "; D$
DATE$=D$
T$=
MID$(N$,7,2)+":"+RIGHT$(N$,2)+":30"
PRINT " Time$ set to "; T$
TIME$=T$
ENDIF
ELSE
PRINT Err$(5)
END IF
END SUB


Combining single line and multiline IFs is not a good idea

Date$ set to 25/12/19
Time$ set to 16:45:30
[20] Print Err$(5)
Error: ERR is not declared
>


Jim
VK7JH
MMedit
 
OA47

Guru

Joined: 11/04/2012
Location: Australia
Posts: 986
Posted: 12:24am 01 Feb 2019
Copy link to clipboard 
Print this post

Thanks Jim. I thought I might give the string another qualifier as well and see how that goes.
Sub SetClock
' N$ Should look like "2512191645"
If Val(Mid$(N$,3,2)) < 13 Then
If Val(Mid$(N$,5,2)) > 18 Then
If Val(Right$(N$,4)) < 2400 Then
D$=Left$(N$,2)+"/"+Mid$(N$,3,2)+"/"+Mid$(N$,5,2)
Print " Date$ set to "; D$
Date$=D$
T$=Mid$(N$,7,2)+":"+Right$(N$,2)+":30"
Print " Time$ set to "; T$
Time$=T$
End If
End If
Else
Print Err$(5)
End If
End Sub


OA47
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 12:35am 01 Feb 2019
Copy link to clipboard 
Print this post

In that case you can combine
If Val(Mid$(N$,5,2)) > 18 Then
If Val(Right$(N$,4)) < 2400 Then


into

If Val(Mid$(N$,5,2)) > 18 and Val(Right$(N$,4)) < 2400 Then


Jim
VK7JH
MMedit
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2944
Posted: 10:30am 01 Feb 2019
Copy link to clipboard 
Print this post

  TassyJim said  
If Val(Mid$(N$,5,2)) > 18 and Val(Right$(N$,4)) < 2400 Then



I strongly recommend putting brackets around the expressions either side of the AND to ensure the 'correct parts' are AND'd:

If (Val(Mid$(N$,5,2)) > 18) AND (Val(Right$(N$,4)) < 2400) Then


Also makes it clearer to read when tracking down errors Edited by WhiteWizzard 2019-02-02
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 11:08am 01 Feb 2019
Copy link to clipboard 
Print this post

as an aside (a bit) here, I have two functions that use regular expressions to validate the "look and feel" of date and time strings and then the actual values of them here:

http://www.fruitoftheshed.com/MMBasic.IsDate-and-IsTime-functions-VB-Work-A-Like.ashx

the functions simply return a true(1) or false(0) if the input string is or isn't a date or time.

I use this all over my code where I set the date & time remotely - One product I have uses a star network of HC-12s and slave nodes get a periodic broadcast timecheck just like yours. The slaves pick out the time and date then use these two functions before setting their own internal clocks to prevent errors from garbage.Edited by CaptainBoing 2019-02-02
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025