Home  |  Contents 
Microcontroller and PC projects
  Forum Index : Microcontroller and PC projects         Section
Subject Topic: Newie got a few questions Post ReplyPost New Topic
<< Prev Page of 33 Next >>
Author
Message << Prev Topic | Next Topic >>
lew247
Guru
Guru
Avatar

Joined: 23 December 2015
Location: United Kingdom
Online Status: Offline
Posts: 790
Posted: 15 August 2016 at 12:33pm | IP Logged Quote lew247

Thanks, I'll be able to sort it out with that help


Back to Top View lew247's Profile Search for other posts by lew247
 
lew247
Guru
Guru
Avatar

Joined: 23 December 2015
Location: United Kingdom
Online Status: Offline
Posts: 790
Posted: 15 August 2016 at 1:08pm | IP Logged Quote lew247

I spoke too soon

What I want to do is this


Open "COM1:19200" As #2                                'Open port for HC-12 TX/RX  
'OPEN "COM3:9200" AS #3 
If LOC(#2) <> 0 then SERIALFLUSH                          'If COM1 serial port buffer is NOT empty.... 
if LOC(#3) <> 0 then SERIALFLUSH1
DO
clock routine and other bits go here
LOOP
End  
Sub SERIALFLUSH 
Local D$ 'Define D$ only for use by this subroutine(ignores any D$ in main code) 
D$ = Input$(40, #2)  'Suck everything in buffer out 
Print D$;
End Sub 

Sub SERIALFLUSH1 
Local F$ 'Define D$ only for use by this subroutine(ignores any D$ in main code) 
D$ = Input$(80, #3)  'Suck everything in buffer out 
Print F$;
End Sub 


The only way I've got part of it working is this
Quote:

Open "COM1:19200" As #2 'Open port for HC-12 TX/RX
'OPEN "COM3:9200" AS #3
If LOC(#2)<>0 then LOOP 'If COM1 serial port buffer is NOT empty....
DO
IF LOC(#2)>1 THEN SERIALFLUSH
SERIALFLUSH
pause 500
LOOP
End

Sub SERIALFLUSH
Local D$ 'Define D$ only for use by this subroutine(ignores any D$ in main code)
D$ = Input$(40, #2) 'Suck everything in buffer out
Print D$;
End Sub
Back to Top View lew247's Profile Search for other posts by lew247
 
Zonker
Guru
Guru
Avatar

Joined: 18 August 2012
Location: United States
Online Status: Offline
Posts: 665
Posted: 17 August 2016 at 12:47am | IP Logged Quote Zonker

Lew..

Sub SERIALFLUSH1 
Local F$ 'Define D$ only for use by this subroutine(ignores any D$ in main code) 
D$ = Input$(80, #3)  'Suck everything in buffer out 
Print F$;
End Sub


This one has the data going back into D$ (not F$)

Prints nothing...

This is easy to do, and I have done it many times...

Easy fix...

Also, you open #3 at 9200 baud...? not 9600...

I notice also, you hit the action subs if ANY amount of data is collected...

Usually, you would wait for a small number of characters to build up first. If you know how many are expected, then set the LOC on a few less...
If you know what the termination character is (like cr/lf) then you can also check for this... Data is usually collected from the RX buffer one character at a time, and then "added" to the D$ or F$ holding variable... While doing this with a simple DO loop, you can also be checking for the termination character...

Edited by Zonker on 17 August 2016 at 12:59am
Back to Top View Zonker's Profile Search for other posts by Zonker
 
Grogster
Guru
Guru
Avatar

Joined: 31 December 2012
Location: New Zealand
Online Status: Offline
Posts: 5170
Posted: 17 August 2016 at 1:07am | IP Logged Quote Grogster

Yes, as Zonker says....

Note my examples are just examples, and I have not tried it with an HC-12 module.

However, it should work as commented.

In your subs, you are sucking 40 bytes out of the buffer in SERIALFLUSH and 80 bytes in SERIALFLUSH1.

Please note that if there is NOT 40 or 80 bytes in there to suck out, you will get that "Error: Number out of bounds." error again, as you would be trying to read what is not there.

Writing code is fun, eh?!

I also agree with Zonker on the CR or LF idea.
This is how I detect all my serial coms, but that is in MY case, which is specific to my serial devices and needs.

__________________
Smoke makes things work. When the smoke gets out, it stops!
Back to Top View Grogster's Profile Search for other posts by Grogster Visit Grogster's Homepage
 
lew247
Guru
Guru
Avatar

Joined: 23 December 2015
Location: United Kingdom
Online Status: Offline
Posts: 790
Posted: 28 August 2016 at 5:08pm | IP Logged Quote lew247

Now I#'m back in the country again I can start working on this again

I'm "slightly" (very) confused over input$ and character length

If I use D$ = Input$(80, #3) 'Suck everything in buffer out
That assumes there are 80 characters coming in via the com port

However does characters mean each letter and space, or each comma seperated item?

for instance

how many characters are there in this string send to the serial port?
Quote:
{"city":{"id":2651357,"name":"Denton","coord":{"lon":-2.11822,"lat":53.456779},"country":"GB","population":0},"cod":"200 ","message":0.0092,"cnt":7,"list":[{"dt":1472385600,"temp":{"day":20.82,"min":15.1,"max":20.82,"night":15.1,"eve":18.07,"morn":20.82},"pressure":1000.23,"humidity":91,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}] ,"speed":2.01,"deg":7,"clouds":92,"rain":2.54},{"dt":1472472000,"temp":{"day":18.7,"min":11.34,"max":19.24,"night":11.34 ,"eve":17.68,"morn":14.1},"pressure":1009.58,"humidity":77,"weather": [{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}] ,"speed":4.82,"deg":284,"clouds":0},{"dt":1472558400,"temp":{"day":22.25,"min":8.84,"max":23.07,"night":14.47,"eve":21.2 2,"morn":8.84},"pressure":1010.56,"humidity":67,"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}] ,"speed":3.61,"deg":218,"clouds":0},{"dt":1472644800,"temp":{"day":20.04,"min":11.67,"max":20.04,"night":13.12,"eve":18. 19,"morn":11.67},"pressure":1003.72,"humidity":70,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}] ,"speed":4.41,"deg":226,"clouds":0,"rain":1.44},{"dt":1472731200,"temp":{"day":16.42,"min":8.1,"max":18.15,"night":8.1," eve":16.97,"morn":11.79},"pressure":1010.7,"humidity":75,"weather": [{"id":500,"main":"Rain","description":"light rain","icon":"10d"}] ,"speed":2.62,"deg":293,"clouds":8,"rain":0.22},{"dt":1472817600,"temp":{"day":20.17,"min":11.54,"max":20.17,"night":11. 54,"eve":18.96,"morn":16.62},"pressure":1012.65,"humidity":0,"weather": [{"id":500,"main":"Rain","description":"light rain","icon":"10d"}] ,"speed":5.94,"deg":239,"clouds":61,"rain":0.87},{"dt":1472904000,"temp":{"day":23.21,"min":10.51,"max":23.5,"night":14. 51,"eve":23.5,"morn":10.51},"pressure":1013.14,"humidity":0,"weather": [{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"speed":3.89,"deg":140,"clouds":0}]}


Edit:
or if thats too many to count
How many are in this string?
Quote:
{"coord":{"lon":-2.12,"lat":53.46},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}] ,"base":"stations","main":{"temp":17.99,"pressure":1013,"humidity":68,"temp_min":16,"temp_max":20},"visibility":10000,"w ind":{"speed":5.1,"deg":300},"clouds":{"all":40},"dt":1472403905,"sys":{"type":1,"id":5060,"message":0.0305,"country":"G B","sunrise":1472361153,"sunset":1472411099},"id":2651357,"name":"Denton","cod":200}


And am I right in assuming that If I use D$ = Input$(80, #3)
If there are less than 80 characters the string is still received correctly and will work, but if there are more than 80 characters then the string received will be ignored by the program and it would appear on first glance the com port isn't working?


Edited by lew247 on 28 August 2016 at 5:11pm
Back to Top View lew247's Profile Search for other posts by lew247
 
JohnS
Guru
Guru


Joined: 18 November 2011
Location: United Kingdom
Online Status: Offline
Posts: 1562
Posted: 28 August 2016 at 5:54pm | IP Logged Quote JohnS

I think it's:

Fewer than 80 and it will wait until there are 80. Forever, if needs be. So, if you expect 80 but there is a tiny chance one will be corrupted your program may hang forever. (It may be wise to assume this can happen and program differently.)

It counts each char (byte), so any commas are not special just more char(s).

More than 80 and you get the 80 you asked for with the rest still in the buffer.

John

Edited by JohnS on 28 August 2016 at 5:58pm
Back to Top View JohnS's Profile Search for other posts by JohnS
 
Phil23
Guru
Guru


Joined: 27 March 2016
Location: Australia
Online Status: Offline
Posts: 1169
Posted: 29 August 2016 at 3:40am | IP Logged Quote Phil23

Yes,

Every Single ASCII chatacter counts.

{"This is",50 Characters:of text, etc, etc, "etc".


If it was Printed Without a semi colon,

Ie

Data$="{"This is",15 Characters:of text, etc, etc, "etc"."

Print#1, Data$

Vs

Print#1, Data$;


It would be 52 characters long as the string would have a CR & LF at the end.

The two strings you show above are 1928 & 446 characters long according to Excel.


Phil.
Back to Top View Phil23's Profile Search for other posts by Phil23
 
lew247
Guru
Guru
Avatar

Joined: 23 December 2015
Location: United Kingdom
Online Status: Offline
Posts: 790
Posted: 29 August 2016 at 7:23am | IP Logged Quote lew247

Thanks you Phil and JohnS
I understand it fine now

However what do I use IF the serial port is going to have 2 different sets of data coming in, one set is 1928 characters, and the other set is 446 characters?


If I want to pick out words that are comma seperated like this
Quote:
mmrain "," , mmrain2 "," , hightemp "," , hightemp2 "," ,

Would I be correct in assuming that I use arg$
ie arg$(0) would select mmrain and arg$(3) would select hightemp?


Edited by lew247 on 29 August 2016 at 7:27am
Back to Top View lew247's Profile Search for other posts by lew247
 
WhiteWizzard
Guru
Guru


Joined: 05 April 2013
Location: United Kingdom
Online Status: Offline
Posts: 2436
Posted: 29 August 2016 at 8:07am | IP Logged Quote WhiteWizzard

I don't believe ARG$ is a function (or at least its not in the manual!).

You may want to use INSTR to search for the position of the words (if the words are known), and then read the subsequent data with MID$

WW

__________________
For everything MicroMite visit MicroMite.org

Direct Email: WhiteWizzard@MicroMite.org
Back to Top View WhiteWizzard's Profile Search for other posts by WhiteWizzard Visit WhiteWizzard's Homepage
 
TassyJim
Guru
Guru


Joined: 07 August 2011
Location: Australia
Online Status: Offline
Posts: 2277
Posted: 29 August 2016 at 8:13am | IP Logged Quote TassyJim

Before you get too carried away, strings in MMBasic are limited to 255 characters.
The com buffer can be configured to hold more so I would read the port in bite sized chunks and search them for your keywords before getting the next chunk.

Someone here did a program for scraping weather data.

Jim

__________________
It all started with the ZX81....
VK7JH
http://www.c-com.com.au/MMedit.htm
Back to Top View TassyJim's Profile Search for other posts by TassyJim Visit TassyJim's Homepage
 
lew247
Guru
Guru
Avatar

Joined: 23 December 2015
Location: United Kingdom
Online Status: Offline
Posts: 790
Posted: 29 August 2016 at 8:14am | IP Logged Quote lew247

Thanks Phil
I can see a problem so, while I know what "words" will be sent the actual character length can vary, for instance the pressure might at one time be 948.34 another time it might be 1024.87 for example
That's why it would be better to be able to select specific "words" which are a number of comma's apart, that way I wouldn't need to know the exact character length or what the actual "word" will be

an example of what I mean
The serial port will be receiving these "words?"
"Today," & desc & "," & icon & "," & temp & "," & press &"," & speed & "," & dir & "," & humid
BUT the actual words received will vary each time
for example desc one time might be "light clouds" but another time might be "sunny"
dir could be 1 or it could be 360 and so on
Back to Top View lew247's Profile Search for other posts by lew247
 
lew247
Guru
Guru
Avatar

Joined: 23 December 2015
Location: United Kingdom
Online Status: Offline
Posts: 790
Posted: 29 August 2016 at 8:27am | IP Logged Quote lew247

Thanks TassyJim
I actualy made an error above
It's the ESP8266 that will be receiving all those characters and parsing them, then it will be transmitting only the data I actually want on it's serial port to the Micromite

But as a result I won't know the exact number of characters coming into the serial port at any one time (but it will be less than 255) and each item of data will have a comma separating them

Edited by lew247 on 29 August 2016 at 8:28am
Back to Top View lew247's Profile Search for other posts by lew247
 


<< Prev Page of 33 Next >>
In the news...
 
Post ReplyPost New Topic
Printable version Printable version
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum

Powered by Web Wiz Forums version 7.8
Copyright ©2001-2004 Web Wiz Guide

This page was generated in 0.1094 seconds.
Privacy Policy     Process times : 0, 0.02, 0, 0.09