Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 17:42 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 : Com Port Problems

     Page 4 of 5    
Author Message
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 02:16pm 01 Dec 2017
Copy link to clipboard 
Print this post

That works perfectly thanks
I'll try and incorporate it into the main program now
 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 02:18pm 01 Dec 2017
Copy link to clipboard 
Print this post

So, assuming this now gets you receiving your ESP strings:
Does this mean we need to strip out the CR/LF or other characters?

Also there is a flaw in this simplistic type of transfer. Just so you are aware:
There could be a corruption in the transfer.
This could mean lost characters, corrupted (unknown/unexpected characters), missing end of line character, etc.

These sort of conditions need to be thought about how you want to handle them.
Because it is a regularly repeated stream you can most likely just drop the bad line.
Important thing it to try and make sure the receivers code does not get out of whack because of a receive data error.
It could/should check for valid data/characters (in the main code), it could reset the string being assembled after some time to handle no end of line character received. Or you can jsut hope those things never cause you a problem.
 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 02:22pm 01 Dec 2017
Copy link to clipboard 
Print this post

  lew247 said   That works perfectly thanks
I'll try and incorporate it into the main program now

That's great news. Little steps are good steps.
If you want to post the updated code I can look over it when I get a moment.

I suspect your com1 isr can be made simpler too. Different end of line checking but similar concept isn't it. Receive a string up to some end of line delimeter(s) and pass it onto the main program.
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 02:45pm 01 Dec 2017
Copy link to clipboard 
Print this post

I made a tiny mod but this works perfectly now and prints out each line of data that's coming into Com2 on a new line
[code]OPTION AUTORUN ON 'Autorun upon power up (after first run)
option explicit
Dim Esp$ = "", EspT$ = "" 'Serial 2 ESP8266 input string
Open "COM2:9600,, EspIsr" As #2 'Open port for ESP8266
' *** Start of Main Program ***
DO
If Esp$ <> "" Then 'Check if line of ESP data has arrived
serial2 'If data in Com1 then GOSUB Serial2
LOOP

Sub EspIsr ' serial port 2 ESP8266 interrupt handler
Local T$ length 200
T$ = Input$(1,#2) 'Get data into temp buffer
If T$ = "*" Then
Esp$ = EspT$ 'Save string to global now that it is complete
EspT$ = "" 'Clear ESP Temp string for next line
Else
EspT$ = EspT$ + T$ 'Add new char to ESP Temp input string
End If
End Sub

Sub serial2
Print Esp$, "Print command"
Esp$ = ""
END SUB[/code]

With reference to what you said earlier
EVERY line of valid data starts with the characters STX and ends with a *
 
lew247

Guru

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

Azure sent you a message*
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 11:54am 02 Dec 2017
Copy link to clipboard 
Print this post

I've now got to the stage where my Serial2 sub is getting the info from the com port
but It won't go onto SUB T1

This is the complete code
[code]OPTION EXPLICIT
DIM FieldArray$(50) 'Max number of fields recieved by serial port 2
Dim CB$(10)
Dim ESP$, ESPT$ 'Serial 2 ESP8266 input string
Open "COM2:9600,200, ESPIsr" As #2 'Open port for ESP8266

DO
If ESP$ <> "" Then 'Check if line of ESP data has arrived
serial2 'If data in Com1 then GOSUB Serial2
ESP$ = ""
endif
LOOP

Sub ESPIsr ' serial port 2 ESP8266 interrupt handler
Local T$ length 85
T$ = Input$(2,#2) 'Get data into temp buffer
If T$ = CHR$(42) Then
ESP$ = ESPT$ 'Save string to global now that it is complete
ESPT$ = "" 'Clear ESP Temp string for next line
Else
ESPT$ = ESPT$ + T$ 'Add new char to ESP Temp input string
End If
End Sub

SUB SERIAL2 'Com2 ESP8266 Weather Forecast
LOCAL M as INTEGER
M = GetFieldArray(ESP$)
IF UCASE$(FieldArray$(0)) = "STX1" Then T1 'test to see if this works
PRINT ESP$, " ESP$ - this is what's received from COM2"
print UCASE$(FieldArray$(0)), " UCASES - test print"
PRINT Left$(ESP$,4), " Left FieldArray$(0) test print"
PRINT FieldArray$(0), " FieldArray1$(0) test print"
IF FieldArray$(0)="STX1" THEN T1 'If the 1st array is "Today" then GOSUB T1
IF UCASE$(FieldArray$(0)) = "STX1" Then T1 'test to see if this works
End Sub

SUB T1
PRINT "GOT HERE SUB T1"
END SUB

Function GetFieldArray( Record$, Delimiter$, KeepQuotes )
Local Index, Char, InQuote, Count
InQuote = 0
Count = 0
FieldArray$(Count) = ""
If Delimiter$ = "" Then Delimiter$ = ","
For Index = 1 To Len(Record$)
Char = Asc(Mid$(Record$, Index, 1))
If Char = 34 Then InQuote = Not InQuote
If Not InQuote And Instr(Delimiter$, Chr$(char)) >= 1 Then
Count = Count + 1
FieldArray$(Count) = ""
Else
If Char <> 34 Or KeepQuotes Then
FieldArray$(Count) = FieldArray$(Count) + Chr$(char)
EndIf
EndIf
Next
GetFieldArray = Count + 1
End Function[/code]

This is what's printed when I run the program
[quote]
> run
STX4,light rain,10d,Tue Dec 05 11:00:00 2017
,5.63,7.91,1029.88,95,4,235, ESP$ - this is what's received from COM2
STX4 UCASES - test print
STX4 Left FieldArray$(0) test print
STX4 FieldArray1$(0) test print

STX1,mist,50d, ESP$ - this is what's received from COM2

STX1 UCASES - test print

ST Left FieldArray$(0) test print

STX1 FieldArray1$(0) test print

STX2,light rain,10d,Sun Dec 03 11:00:00 2017
,5.18,8.05,1026.11,98,3,329, ESP$ - this is what's received from COM2

STX2 UCASES - test print

ST Left FieldArray$(0) test print

STX2 FieldArray1$(0) test print

STX3,light rain,10d,Mon Dec 04 11:00:00 2017
,6.63,8.04,1031.39,99,4,297, ESP$ - this is what's received from COM2

STX3 UCASES - test print

ST Left FieldArray$(0) test print

STX3 FieldArray1$(0) test print

STX4,light rain,10d,Tue Dec 05 11:00:00 2017
,5.63,7.91,1029.88,95,4,235, ESP$ - this is what's received from COM2

STX4 UCASES - test print

ST Left FieldArray$(0) test print

STX4 FieldArray1$(0) test print[/quote]

Any ideas why it won't get to SUB T1?
The Fieldarray function works perfectly on COM1 so I'm pretty sure that isn't the problemEdited by lew247 2017-12-03
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 12:27pm 02 Dec 2017
Copy link to clipboard 
Print this post

Why is this used?

T$ = Input$(2,#2)

You look to be requiring 2 chars but one is all you probably want at a time.

John
 
lew247

Guru

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

I thought the number was a temp buffer number, I changed it to T$ = Input$(1,#2)
and the result is still the same
For some reason when I print PRINT Left$(ESP$,4) it doesn't print all 4 characters
EDIT:if I PRINT FieldArray$(0) or FieldArray$(1) 0r FieldArray$(2)
It prints the correct info

Edited by lew247 2017-12-03
 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 01:18pm 02 Dec 2017
Copy link to clipboard 
Print this post

Run the following code for SERIAL2 sub as is, no variable changes, no comment changes.

Sub SERIAL2 'Com2 ESP8266 Weather Forecast
LOCAL INTEGER M, C
M = GetFieldArray(ESP$)
IF UCASE$(FieldArray$(0)) = "STX1" Then T1
PRINT ESP$; " ESP$ - received from COM2"
PRINT M; " Entries found in ESP$"
For C = 0 to M
PRINT UCASE$(FieldArray$(C)); " entry "; C; ", length "; LEN(FieldArray$(C)
NEXT
End Sub


Fix the typo added to the ISR input:
T$ = Input$(1,#2) 'Get data into temp buffer


Post the results

Edit* changed LEN to use C, mistyped it as 0 when first postedEdited by Azure 2017-12-03
 
lew247

Guru

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

It's a bit long - I let it run many times but only pasted 2 loops
for some reason the 1st loop it got to the SUB T1 ONCE - but never returned back to it and I let the program run for 5 minutes
[quote]> run
GOT HERE SUB T1
STX1,light intensity drizzle,09d, ESP$ - received from COM2
4 Entries found in ESP$
STX1 UCASE$ entry 0, length 4
LIGHT INTENSITY DRIZZLE UCASE$ entry 1, length 4
09D UCASE$ entry 2, length 4
UCASE$ entry 3, length 4
UCASE$ entry 4, length 4

STX2,light rain,10d,Sun Dec 03 11:00:00 2017
,5.18,8.05,1026.11,98,3,329, ESP$ - received from COM2
11 Entries found in ESP$

STX2 UCASE$ entry 0, length 6
LIGHT RAIN UCASE$ entry 1, length 6
10D UCASE$ entry 2, length 6
SUN DEC 03 11:00:00 2017
UCASE$ entry 3, length 6
5.18 UCASE$ entry 4, length 6
8.05 UCASE$ entry 5, length 6
1026.11 UCASE$ entry 6, length 6
98 UCASE$ entry 7, length 6
3 UCASE$ entry 8, length 6
329 UCASE$ entry 9, length 6
UCASE$ entry 10, length 6
UCASE$ entry 11, length 6

STX3,light rain,10d,Mon Dec 04 11:00:00 2017
,6.63,8.04,1031.39,99,4,297, ESP$ - received from COM2
11 Entries found in ESP$

STX3 UCASE$ entry 0, length 6
LIGHT RAIN UCASE$ entry 1, length 6
10D UCASE$ entry 2, length 6
MON DEC 04 11:00:00 2017
UCASE$ entry 3, length 6
6.63 UCASE$ entry 4, length 6
8.04 UCASE$ entry 5, length 6
1031.39 UCASE$ entry 6, length 6
99 UCASE$ entry 7, length 6
4 UCASE$ entry 8, length 6
297 UCASE$ entry 9, length 6
UCASE$ entry 10, length 6
UCASE$ entry 11, length 6

STX4,light rain,10d,Tue Dec 05 11:00:00 2017
,5.63,7.91,1029.88,95,4,235, ESP$ - received from COM2
11 Entries found in ESP$

STX4 UCASE$ entry 0, length 6
LIGHT RAIN UCASE$ entry 1, length 6
10D UCASE$ entry 2, length 6
TUE DEC 05 11:00:00 2017
UCASE$ entry 3, length 6
5.63 UCASE$ entry 4, length 6
7.91 UCASE$ entry 5, length 6
1029.88 UCASE$ entry 6, length 6
95 UCASE$ entry 7, length 6
4 UCASE$ entry 8, length 6
235 UCASE$ entry 9, length 6
UCASE$ entry 10, length 6
UCASE$ entry 11, length 6

STX1,light intensity drizzle,09d, ESP$ - received from COM2
4 Entries found in ESP$

STX1 UCASE$ entry 0, length 6
LIGHT INTENSITY DRIZZLE UCASE$ entry 1, length 6
09D UCASE$ entry 2, length 6
UCASE$ entry 3, length 6
5.63 UCASE$ entry 4, length 6

STX2,light rain,10d,Sun Dec 03 11:00:00 2017
,5.18,8.05,1026.11,98,3,329, ESP$ - received from COM2
11 Entries found in ESP$

STX2 UCASE$ entry 0, length 6
LIGHT RAIN UCASE$ entry 1, length 6
10D UCASE$ entry 2, length 6
SUN DEC 03 11:00:00 2017
UCASE$ entry 3, length 6
5.18 UCASE$ entry 4, length 6
8.05 UCASE$ entry 5, length 6
1026.11 UCASE$ entry 6, length 6
98 UCASE$ entry 7, length 6
3 UCASE$ entry 8, length 6
329 UCASE$ entry 9, length 6
UCASE$ entry 10, length 6
UCASE$ entry 11, length 6

STX3,light rain,10d,Mon Dec 04 11:00:00 2017
,6.63,8.04,1031.39,99,4,297, ESP$ - received from COM2
11 Entries found in ESP$

STX3 UCASE$ entry 0, length 6
LIGHT RAIN UCASE$ entry 1, length 6
10D UCASE$ entry 2, length 6
MON DEC 04 11:00:00 2017
UCASE$ entry 3, length 6
6.63 UCASE$ entry 4, length 6
8.04 UCASE$ entry 5, length 6
1031.39 UCASE$ entry 6, length 6
99 UCASE$ entry 7, length 6
4 UCASE$ entry 8, length 6
297 UCASE$ entry 9, length 6
UCASE$ entry 10, length 6
UCASE$ entry 11, length 6

STX4,light rain,10d,Tue Dec 05 11:00:00 2017
,5.63,7.91,1029.88,95,4,235, ESP$ - received from COM2
11 Entries found in ESP$

STX4 UCASE$ entry 0, length 6
LIGHT RAIN UCASE$ entry 1, length 6
10D UCASE$ entry 2, length 6
TUE DEC 05 11:00:00 2017
UCASE$ entry 3, length 6
5.63 UCASE$ entry 4, length 6
7.91 UCASE$ entry 5, length 6
1029.88 UCASE$ entry 6, length 6
95 UCASE$ entry 7, length 6
4 UCASE$ entry 8, length 6
235 UCASE$ entry 9, length 6
UCASE$ entry 10, length 6
UCASE$ entry 11, length 6[/quote]

This is the webpage printout of the info being transmitted by the ESP8266
[quote]STX1,light intensity drizzle,09d,*
STX2,light rain,10d,Sun Dec 03 11:00:00 2017 ,5.18,8.05,1026.11,98,3,329,*
STX3,light rain,10d,Mon Dec 04 11:00:00 2017 ,6.63,8.04,1031.39,99,4,297,*
STX4,light rain,10d,Tue Dec 05 11:00:00 2017 ,5.63,7.91,1029.88,95,4,235,*[/quote]Edited by lew247 2017-12-04
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 02:43pm 02 Dec 2017
Copy link to clipboard 
Print this post

  lew247 said   I thought the number was a temp buffer number, I changed it to T$ = Input$(1,#2)
and the result is still the same


The code looks more likely to be right, though. Sooner or later it'll be right :)

  lew247 said   For some reason when I print PRINT Left$(ESP$,4) it doesn't print all 4 characters


Fair chance that's a clue. Any "missing" char is probably a space, CR, LF, or the like. (You can easily print the length in case there aren't 4 chars and/or the ASC value of each to help with debug.)

John
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 03:57pm 02 Dec 2017
Copy link to clipboard 
Print this post

This is what the ESP is sending to the com port
It's using EspBasic but very similar to MMBasic
The PRINT command sends it to the com port
[quote]print "STX1" & "," & desc & "," & icon & "," & "*"[/quote]
I've no idea where the extra 2 characters are coming from (if there are 2 extra and its not a problem with the MMBasic code)Edited by lew247 2017-12-04
 
lew247

Guru

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

I changed the ESP code to serialprint so it ONLY sends it to the serial port and the result is the same
The 1st time the code loops it gets to SUB T1
after that it never gets there again
EDIT:
I changed the ESP code so It ONLY sends 1 string to the com port now and not 4 seperate ones
and the result is the same
Edited by lew247 2017-12-04
 
lew247

Guru

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

What I don't understand is it seems the com port is seeing more than it is?
[code]STX1,drizzle rain,09d, ESP$ - received from COM2
4 Entries found in ESP$
STX1 UCASE$ entry 0, length 4
DRIZZLE RAIN UCASE$ entry 1, length 4
09D UCASE$ entry 2, length 4
UCASE$ entry 3, length 4
UCASE$ entry 4, length 4
[/code]
STX1,drizzle rain,09d is what the com port is receiving, which is what the ESP is transmitting

Any idea what UCASE$ entry 3, length 4 and UCASE$ entry 4, length 4 could be or where they are coming from? there is only 3 items being received not 5

the 4th item is the character * which is being stripped out and not showing as it's using it as an end of EOL characterEdited by lew247 2017-12-04
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 05:39pm 02 Dec 2017
Copy link to clipboard 
Print this post

Your print does not end with semi-colon so you get CR LF as well.

John
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 06:25pm 02 Dec 2017
Copy link to clipboard 
Print this post

  JohnS said   Your print does not end with semi-colon so you get CR LF as well.
John

With ESPBasic it doesn't need the semicolon
[quote]SERIALPRINT:
Will output text or a variable to the serial interface only. No new line or added.
serialprint {value or var}

SERIALPRINTLN:
Will output text or a variable to the serial interface only. Will terminate with a new line.
serialprintln {value or var}

PRINT:
Will output text or a variable to the serial interface and the browser with a new line.
print {value or var}[/quote]

I used to have it as PRINT, now it does SERIALPRINT which has no line feedEdited by lew247 2017-12-04
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 06:47pm 02 Dec 2017
Copy link to clipboard 
Print this post

I really don't know how or why but this is now working perfectly
It had to be a problem with the ESP rather than the MM
all I did was clear the code in the ESP and reprogram the exact same code again - which I have also done at least 6 times a day while trying to figure this out

EDIT: Working - it now gets to SuB T1Edited by lew247 2017-12-04
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 07:10pm 02 Dec 2017
Copy link to clipboard 
Print this post

Thank you to everyone who helped, I feel I need to say sorry for such a long post.
It's now working perfectly, although nothing is different from what Azure asked me to post a few posts back - other than I got rid of all the print statements once it worked.
It seems the problem was with the ESP8266 (sorry) I'm not 100% certain but it seems the only logical reason it wasn't working and sending 2 extra characters sometimes.
I really dont understand it when its now running the exact same code, just reprogrammed.
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 07:13pm 02 Dec 2017
Copy link to clipboard 
Print this post

The result of all the above - now showing the correct weather icon and description for today (and the rest of the days once programmed)



 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 12:09am 03 Dec 2017
Copy link to clipboard 
Print this post

That's great news Lewis, glad it is running.

I am still away but will look at the printout that you posted (which I asked you to do) in case it gives any clues. Would be good to see if we can understand what was happening even though it now works.
 
     Page 4 of 5    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025