Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 05:13 07 Jul 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 : Newie got a few questions

     Page 17 of 25    
Author Message
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 12:40pm 23 Dec 2016
Copy link to clipboard 
Print this post

This "seems to work" However it doesn't clear the serial buffer, it keeps adding all the serial data to the next input and prints everything until I get this error
"String too long"

Also the routing to check if "STX is the first 3 characters recieved and the data contains * doesn't seem to be working, yet it doesn't throw any errors

[code]Open "COM1:19200, 100" As #1
DO
B$ = B$+ Input$(254, #1) 'Suck everything in buffer out AND ADD TO B$
IF Left$(B$, 3) = "STX" Then IF INSTR(B$,"*") THEN Print B$ 'IF com port STX as first 3 characters AND contains "*" Then ......
pause 10
END IF
LOOP[/code]
 
disco4now

Guru

Joined: 18/12/2014
Location: Australia
Posts: 1000
Posted: 02:27pm 23 Dec 2016
Copy link to clipboard 
Print this post

Add this as shown below.

B$="" ' Clear the string to started gathering the next message fresh.

  lew247 said   This "seems to work" However it doesn't clear the serial buffer, it keeps adding all the serial data to the next input and prints everything until I get this error
"String too long"

Also the routing to check if "STX is the first 3 characters recieved and the data contains * doesn't seem to be working, yet it doesn't throw any errors

[code]Open "COM1:19200, 100" As #1
DO
B$ = B$+ Input$(254, #1) 'Suck everything in buffer out AND ADD TO B$
IF Left$(B$, 3) = "STX" Then IF INSTR(B$,"*") THEN Print B$ 'IF com port STX as first 3 characters AND contains "*" Then ......
B$="" ' Clear the string to started gathering the next message fresh.
pause 10
END IF
LOOP[/code]

Latest F4 Latest H7 FotS
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 12:57am 24 Dec 2016
Copy link to clipboard 
Print this post

My code is now
[code]
Open "COM1:19200, 100" As #1
DO
B$ = B$+ Input$(254, #1) 'Suck everything in buffer out AND ADD TO B$
IF Left$(B$, 3) = "STX" Then IF INSTR(B$,"*") THEN Print B$ 'IF com port STX as first 3 characters AND contains "*" Then ......
END IF
pause 10
B$="" ' Clear the string to started gathering the next message fresh.
LOOP[/code]

The STXG string prints perfectly

However the STXT and STXW only prints rarely

I've even changed the STXT and STXW strings to this
[quote]For AA = 1 to 10
Print #2, "STXT " , "," , year,",",month,",",day,",",Left$(Time$, 2),",",Mid$(TIME$, 4,2),",",Right$(TIME$, 2),"*" ' Send correct date/time to Indoor unit to set RTC
next AA
[/quote]
and
[quote]FOR BB = 1 to 10
Print #2, "STXW " , "," , bme280_read_temp()",",pressure",",humidity",",wheading",",wSpeed",",wgust",",hgust",",mmrain",",mmrain2",",hightemp",",h ightemp2",",lowtemp",",lowtemp2",",UVindex",",batt,"*"
next BB[/quote]

I really can't see why they won't print reliably



 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4036
Posted: 01:58am 24 Dec 2016
Copy link to clipboard 
Print this post

Your PRINTs won't end with a *

They'll end with (I expect both of, but at least one of) CR and LF.

So, your next INPUT$ will get CR/LF and that means your LEFT$ will not match STX.

You can either suppress the CR/LF (add a ;) or instead of LEFT$ use INSTR

John
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 06:52am 24 Dec 2016
Copy link to clipboard 
Print this post

I changed the Left$ to INSTR but it makes no difference, the STXG works perfectly
The STXT and STXW only works sometimes
I just dont get it at all

If anyone wants to see both sets of code they are enclosed
2016-12-24_165253_Code.zip
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4036
Posted: 07:30am 24 Dec 2016
Copy link to clipboard 
Print this post

You're clearing B$ far too often i.e. in the wrong place!

Imagine being the computer (the mmbasic interpreter). Your program reads a few chars and may have the STX and maybe some more but not the *

Then you pause and clear B$

Next you'll get some more chars, maybe the * as well, but you've already discarded the STX.

JohnEdited by JohnS 2016-12-25
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 07:34am 24 Dec 2016
Copy link to clipboard 
Print this post

I'm trying to do it so B$ clears once I have done all I want with the data and it's empty for when the next set of data comes in
The data is arriving at 19200 Baud
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4036
Posted: 07:35am 24 Dec 2016
Copy link to clipboard 
Print this post

That's very slow compared to your program.

Don't clear B$ where you do.

John
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 08:23am 24 Dec 2016
Copy link to clipboard 
Print this post

I changed it to 100ms delay before it cleared B$ but it makes no difference
I did try other values as well all the way from 10ms to 500ms in 10ms steps
STXG works all the time
STXT works "sometimes"
STXW works rarely
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2430
Posted: 04:23pm 24 Dec 2016
Copy link to clipboard 
Print this post

lew247: use the following code, changing the OPEN statement to match your serial port speed and number of stop bits.

' configure circular line buffer
' ******************************

Dim CB$(10)
head = 1
tail = 1

A$ = ""
B$ = ""

Open "COM1:9600,1024,isr,S2" As #1


' main loop
' *********

Do
If head<>tail Then ' collect sentence from buffer
B$ = CB$(tail)
tail = (tail Mod 10)+1
EndIf

if B$<>"" Print B$
B$ = ""

Loop
End ' end of program



Sub isr ' serial port handler
Local I, S$ length 80
S$ = Input$(72,#1) ' grab data into temporary buffer
A$ = Right$(A$,255-Len(S$))+S$ ' add to end of linear buffer
I = Instr(A$,Chr$(13)+Chr$(10)) ' check for eol marker (CR+LF)
If I<>0 Then
CB$(head) = Left$(A$,I-1) ' transfer into circular buffer
head = (head Mod 10)+1
If I<254 Then A$ = Mid$(A$,I+2) Else A$=""
EndIf
End Sub



cheers,
rob :-)
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 06:03am 25 Dec 2016
Copy link to clipboard 
Print this post

Nope it didn't work, I got nothing printed from the com port at all
I then put a very simple program in that prints everthing recieved by the com port to make sure the com port was working and the com ports is seeing what I'm expecting to see

This just doesn't work though.. Sorry
[code]Dim CB$(10)
head = 1
tail = 1

A$ = ""
B$ = ""

Open "COM1:19200,1024,isr,S2" As #1


' main loop
' *********

Do
If head<>tail Then ' collect sentence from buffer
B$ = CB$(tail)
tail = (tail Mod 10)+1
EndIf

if B$<>"" THEN Print B$
B$ = ""
ENDIF
Loop
End ' end of program



Sub isr ' serial port handler
Local I, S$ length 80
S$ = Input$(72,#1) ' grab data into temporary buffer
A$ = Right$(A$,255-Len(S$))+S$ ' add to end of linear buffer
I = Instr(A$,Chr$(13)+Chr$(10)) ' check for eol marker (CR+LF)
If I<>0 Then
CB$(head) = Left$(A$,I-1) ' transfer into circular buffer
head = (head Mod 10)+1
If I<254 Then A$ = Mid$(A$,I+2) Else A$=""
EndIf
End Sub[/code]
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2430
Posted: 06:52am 25 Dec 2016
Copy link to clipboard 
Print this post

can you post the code that is generating the messages (time, etc) at the other end?


cheers,
rob :-)
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 07:15am 25 Dec 2016
Copy link to clipboard 
Print this post

SetPin 16, DOUT 'HC-12 Power
Pin(16) = 1 'HC-12 Power OFF
Open "COM1:19200" As #2 Open port for HC-12 TX/RX

'When searching for GPS lock****

Pin(16) = 1 'HC-12 Power OFF
Print #2, "STXG" , "," , "*"

Once GPS LOCK is found and it's converted the time to local time

Print #2, "STXT " , "," , year,",",month,",",day,",",Left$(Time$, 2),",",Mid$(TIME$, 4,2),",",Right$(TIME$, 2),"*" ' Send correct date/time to Indoor unit to set RTC
Pause 100
Pin(16) = 0 'HC-12 Power OFF

'Once the weather is ready to transmit to the indoor unit
If secs Mod 15 = 0 Then 'every 15 seconds
Pin(16) = 0 ' Turn HC-12 Power ON
BATTERY ' Measure battery voltage
SendDataUpdate 'SEND DATA via Com Port
Pause 500
Pin(16) = 1 ' Turn HC-12 Power OFF
End If


Sub SendDataUpdate
LOCAL BB as integer
FOR BB = 1 to 10
Print #2, "STXW " , "," , bme280_read_temp()",",bme280_read_pressure()",",bme280_read_humidity()",",wheading",",wSpeed",",wgust",",hgust",",mmrain ",",mmrain2",",hightemp",",hightemp2",",lowtemp",",lowtemp2",",UVindex",",batt,"*"
pause 20
next BB


I have it sending the data to the com port 10 times at the moment in the hope of "one of the streams" being received properly and it will only transmit once every 5 mins or so instead of every 15 secondsEdited by lew247 2016-12-26
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2430
Posted: 06:47pm 25 Dec 2016
Copy link to clipboard 
Print this post

ok. try this program on the inside unit - it will print out everything that comes in, but also show the values of control characters:

Open "COM1:19200" As #1

Do
S$ = Input$(72,#1)

For I=1 To Len(S$)
C$=Mid$(S$,I,1)

If L$=Chr$(13) And C$<>Chr$(10) Then Print

If Asc(C$)<32 Then
Print "<";Str$(Asc(C$));">";
Else
Print C$;
EndIf

If L$=Chr$(13) And C$=Chr$(10) Then Print
L$=C$

Next i
Loop


also, i notice you seem to be powering the HC-12 down when not in use. when powering it up, you should allow a short delay for the onboard processor to boot:

Pin(16) = 1 'HC-12 Power ON
pause(50)
Print #2, "STXG" , "," , "*"


please post the output that you see.

cheers,
rob :-)
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 12:32am 26 Dec 2016
Copy link to clipboard 
Print this post

  robert.rozee said   ok. try this program on the inside unit - it will print out everything that comes in, but also show the values of control characters:


You are a genius!

Once I could see the control characters I found the problem, there were a few problems with the code I was transmitting in the formatting of it

The main problem is RECEIVING the Humidity reading from the BME280
I just don't get why, it's printing on the console perfectly, but as you can see from the output below it's not reliably sending on the com port for some reason

[quote]STXW <9>,<9> 20.76<9>,<9> 1017.88<9>,<9> 52.46<9>,<9> 87.9179<9>,<9> 0<9>,<9> 0.416667<9>,<9> 0.416667<9>,<9> 0<9>,<9> 0<9>,<9> 20.76<9>,<9> 0<9>,<9> 20.76<9>,<9> 0<9>,<9> 0.03<9>,<9> 3.54194<9>,<9>*<13><10>
STXW <9>,<9> 20.76<9>,<9> 1017.99<9>,<9>STXW <9>,<9> 20.76<9>,<9> 1017.94<9>,<9>STXW <9>,<9> 20.75<9>,<9> 1017.93<9>,<9>[/quote]

STXW is the start of the Transmit string and * SHOULD be the end
But as you can see after the first successful transmission, the string stops sending after the temp and pressure readings are sent.. the humidity does not send

Yet as you can see below from the console output it does send to the console
[quote]
*********************************
11:22:49
Battery Voltage 3.54 V
Direction: 87.39 Degrees
WindSpeed: 0 MPH
Gust 0.42 MPH
Highest Gust 0.42 MPH
Rain Today 0.00 mm
Rain Yesterday 0.00 mm
Temperature 20.69 C
Highest Temp 20.73 C
Lowest Temp 20.69 C
Hi Temp Yesterday 0.00 C
LOW Temp Yesterday 0.00 C
Pressure 1017.91 mB
HUMIDITY 52.76%
UV Index: 0.02
*******************************
*********************************
11:23:19
Battery Voltage 3.54 V
Direction: 87.2141 Degrees
WindSpeed: 0 MPH
Gust 0.42 MPH
Highest Gust 0.42 MPH
Rain Today 0.00 mm
Rain Yesterday 0.00 mm
Temperature 20.69 C
Highest Temp 20.73 C
Lowest Temp 20.69 C
Hi Temp Yesterday 0.00 C
LOW Temp Yesterday 0.00 C
Pressure 1017.92 mB
HUMIDITY 52.75%
UV Index: 0.02
*******************************
[/quote]

Here's the code I@m using to send to both the console and com port... I can't see why the com port stops working after the 1st transmit
BTW - the HC-12 is turned on 100Ms before the transmit code is sent to it so it is sending correctly and the recieve one is always on

[code]Sub SendDataUpdate
Print "*********************************"
Print Time$
Print " Battery Voltage ";Str$(batt,3,2);" V"
Print " Direction: ";Str$(wheading);" Degrees"
Print " WindSpeed: ";Str$(wSpeed); " MPH"
Print " Gust ";Str$(wgust,3,2); " MPH"
Print " Highest Gust ";Str$(hgust,3,2); " MPH"
Print " Rain Today ";Str$(mmrain,3,2);" mm"
Print " Rain Yesterday ";Str$(mmrain2,3,2);" mm"
Print " Temperature "; Str$(bme280_read_temp(),3,2)" C "
Print " Highest Temp ";Str$(hightemp,3,2) " C"
Print " Lowest Temp ";Str$(lowtemp,3,2) " C"
Print " Hi Temp Yesterday ";Str$(hightemp2,3,2) " C"
Print " LOW Temp Yesterday ";Str$(lowtemp2,3,2) " C"
Print " Pressure "; Str$(bme280_read_pressure(),3,2)" mB "
Print " HUMIDITY "; Str$(bme280_read_humidity(),3,2)"%"
Print " UV Index: ";Str$(UVindex,3,2)
Print "*******************************"
Print #2, "STXW ",",",bme280_read_temp(),",",bme280_read_pressure(),",",bme280_read_humidity(),",",wheading,",",wSpeed,",",wgust,",",hgu st,",",mmrain,",",mmrain2,",",hightemp,",",hightemp2,",",lowtemp,",",lowtemp2,",",UVindex,",",batt,",","*"

End Sub[/code]

I did also try changing the humidity to Str$(bme280_read_pressure(),3,2) but it makes no difference

EDIT: If I delete bme280_read_humidity() totally then the STXW works perfectly
Edited by lew247 2016-12-27
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2430
Posted: 01:07am 26 Dec 2016
Copy link to clipboard 
Print this post

i suspect you may be hitting a buffer overflow with that extremely long line. there is also an issue with using commas to separate items. this code:

Print 123, ",", 456, "hello"

produces this output:

123<9>,<9> 456<9>hello<13><10>

the <9> are tab characters, that are meaningful only to teraterm.

try something like this instead:

Print #2, "STXW ," bme280_read_temp() "," bme280_read_pressure() ",";
Print #2, bme280_read_humidity() "," wheading "," wSpeed ",";
Print #2, wgust "," hgu st "," mmrain "," mmrain2 "," hightemp ",";
Print #2, hightemp2 "," lowtemp "," lowtemp2 "," UVindex "," batt ",*"


i've removed all the commas between items being printed. then only commas left are the ones after the #2. print is quite happy to have a list of items that are just separated with spaces - the spaces are not printed.

all bar the first line end in a ; so that theputput appears as a single output line.

cheers,
rob :-)Edited by robert.rozee 2016-12-27
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 01:19am 26 Dec 2016
Copy link to clipboard 
Print this post

Thanks, I didn't realise that using ; allowed the output to be appended without putting in a cr/lf

Unfortunately it made no difference, IF I remove the Humidity then it works perfectly, but when I put Humidity back in it fails

What I don't get is the humidity prints to the console perfectly, it just won't send over the com port
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2430
Posted: 01:37am 26 Dec 2016
Copy link to clipboard 
Print this post

i still feel you may be overflowing a buffer. how about if you split the data between several id tags:

Print #2, "STXW1," bme280_read_temp() "," bme280_read_pressure() ",*"
pause 50
Print #2, "STXW2," bme280_read_humidity() "," wheading "," wSpeed ",*"
pause 50
Print #2, "STXW3," wgust "," hgu st "," mmrain "," mmrain2 "," hightemp ",*"
pause 50
Print #2, "STXW4," hightemp2 "," lowtemp "," lowtemp2 "," UVindex "," batt ",*"
pause 50


remember, the HC-12 also has a limited internal buffer (possibly only 60 bytes), so blasting it with a long string of text may cause characters to be lost.

on the outdoors micromite, try adding in 2 stop bits to the open command. in theory having 2 stop bits should slightly slow the rate of transmittion of serial data from the micromites, but not affect the received rate.


cheers,
rob :-)
Edited by robert.rozee 2016-12-27
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 02:18am 26 Dec 2016
Copy link to clipboard 
Print this post

I'm totally confused now
I tried as an experiment swapping the Humidity place in the TX string
So now it sends Temp, Humidity and then pressure

It now receives the Humidty but It's still failing after the 2nd item is sent (this time it's not recieving the pressure reading

Print #2, "STXW" "," bme280_read_temp() "," bme280_read_humidity() "," bme280_read_pressure()
This "should" work and I don't understand why its stopping after the 2nd item is transmitted I've swapped humidity and pressure round and whichever is the 2nd item sends ok but not the 3rd
It has to be something stupidly simple but I'm not seeing it
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2430
Posted: 02:34am 26 Dec 2016
Copy link to clipboard 
Print this post

try using the following on the outside unit:

Open "COM1:19200,S2" As #2



cheers,
rob :-)
 
     Page 17 of 25    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025