Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 17:59 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 3 of 5    
Author Message
lew247

Guru

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

Thanks Message me if thats easier
Lew
 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 12:37pm 29 Nov 2017
Copy link to clipboard 
Print this post

Here it is changed a little.
It had a typo in the open command (closing quote in wrong place).

OPTION AUTORUN ON 'Autorun upon power up (after first run)
option explicit
Dim Esp$ = "" 'Serial 2 ESP8266 input string
Dim EspLine% = 0 'Serial 2 ESP8266 line received flag
Dim Count% = 0 'Counter to put stuff on screen
Open "COM2:9600, 1024, EspIsr" As #2 'Open port for ESP8266
' *** Start of Main Program ***
Do
' *** Check Serial Ports for Data ***
If EspLine% = 1 Then 'Complete ESP line received
Print 'Start new line after printing waiting ****
Count% = 0 'Reset count to start waiting message again
Print Esp$ 'Print line to check it
'Add processing for line received here
Esp$ = "" 'Clear line ready for next one to come in
EspLine% = 0 'Clear line received flag so we can get next line
Else
'Just print something to show we are running
If Count% = 0 Then Print "Waiting: " ;
Count% = 1
Print "z" ; 'Print z to keep it short on same line
Pause 500 'Might cause issues with serial port buffering
End If
Loop
' *** COM 2 Input ***
Sub EspIsr ' serial port 2 ESP8266 interrupt handler
Local T$ length 10
If EspLine = 0 Then 'Dont get more if previous line not handled
T$ = Input$(1,#2) 'Get data into temp buffer
Print "ESP char: " T$ " Received" 'Just to check for other unwanted chars
Select Case T$
Case "*"
EspLine% = 1 'Set flag to say we have finished this line
Else
Esp$ = Esp$ + T$ 'Add new char to ESP input string
End Select
End If
End Sub
End
Edited by Azure 2017-11-30
 
lew247

Guru

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

If I comment out the
"If EspLine = 0 Then"
in the SUB it works
However it's printing every character that comes into the com port on a new line with the work "Received" after the character
an example of part of what's received
[quote]ESP char: , Received
ESP char: 9 Received
ESP char: 8 Received
ESP char: , Received
ESP char: 2 Received
ESP char: , Received
ESP char: 2 Received
ESP char: 9 Received
ESP char: 2 Received
ESP char: , Received
ESP char: * Received[/quote]
Edited by lew247 2017-11-30
 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 01:47pm 29 Nov 2017
Copy link to clipboard 
Print this post

Seems to be a problem with the string and select case.
Works with If Then and since end char is "*" it will work.
Might need to consider what it would do if string gets corrupted somehow during tx.
This code works for me. Error you mentioned was variable missing it's %.


OPTION AUTORUN ON 'Autorun upon power up (after first run)
option explicit
Dim Esp$ = "" 'Serial 2 ESP8266 input string
Dim EspLine% = 0 'Serial 2 ESP8266 line received flag
Dim Count% = 0 'Counter to put stuff on screen
Open "COM2:9600, 1024, EspIsr" As #2 'Open port for ESP8266
' *** Start of Main Program ***
Do
' *** Check Serial Ports for Data ***
If EspLine% = 1 Then 'Complete ESP line received
Print 'Start new line after printing waiting ****
Count% = 0 'Reset count to start waiting message again
Print Esp$ 'Print line to check it
'Add processing for line received here
Esp$ = "" 'Clear line ready for next one to come in
EspLine% = 0 'Clear line received flag so we can get next line
Else
'Just print something to show we are running
If Count% = 0 Then Print "Waiting: " ;
Count% = 1
Print "z" ; 'Print z to keep it short on same line
Pause 500 'Might cause issues with serial port buffering
End If
Loop
' *** COM 2 Input ***
Sub EspIsr ' serial port 2 ESP8266 interrupt handler
Local T$ length 10
If EspLine% = 0 Then 'Dont get more if previous line not handled
T$ = Input$(1,#2) 'Get data into temp buffer
If T$ = "*" Then
EspLine% = 1 'Set flag to say we have finished this line
Else
Esp$ = Esp$ + T$ 'Add new char to ESP input string
End If
End If
End Sub
End
Edited by Azure 2017-11-30
 
lew247

Guru

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

It's getting close, I had to modify it slightly to get it to run
[code]OPTION AUTORUN ON 'Autorun upon power up (after first run)
option explicit
Dim Esp$ = "" 'Serial 2 ESP8266 input string
Dim EspLine = 0 'Serial 2 ESP8266 line received flag
Dim Count = 0 'Counter to put stuff on screen
Open "COM2:9600, 1024, EspIsr" As #2 'Open port for ESP8266
' *** Start of Main Program ***
Do
' *** Check Serial Ports for Data ***
If EspLine = 1 Then 'Complete ESP line received
Print "GOT TO HERE" 'Start new line after printing waiting ****
Count = 0 'Reset count to start waiting message again
Print Esp$ 'Print line to check it
'Add processing for line received here
Esp$ = "" 'Clear line ready for next one to come in
EspLine = 0 'Clear line received flag so we can get next line
Else
'Just print something to show we are running
If Count = 0 Then Print "Waiting: " ;
Count = 1
Print "z" ; 'Print z to keep it short on same line
Pause 500 'Might cause issues with serial port buffering
End If
Loop
' *** COM 2 Input ***
Sub EspIsr ' serial port 2 ESP8266 interrupt handler
Local T$ length 10
If EspLine = 0 Then 'Dont get more if previous line not handled
T$ = Input$(1,#2) 'Get data into temp buffer
If T$ = "*" Then
EspLine = 1 'Set flag to say we have finished this line
Else
Esp$ = Esp$ + T$ 'Add new char to ESP input string
End If
End If
PRINT T$; " T$ TEST "
End Sub
End[/code]

The only problem is it never comes out of the ESPIsr SUB, it keeps printing "" T$ TEST " in a never ending loop
 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 03:55pm 29 Nov 2017
Copy link to clipboard 
Print this post

It ran on my unit, so not sure what stopped it working in the changes made.

Change:

If Count = 0 Then Print "Waiting: " ;


To:

If Count = 0 Then
Print "Waiting: " ;

If then is structured incorrectly,
print should be on next line as part of the multiline "If Then"

Can't see why it is stuck in isr if that doesn't help.

*Edit:
It seems like it has received a line, expand that isr print to be:

PRINT T$; " T$ TEST "; EspLine


It should print 0 if it is still building up a line and 1 when it has found *.
Edited by Azure 2017-12-01
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 05:20pm 29 Nov 2017
Copy link to clipboard 
Print this post

I wonder if it's because the ESP is transmitting every 15 seconds?

With the mod's you suggested it's stuck in a loop printing
[code]
T$ TEST 1
T$ TEST 1[/code]
continuously
Edited by lew247 2017-12-01
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 06:52pm 29 Nov 2017
Copy link to clipboard 
Print this post

Deleted as it had errorsEdited by lew247 2017-12-01
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 11:25am 30 Nov 2017
Copy link to clipboard 
Print this post

I'm just going to give up on the whole thing, I can't get com2 working properly no matter what I try
Thanks everyone who tried to help with thisEdited by lew247 2017-12-01
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3292
Posted: 12:16pm 30 Nov 2017
Copy link to clipboard 
Print this post

I have not followed this thread in detail but it seems to involve strings and subs. In this case this known bug might be relevant:
  Quote  There is a bug which could corrupt a string which is returned by a function
when that function is used in the argument list for another function. The
workaround is to assign the output of the function to a string variable then
use that variable as the argument to the other function.


This will be fixed in the next release.

Geoff
Geoff Graham - http://geoffg.net
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 12:24pm 30 Nov 2017
Copy link to clipboard 
Print this post

Thanks Geoff
Basically what I was trying to do is receive data in COM2 then parse that data
and a function to tell when the data was received and then another function to parse the data
I can get it working on COM1 perfectly, but when I try and replicate it on COM 2 (different data) it just won't work
 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 12:41pm 30 Nov 2017
Copy link to clipboard 
Print this post

you just posted a good thought. just to eliminate things have you tried using different com ports on the mm for talking to the esp?

Sorry busy with other stuff at the moment. I will do some tests next week as that code should work. I need to setup a second machine to simulate the esp data streams you posted while my main development notebook machine (which has only 1 USB port) is the console interface and I don't have time for that for a few days.
 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 01:15pm 30 Nov 2017
Copy link to clipboard 
Print this post

So I am prepared when I get to play with this issue, can you just explain how the
esp8266 is sending data.

Is it a line (day at a time) up to the end of line "*" character then in 15 seconds it does the next days forecast and so on, or is it all days together one day directly after the other repeated every 15 seconds?
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 01:27pm 30 Nov 2017
Copy link to clipboard 
Print this post

This is exactly what it's transmitting - one line at a time, getting the data for the tomorrow and transmitting it, getting the data for Day 2 and transmitting it and finally getting the data for Day 2 and transmitting it then repeating this every 15 seconds
15 seconds for test purposes, this will be a lot longer delay "eventually"

[quote]STX1,broken clouds,04n,*
STX2,scattered clouds,03d,Fri Dec 01 11:00:00 2017 ,-0.76,3.91,1024.77,87,4,4,*
STX3,light rain,10d,Sat Dec 02 11:00:00 2017 ,2.08,7.43,1025.56,97,3,307,*
STX4,light rain,10d,Sun Dec 03 11:00:00 2017 ,4.83,8.5,1027.69,98,3,317,*[/quote]

EDITED:
I've changed the ESP output so it has the same info in the first 3 sections
STX1 = todays data
STX2 = tomorrows and so on
This "SHOULD" make it easier to parse, if I ever go back to trying to get COM2 working properlyEdited by lew247 2017-12-02
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 09:00pm 30 Nov 2017
Copy link to clipboard 
Print this post

When I'm not sure if something is getting to all the right places,
I often add a line,

[code]Print Crash
or just
? cc[/code]

As Crash or cc is an undefined variable when OPTION EXPLICIT is used,
it works as a simple test to see it the code has got to a line in question.

At times I've also done a similar thing with the Trace on, which will allow you to see previous lines that were executed & looped thru.

Bit of a primitive debugging technique, but useful at times.

Phil.Edited by Phil23 2017-12-02
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 09:07pm 30 Nov 2017
Copy link to clipboard 
Print this post

I "THINK" I might be getting somewhere
This is working at the moment, I just have to incorporate it into the main program and parse the sentences but I'll try that tomorrow after I get up
[code]Open "COM2:9600" As #2
Do
Do
BT$=Input$(1,#2)
If By$=Chr$(10) Then BT$="" 'Get rid of a LineFeed (LF)!
If BT$=Chr$(42) Then Exit '"*" to signify end of message line
MSG$=MSG$+BT$
Loop
MSG1$ = MSG$
MSG$=""
print MSG1$,"COM2 Message String"
Loop[/code]
 
lew247

Guru

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

I've spent that past 8 hours trying to get the above code incorporated into my existing program
It will work
BUT
it will stop the clock for 4 seconds every time it gets data

Is there a way to incorporate this into an existing program without this DO loop?
I think this is why it's taking so long to run and move on
The original program already has a DO/LOOP and this 2nd one I "think" is making it pause

[code]Open "COM2:9600" As #2
DO
BT$=Input$(1,#2)
If By$=Chr$(10) Then BT$="" 'Get rid of a LineFeed (LF)!
If BT$=Chr$(42) Then Exit '"*" to signify end of message line
MSG$=MSG$+BT$
Loop
MSG1$ = MSG$
MSG$=""
print MSG1$[/code]

On the plus side, I found out why COM2 is double printing everything, it was a bug in the ESP8266 program, it was sending the data twice, that's corrected now so it's only sending onceEdited by lew247 2017-12-02
 
Azure

Guru

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

I am away this weekend so cannot play with hardware.

My preference would be (still) to get it running in the background using an ISR.

I have written a simpler ISR below that the main code can easily check when the line has arrived. If that works it can be built up to do more (and remove and unwanted characters).

OPTION AUTORUN ON 'Autorun upon power up (after first run)
option explicit
Dim Esp$ = "" 'Serial 2 ESP8266 input string
Dim Count% = 0 'Counter just for something in main loop
Open "COM2:9600,, EspIsr" As #2 'Open port for ESP8266
' *** Start of Main Program ***
Do
For Count% = 1 to 100 'Do something in main loop
Next
If Esp$ <> "" Then 'Check if line of ESP data has arrived
Print Esp$
Esp$ = ""
End If
Loop

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

 
lew247

Guru

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

I couldn't get it working
If you look at the print statements I added

both print statements in the interrupt sub print out the following
111 with nothing printed before it
each character received from the com port followed by,222
The "print command" never gets printed
[code]OPTION AUTORUN ON 'Autorun upon power up (after first run)
option explicit
Dim Esp$ = "" 'Serial 2 ESP8266 input string
Dim Count% = 0 'Counter just for something in main loop
Open "COM2:9600,, EspIsr" As #2 'Open port for ESP8266
' *** Start of Main Program ***
Do
For Count% = 1 to 100 'Do something in main loop
Next
If Esp$ <> "" Then 'Check if line of ESP data has arrived
Print Esp$, "Print command"
Esp$ = ""
End If
Loop

' *** COM 2 Input ***
Sub EspIsr ' serial port 2 ESP8266 interrupt handler
Local T$ length 200
Local ESPT$
T$ = Input$(1,#2) 'Get data into temp buffer
If T$ = "*" Then
ESP$ = ESPT$ 'Save string to global now that it is complete
print EspT$,"111"
Else
EspT$ = EspT$ + T$ 'Add new char to ESP Temp input string
print EspT$, "222"
End If
End Sub[/code]
 
Azure

Guru

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

I managed to run a doctored version (cheating input)
The problem was that the EspT$ is being recreated each time it is not static. This caused it only ever to be the last character. I changed it to global, that should fix things, also needed to clear temp string once line completed.

Here it is modified:
OPTION AUTORUN ON 'Autorun upon power up (after first run)
option explicit
Dim Esp$ = "", EspT$ = "" 'Serial 2 ESP8266 input string
Dim Count% = 0 'Counter just for something in main loop
Open "COM2:9600,, EspIsr" As #2 'Open port for ESP8266
' *** Start of Main Program ***
Do
For Count% = 1 to 100 'Do something in main loop
Next
If Esp$ <> "" Then 'Check if line of ESP data has arrived
Print Esp$, "Print command"
Esp$ = ""
End If
Loop

' *** COM 2 Input ***
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
' print EspT$,"111"
Else
EspT$ = EspT$ + T$ 'Add new char to ESP Temp input string
' print EspT$, "222"
End If
End Sub


Edit: missed $ sign in first postEdited by Azure 2017-12-03
 
     Page 3 of 5    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025