![]() |
Forum Index : Microcontroller and PC projects : String problem
Page 1 of 2 ![]() ![]() |
|||||
Author | Message | ||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1993 |
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: AustraliaPosts: 1097 |
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 result ChopperP |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10315 |
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: AustraliaPosts: 55 |
Does this sound like an ascii unicode mismatch? |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1993 |
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: AustraliaPosts: 6283 |
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: AustraliaPosts: 1993 |
@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 "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: GermanyPosts: 1593 |
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 causality ≠ correlation ≠ coincidence |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Show how you produce T$ etc. Something aint right! Jim VK7JH MMedit |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1993 |
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: AustraliaPosts: 1993 |
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: AustraliaPosts: 6283 |
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 VK7JH MMedit |
||||
Chopperp![]() Guru ![]() Joined: 03/01/2018 Location: AustraliaPosts: 1097 |
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: AustraliaPosts: 1993 |
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 ZealandPosts: 2442 |
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: AustraliaPosts: 1993 |
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 KingdomPosts: 4044 |
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. John |
||||
PicFan Senior Member ![]() Joined: 18/03/2014 Location: AustriaPosts: 133 |
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) Wolfgang |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1993 |
@ 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. "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: AustraliaPosts: 6283 |
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 ![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |