![]() |
Forum Index : Microcontroller and PC projects : Grey Cells need checking
Author | Message | ||||
OA47 Guru ![]() Joined: 11/04/2012 Location: AustraliaPosts: 986 |
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: AustraliaPosts: 1003 |
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: AustraliaPosts: 986 |
Thanks disco4now, 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: AustraliaPosts: 6283 |
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: AustraliaPosts: 986 |
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: AustraliaPosts: 6283 |
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 KingdomPosts: 2944 |
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 ![]() |
||||
CaptainBoing![]() Guru ![]() Joined: 07/09/2016 Location: United KingdomPosts: 2170 |
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. |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |