Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 23:02 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 : String problem

     Page 2 of 2    
Author Message
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1993
Posted: 12:31am 07 Apr 2019
Copy link to clipboard 
Print this post

@ TassyJim

Do
If Loc(#1)<>0 Then
D$=Input$(1,#1)
Weather$=Weather$+D$ ' Weather$ contains all the data
EndIf
Loop Until D$=Chr$(13)
D$="" 'clear end of line flag
Close #1
Print "Weather$ ";Weather$
Print Len(Weather$)
If Asc(Weather$)<32 Then Weather$=Mid$(Weather$,2,13)
OutTemp$=Left$(Weather$,2)+"`" 'split up Weather$ to regain data
OutHum$=Mid$(Weather$,3,2)+"%"
Rain$=Mid$(Weather$,5,3)
RY$=Mid$(Weather$,8,3)
RYear$=Right$(Weather$,3)
Rainfall$=Rain$+" "+RY$+" "+RYear$
Weather$="" 'clear Weather$

This is the code, I don't see what else I can do to clear the buffer.Edited by palcal 2019-04-08
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
TassyJim

Guru

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

This should clear any left over characters in the system.

pause 20
junk$=Input$(Loc(#1),#1)



An alternative way to capture the data:

If Loc(#1)<>0 Then
pause 100 ' wait long enough for all text to arrive. Time depends on baud rate
Weather$==Input$(Loc(#1,#1) ' grab everything
' Weather$ contains all the data
EndIf
...

RYear$=Mid$(Weather$,11,3)


In both cases, the PAUSE time depends on the number of characters and the baud rate.
The values given are probably way too high (but longer than needed works OK)

Jim
VK7JH
MMedit
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1993
Posted: 02:59am 07 Apr 2019
Copy link to clipboard 
Print this post

@ TassyJim.

Thanks I think that has fixed it.Edited by palcal 2019-04-08
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1642
Posted: 06:31am 07 Apr 2019
Copy link to clipboard 
Print this post

Palcal,

I don't know if this helps but it works for me:

  Quote  '
OPEN "Com1:9600,,RxMsg,5" AS #1
'
' Receive the master's message
SUB RxMsg
RXD$ =
""
Cnt =
0
DO
IF LOC(#1)<>0 THEN
RXD$ = RXD$ +
INPUT$(1,#1)
Cnt =
0
ELSE
PAUSE 5 ' Wait longer than 1 character
Cnt = Cnt + 1
ENDIF
LOOP UNTIL Cnt = 2
END SUB
'
' Send the Response message
SUB TxMsg
PRINT #1, TXD$;
END SUB
'



It's part of a Modbus slave. The messages are variable in length and can end in any character (ie. a CRC). The end of the message is detected by waiting for a period of more that two character lengths with no character being received.

The message is received in an interrupt routine - it's a bit long but the slave has nothing else to do until the master tells it what to do anyway.

Bill
Keep safe. Live long and prosper.
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1993
Posted: 07:05am 07 Apr 2019
Copy link to clipboard 
Print this post

Thanks for that Bill but my code is working OK now, I have saved your code and will keep it in mind.
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
     Page 2 of 2    
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