Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 16:52 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 1 of 2    
Author Message
palcal

Guru

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

I am sending weather information using HC-12s. I have formatted the individual strings and added them together to send. So the string I send is something like "3065 21 45137"
where the first two characters are the temperature the second two the humidity and the rest are three character strings of rain values.
I have printed out the string as received "3065 21 45137", to break it up into the original values I use Temperature$=Left$(Weather$,2) - Humidity$=Mid$(Weather$,3,2)
But Left$(Weather$,2) only returns the first character "3" then Humidity$=Mid$(Weather$,3,2) returns "06" it is as there is a character before the first 3. I have overcome the problem by using Left$(Weather$,3) which returns "30" and so on.
Any ideas?
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1097
Posted: 08:54am 02 Apr 2019
Copy link to clipboard 
Print this post

palcal

Have you done a LEN$() to see how many characters are actually in the string?

Brian

Edit

I usually do what you did: Adjust the Left$ & Mid$ until I get the desired resultEdited by Chopperp 2019-04-03
ChopperP
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10315
Posted: 09:53am 02 Apr 2019
Copy link to clipboard 
Print this post

  Quote  I have overcome the problem by using Left$(Weather$,3) which returns "30" and so on.
Any ideas?


print asc(left$(Weather$,1))


will tell you what is in the first location in the string
 
Timbergetter

Regular Member

Joined: 08/10/2018
Location: Australia
Posts: 55
Posted: 10:34am 02 Apr 2019
Copy link to clipboard 
Print this post

Does this sound like an ascii unicode mismatch?
 
palcal

Guru

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

Len(Weather$) returns 15 although it is only 13 characters long.
asc(left$(Weather$,1)) returns zero as does asc(Weather$)

"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: 08:37pm 02 Apr 2019
Copy link to clipboard 
Print this post

Look at what you are sending to see if the rouge zero is being inserted before it's send or during the sending.

Find the other extra character while you are at it.

for n = 1 to len(Weather$)
print asc(mid$(weather$,n,1))
next n


I would expect a CR or LF at the end of the string.

Jim

VK7JH
MMedit
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1993
Posted: 08:58pm 02 Apr 2019
Copy link to clipboard 
Print this post

@TassyJim

Tried that, the string prints as "2571 23 57113"
and your code returns
0
02
06
07
01
0
02
03
0
05
07
01
01
03
0
so there appears to be a blank character at each end although it does not print.

The code I am using to receive the data is
Do
If Loc(#1)<>0 Then
D$=Input$(1,#1)
Weather$=Weather$+D$ ' Weather$ contains all the data
EndIf
Loop Until D$=Chr$(13)

so there is a CR at the end but your code is not printing it.

On the transmit side the code is,
Open "COM2:9600" As #1
Weather$=T$+H$+R$+RY$+RM$ ' convert data into one string
Print #1, Weather$
Pause 500
Close #1

Edited by palcal 2019-04-04
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1593
Posted: 11:06pm 02 Apr 2019
Copy link to clipboard 
Print this post

  palcal said   @TassyJim

Tried that, the string prints as "2571 23 57113"
and your code returns
0
02
06
07
01
0
02
03
0
05
07
01
01
03
0
so there appears to be a blank character at each end although it does not print.


Hi Paul,

there must be something completely wrong!
For "2571 23 57113" I would expect you should get:

50
53
55
49
32
50
51
32
53
55
49
49
51
(13)


A blank character " " (or <Space>) is represented by chr$(32).
Chr$(0) is a non-printable character.

Regards Michael

Edit:
Code
Weather$="2571 23 57113"+Chr$(13)
For n = 1 To Len(Weather$)
Print Asc(Mid$(weather$,n,1))
Next n
End


This code would produce your result:
Weather$="2571 23 57113"+Chr$(13)
For n = 1 To Len(Weather$)
Print "0"+Mid$(weather$,n,1)
Next n
End
Edited by twofingers 2019-04-04
causality ≠ correlation ≠ coincidence
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 11:53pm 02 Apr 2019
Copy link to clipboard 
Print this post

  palcal said  
On the transmit side the code is,
Open "COM2:9600" As #1
Weather$=T$+H$+R$+RY$+RM$ ' convert data into one string
Print #1, Weather$
Pause 500
Close #1



Show how you produce T$ etc.
Something aint right!

Jim
VK7JH
MMedit
 
palcal

Guru

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

I agree something is a bit screwy I can't see how Asc(Mid$(weather$,n,1))
can produce the string variables instead of the ascii.
Here is a bigger snippet of the code.
Open "COM2:9600" As #1
Temperature = Cint(Temperature)
RelHumid = Cint(RelHumid)
Rain=Pin(18)*.45 ' convert gauge tips to mm.
T$= Str$(Temperature,2)
H$= Str$(RelHumid,2)
R$=Str$(rain,3)
RY$=str$(RainY,3)
RM$=str$(RainM,3)

Weather$=T$+H$+R$+RY$+RM$ ' convert data into one string
Print #1, Weather$
Print Temperature
Print RelHumid
Pause 500
Close #1

"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
palcal

Guru

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

I wiped the code from the MM and reinstalled it, now things are very different.
The string prints as "3153 23 57113" and Jim's code prints
0
51
49
53
51
32
50
51
32
53
55
49
49
51
13
so I have a rogue character at the start and a CR at the end.

"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: 02:33am 03 Apr 2019
Copy link to clipboard 
Print this post

I would rather find out where the zero is coming from but this will get around the problem:

if asc(Weather$) = 0 then Weather$ = mid$(Weather$,2)


The zero might be left over from a previous transmission.
It would be worth flushing the buffer after processing any data.

That's a couple of very strange problems you have had recently.
Too much water in the system?

Jim
Edited by TassyJim 2019-04-04
VK7JH
MMedit
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1097
Posted: 03:02am 03 Apr 2019
Copy link to clipboard 
Print this post

Interesting, I did Jim's ASC check on my received data to see what it was doing. It was as expected.

In the process, after I had removed other print statements, I noticed my CMM was sending one lot of data when it shouldn't have been.
Something wrong with an AND function and another test I put in but wasn't working as expected.
Couldn't work out why, so removed them. Now appears OK.

Thanks guys for the topic.

I flush buffers if data is incorrect.

Brian


ChopperP
 
palcal

Guru

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

Yes the 0 is a mystery. I added a line to the transmit side to clear Weather$
Weather$=""
Weather$=T$+H$+R$+RY$+RM$ ' convert data into one string

and I clear the buffer on the receive side.
I am working around it, Grogster has had a lot to do with HC-12s maybe he has an idea.
Grogs are you there?

"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2442
Posted: 10:40am 03 Apr 2019
Copy link to clipboard 
Print this post

linux terminates strings with a chr$(0), are you getting your data from a linux-based device?


cheers,
rob :-)
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1993
Posted: 09:04pm 03 Apr 2019
Copy link to clipboard 
Print this post

No not Linux, but as TassyJim said I have had a batch of very unique problems with this project, and the latest is nothing short of bizarre, I will elaborate further later.
But I am now convinced that I have a hardware problem somewhere, whether it be the E-28 or my computer or somewhere else. I am going to change over to a 170 Backpack later and see if that fixes things. Meantime, today is shopping day, so off to town.
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 07:31am 04 Apr 2019
Copy link to clipboard 
Print this post

  robert.rozee said   linux terminates strings with a chr$(0)

cheers,
rob :-)

Sorry to say otherwise but no, it doesn't.

It's usual to use that internally in C, to mark the end of a string, but it's also usual not to send such a char out (except in the not so common case where you want to).

It's not required to use a char 0 (null byte if you prefer the term) in C, however.

The above applies to C on Windows, Linux or anywhere.

Linux itself will not send any char out (0 or otherwise) unless you tell it to do so.

JohnEdited by JohnS 2019-04-05
 
PicFan
Senior Member

Joined: 18/03/2014
Location: Austria
Posts: 133
Posted: 08:56am 05 Apr 2019
Copy link to clipboard 
Print this post

  palcal said  
The code I am using to receive the data is
Do
If Loc(#1)<>0 Then
D$=Input$(1,#1)
Weather$=Weather$+D$ ' Weather$ contains all the data
EndIf
Loop Until D$=Chr$(13)



Hello !

Please try in your code:
Do
If Loc(#1)<>0 Then

PAUSE 10 '**********************

D$=Input$(1,#1)
Weather$=Weather$+D$ ' Weather$ contains all the data
EndIf
Loop Until D$=Chr$(13)



WolfgangEdited by PicFan 2019-04-06
 
palcal

Guru

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

@ PicFan That did not help, thanks anyway.

So what I thought was so bizarre in hindsight was not. What happened was that when I tried printing to the LCD Screen everything printed OK except the Out Temp.
Nothing I could do would make it print, then it dawned on me it was the first entry in WEATHER$ that contained all the data and since the extra character was being sent at the beginning of the string OUT_TEMP$ contained the extra character. I didn't realise because it printed to the screen OK but the LCD Display did not like it. I have overcome the problem, the extra character which I originally thought was ASC 0 (Null) because on the first pass it is 0 and I was only letting it get that far when I let it run longer I found out it was actually ASC 10 (LF).
So to be on the safe side I put a line in the code
If Asc(Weather$)<32 Then Weather$=Mid$(Weather$,2,13)

and it works OK.
As for the HC-12s it must be the way they work, I removed them and reconfigured them both, but I have used them before and did not have this problem.

Edit. The extra character must be getting added on the receive side. If the TX has been running and I power up the RX, on the first loop the extra character is a Null but then on subsequent loops it is a LF.Edited by palcal 2019-04-07
"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: 04:28am 06 Apr 2019
Copy link to clipboard 
Print this post

You will be getting the LF because you are sending CRLF at the end of your packets and not clearing the RX buffer fully.
The first zero could be a startup artifact so a flushing of the RX buffer during initialisation might fix that.

Your system is not quite as 'strange' as we thought.

Jim


VK7JH
MMedit
 
     Page 1 of 2    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025