[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions_content.php on line 1014: Undefined array key 3
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions_content.php on line 1014: Undefined array key 3
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions_content.php on line 1014: Undefined array key 3
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions_content.php on line 1014: Undefined array key 3
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions_content.php on line 1014: Undefined array key 3
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions_content.php on line 1014: Undefined array key 3
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4130: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3009)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4130: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3009)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4130: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3009)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4130: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3009)
Controlling your World the Easy Way • How-To BASIC Programming Logic Question...
Page 1 of 1

How-To BASIC Programming Logic Question...

Posted: Thu Mar 07, 2013 7:45 pm
by RBSe
Greetings,
I'm not an experienced programmer so 'i'm not sure how to proceed.
I want to track an analog signal and output that value string to the terminal only when it changes. (filtered relatively slow moving signal)
And I want to set another variable string to show the direct the analog signal is moving.
What would that code be for the EzSBC1???
Thanks for any and all input as I learn from code examples.
Robert

Re: How-To BASIC Programming Logic Question...

Posted: Thu Mar 07, 2013 9:30 pm
by admin
The small program below does what you want. The test AdcNew < OldAdc-2 prevents the moving LSB from generating a message.
The MAP function translates the code from the ADC to a voltage reading. An ADC reading 0f 0 is 0V and a reading of 1023 is 3.3V.
The WaitTime loop at the end makes the code run once per second

Please ask if this is not what you wanted to do.

Code: Select all

' Display the Voltage on P8 when it changes
PINMODE 8, ADC
OldAdc = 10
REPEAT
	'Now get updated value
	AdcNew = INADC( 8 )
	IF AdcNew < OldAdc-2 THEN
		PRINT "Lower Value : ", MAP( AdcNew, 0, 1023, 0.0, 3.3 )
	ENDIF
	IF AdcNew > OldAdc+2 THEN
		PRINT "Higher Value : ", MAP( AdcNew, 0, 1023, 0.0, 3.3 )
	ENDIF
	OldAdc = AdcNew

	WaitTime =  Gettick + 10000 'One second from now
	REPEAT
	  ' Do other things
	UNTIL Gettick > WaitTime
	
UNTIL INKEY > 0
END

Re: How-To BASIC Programming Logic Question...

Posted: Fri Mar 08, 2013 5:53 pm
by RBSe
Greetings admin (name?),

Thanks, that actually answered 2 question.
Now that i see how it's done to track i can reverse-engineer for my own use.
The other question you answered was how to use the MAP command, the syntax was not clear.
I know you folks are just getting started but you are assuming too much in your documentation.
One thing i've learned from being an electrician is you can never assume anything, it will kill you.
You need an example with real-world syntax for every command in the control basic language.
I am new to the EzSBC1 and new to programming and you are assuming i know things.
That would make "Controlling your World the Easy Way" easier.
The one question you did not answer was how to tx/rx to the terminal.
Another real-world example on how to do this would be great.
Also i want to break out of the main program loop not on any key but the "~" key only.
There will be data sent back and forth to the terminal (port0) but not the "~" key...
Your assistance is much appreciated.

Robert
.

Re: How-To BASIC Programming Logic Question...

Posted: Mon Mar 11, 2013 2:35 pm
by Daniel
Hi Robert

Several points are well taken. Firstly, I will now answer questions under my name, Daniel. Also, as time passes I will add real world examples for the commands. You will be surprised how hard it is to dream up a small, clear and useful example when you are faced with a blank page.

To break out of the look on a specific character you need to know the code for that character (Note to self: Add character code table to the manual.) Since I did not provide a character code table in the manual you have to 'measure' it. Here is a piece of code to do that:

Code: Select all

' INKEY
'
PRINT "Test INKEY"
PRINT "Type a character, Esc to quit ..."
PRINT ""
REPEAT
    REPEAT
        ' x=INKEY
        ' Returns the character code of the currently entered character on
        ' the terminal UART0, OR -1 IF no character is available.
        A=INKEY
    UNTIL A > -1
    PRINT CHR$(A),TAB(10),STR$(A), TAB(20), "0x",HEX$(A)
UNTIL A = 27
A=INKEY 'catch some leftover characters
A=INKEY 'from special keys such as F10 etc.
END
The loop above breaks out of the loop when the Escape key is pressed, it will be easy to adjust for any other key.

About the Tx/Rx to the terminal, here I need more information. Is the terminal a PC with a terminal emulation program or a standalone terminal or some industrial device that output RS232, RS488 or RS485? Depending on the answer I can provide some guidance and an example.

Regarding assumptions about knowledge; This has got to be the hardest thing of all. Everyone has an expectation of what people know. Writers say they write for their ideal audience or they can't get anything done. In some sense I did the same when I designed the EzSBC1 and its language. I don't want to have a 500 page manual or many reasons but I do want the manual to be complete and concise. Where there is an omission or error I will fix it but I don't think it will ever be useful to a true beginner without a lot of patience and persistence. Over time I will expand the examples to help people get over the leaning hump. I will also publish example circuits when I can get permission for it (if the are not my own) such as interfacing to a terminal.

Last night I encountered a version of assumed knowledge that applies perfectly to our situation. I'm an Engineer by training and occupation, you are an electrician. In some ways our professional worlds overlap but are distinct in other ways. Yesterday afternoon a section of my house wiring lost power. No big deal, just reset the breaker. Well, wrong. The breaker had not tripped. I spent time futzing around the breaker box, removed the breaker, tested it, put it back, cycled power, sticking multimeters into wall plugs with no success. Eventually i gave up because it got dark and I had things to do but still no power on a few plugs and lights.

I googled for 'No power with breaker closed' and found many references to back stabbers or GFI being the likely source. Now GFI's I know as Earth Leakage units so no problem there but what the heck is a back stabber other than a felon with a knife? For you it is probably immediately evident where to look, for me it is a project. I eventually learned what a back stabber is and why they may be the source of my troubles but right now I'm still struggling in my work area because the lights are on the affected circuit.

Daniel

Re: How-To BASIC Programming Logic Question...

Posted: Mon Mar 11, 2013 8:47 pm
by RBSe
Hi Daniel,

I was about to give up but it seems you don't monitor this over the weekend. :shock:

Thanks for that example of code, that is very helpful. Example code is what I need to study and experiment with, it is what i crave most at this time. I'm a slow reader but I am pouring over the Control Basic Manual to absorb all the commands and their function to see what the capabilities are of the EzSBC1 for my project. This is my 3rd attempt to find a controller that will do the job I want it to. But I am an electrician/tech and not a programmer so the logic theory is not there.

Specifically I want the EzSBC1 to send a <string$>, When data becomes available, to a PC where I can write that <string$> to a file on that PC and then close that file. I figure I could get some BASIC code to use in JustBASIC which has serial communication capabilities. Also the BASIC code to send a <string$> of a file on the PC to the EzSBC1 for processing.

You are doing a great job so far, I know it takes time Daniel. Your help is appreciated.

Robert
.

Re: How-To BASIC Programming Logic Question...

Posted: Sat Mar 16, 2013 5:23 pm
by Daniel
Hi Robert

There was a question that I did not answer earlier:
Specifically I want the EzSBC1 to send a <string$>, When data becomes available, to a PC where I can write that <string$> to a file on that PC and then close that file. I figure I could get some BASIC code to use in JustBASIC which has serial communication capabilities. Also the BASIC code to send a <string$> of a file on the PC to the EzSBC1 for processing.
I took the TERMINAL.bas JustBASIC example program and hacked it so it does something like you asked. It creates a file called SerialLOG.TXT and writes all the characters received from the serial port into that file and to the display. The file is closed when the program ends.

I will post the code under the Downloads tab of the EzSBC1 page as SerialLOG.zip. For interest sake I posted the file below.

Happy hacking
Daniel

Code: Select all

    'This program is to demonstrate the how to write the data received on a communications port
    'to a file in JustBASIC
    'It is in no way intended to be useful for anything else. (or good programming)

nomainwin

WindowWidth = 400
WindowHeight = 300
texteditor #window.te, 0, 0, 391, 254     'The handle for our texteditor is #window.te
graphicbox #window.gb, 800, 1, 10, 10
open "kb" for window as #window           'The handle for our window is #window
print #window.gb, "when characterInput [getChar]"   'When the user presses a key go to [getChar]
print #window, "trapclose [quit]"    'When the user closes our terminal window, go to [quit]
print #window.te, "!autoresize";   'Tell the texteditor to resize with the terminal window
print #window, "font courier_new 9";

'Set the size of the communications buffer to 16K
Com = 8192 * 2

'Open the communications port.  It's handle is #comm.
'If you need to change the COM port, this is the line to modify
open "COM18:57600,n,8,1,ds0,cs0,rs" for random as #comm

'Create or Open a file
open "SerialLOG.TXT" for append as #out

goto [loop]

[getChar]

    'Whenever the user presses a key, we go here to process it.

    c$ = Inkey$
    'Send the character to the com port only if it is not a special keycode
    if len(c$) = 1 then print #comm, c$;
    goto [loop]


[loop]

    'This is where our program spends most of its time after a successful CONNECT

    'If we want to capture keystrokes from our graphicbox, then it always needs
    'to have the input focus.  The next line ensures that it always does.
    print #window.gb, "setfocus"

    inStr$ = input$(#comm, lof(#comm))    'Get com port data
    if inStr$ <> "" then gosub [LogStr]
    scan    'Check for input events

    goto [loop]

[LogStr]
    print #window.te, inStr$;    'Display com port data
    print #out, inStr$
    return

[quit]
    close #out
    close #window
    close #comm
    end

Re: How-To BASIC Programming Logic Question...

Posted: Tue Mar 19, 2013 3:00 pm
by RBSe
Hi Daniel,

This is great!
I appreciate your verbose remarks in your program examples, that helps a great deal for I and others just learning.
There is one thing I need this JustBASIC code to do and that has never changed since I've started.
The only option I have is to read the contents of a file and turn that content into a variable for processing by Automate 5 on my PC.
So there has to be a second file in the JustBASIC code filled with the content of the most current output from the EzSBC1 and then close that file at that instant.
Then Automate 5 sees the file change and makes a variable of the whole contents of the file, so there must only be one command string in the file.
Then it needs to close the file and delete it to make ready for the next incoming command from the EzSBC1.

With Automate 5 I was able to automate teraterm to do the job because you can navigate teraterm with all keystrokes.
It works but it's kind of clunky and takes up foreground desktop time with having to focus on that window during processing.
Below is the code that I wrote in Automate 5's mark-up language tags that are self-explanatory.
If you could modify the JustBASIC code to work like the AM5 code, but in the background, all my problems will be solved.
You almost have it perfect Daniel.
Thanks again for your help.

Robert
.

Code: Select all

<!--- This task is triggered by watching the teraterm.log file for change and responds...
This task takes serial data from the EzSBC1 controller and processes it to perform tasks on XP through Automate 5... --->
<AMVARIABLE NAME="log_data_"></AMVARIABLE>
<AMVARIABLE NAME="Log_data_Bytes_">0</AMVARIABLE>
<!--- Read log file and store in variable "log_data_"
Populate variable "Log_data_Bytes_" with the number of transmitted data bytes from the EzSBC1's terminal serial port... --->
<AMFILEREAD FILE="C:\__tasks\teraterm.log" RESULTVARIABLE="log_data_" MAXBYTES="100" BYTESVARIABLE="Log_data_Bytes_">
<!--- If data bytes = 0 then jump over tasks because we are starting the log file in teraterm to begin data processing... --->
<AMIF EXPRESSION="%Log_data_Bytes_% = 0">
     <AMGOTO LABELNAME="Start_Jump_">
</AMIF>
<!--- Process the contents of the log file...
Write the contents of the teraterm.log file to audit log file... --->
<AMFILEWRITE FILE="C:\__tasks\tom-2013.log">%log_data_%</AMFILEWRITE>
<!--- Close log file and delete log file... --->
<AMWINDOWFOCUS WINDOWTITLE="COM6:57600baud - Tera Term VT">
<!--- Send commands to close the teratem.log file and delete it... --->
<AMSENDKEY DELAYBETWEENKEYS="50">{ALT}fw{ENTER}</AMSENDKEY>
<AMFILEDELETE SOURCE="C:\__tasks\teraterm.log">
<AMWAITFORFILE ACTION="not_exist" FILE="C:\__tasks\teraterm.log">
<!--- Send commands to teraterm to start a new log file and wait for the next input from the EzSBC1 controller to trigger this task... --->
<AMSENDKEY DELAYBETWEENKEYS="50">{ALT}fl{DELAY 500}{ENTER}</AMSENDKEY>
<AMWINDOWMINIMIZE WINDOWTITLE="COM6:57600baud - Tera Term VT">
<!--- Now process the input from the teraterm.log file stored in the variable "log_data_"...
Create IF tasks based on specific commands here...::: --->
<AMIFCONTAINS TEXT="%log_data_%" SUBSTRING="c:\__tasks\meep.wav">
     <AMPLAYAUDIO FILE="c:\__tasks\meep.wav">
     <AMGOTO LABELNAME="skip_default_">
</AMIF>
<!--- If no specific command is sent the default is to speak the input from the EzSBC1 controller... --->
<AMSPEAK SPEED="40" ENGINE="sapi5">%log_data_%</AMSPEAK>
<AMLABEL LABELNAME="skip_default_">
<AMLABEL LABELNAME="Start_Jump_">
<AMWINDOWMINIMIZE WINDOWTITLE="COM6:57600baud - Tera Term VT">

Re: How-To BASIC Programming Logic Question...

Posted: Tue Mar 19, 2013 3:40 pm
by Daniel
Hi

Before I try let me see if I got this straight:
You want one line to a file and to close the file once the line is received. This triggers some action on the PC.
The next line on the serial port must go to the file with the same name, close the file to trigger the action on the PC.

Here is what I propose to do; I open the file and wait for the data on the serial port. When I see the End of Line character(s) I close the file.
I immediately open another file and wait for the serial data to arrive. When I find the end of the line, I delete the previous file and save this file with that name.
I then open a new temporary file and wait for the serial data. I keep doing this till the program ends.

Do I have the idea ?

I read through the previous message again. Who deletes the file? Automate 5 or the log program?
Once I know that I will be able to code it up.

Daniel

Re: How-To BASIC Programming Logic Question...

Posted: Tue Mar 19, 2013 5:30 pm
by RBSe
Hi Daniel,

Sorry to confuse the issue.
I think we are saying the same thing.
It sounds like you know what i'm trying to do.
You are the programmer Daniel.
I'm just trying to follow you.
lets go with your plan and see what happens.
I guess AM5 deletes the file after extracting the data.
Then JustBASIC re-writes the same file when new data comes in.
Does that make sense?
Thanks.

Robert
.

Re: How-To BASIC Programming Logic Question...

Posted: Sat Mar 23, 2013 8:51 am
by RBSe
Thanks Daniel,

I tried to modify the excellent example you created, but i was not successful.
It does not perform and drops bytes.
It seems you are very busy so i wont bother you anymore.
i'm just going to live with what i did by automating teraterm.

This is the code that does NOT work if anyone is interested.

Code: Select all

        'This program is to demonstrate the how to write the data received on a communications port
        'to a file in JustBASIC
        'It is in no way intended to be useful for anything else. (or good programming)

    nomainwin

    WindowWidth = 400
    WindowHeight = 300
    texteditor #window.te, 0, 0, 391, 254     'The handle for our texteditor is #window.te
    graphicbox #window.gb, 800, 1, 10, 10
    open "kb" for window as #window           'The handle for our window is #window
    print #window.gb, "when characterInput [getChar]"   'When the user presses a key go to [getChar]
    print #window, "trapclose [quit]"    'When the user closes our terminal window, go to [quit]
    print #window.te, "!autoresize";   'Tell the texteditor to resize with the terminal window
    print #window, "font courier_new 9";

    'Set the size of the communications buffer to 16K
    Com = 8192 * 2

    'Open the communications port.  It's handle is #comm.
    'If you need to change the COM port, this is the line to modify
    open "COM6:57600,n,8,1,ds0,cs0,rs" for random as #comm

    'Create or Open a file
    'open "c:\__tasks\SerialLOG.txt" for output as #out '<=== I rem'd out this line, RBSe

    goto [loop]

    [getChar]

        'Whenever the user presses a key, we go here to process it.

        c$ = Inkey$
        'Send the character to the com port only if it is not a special keycode
        if len(c$) = 1 then print #comm, c$;
        goto [loop]


    [loop]

        'This is where our program spends most of its time after a successful CONNECT

        'If we want to capture keystrokes from our graphicbox, then it always needs
        'to have the input focus.  The next line ensures that it always does.
        print #window.gb, "setfocus"

        inStr$ = input$(#comm, lof(#comm))    'Get com port data
        if inStr$ <> "" then gosub [LogStr]
        scan    'Check for input events

        goto [loop]

    [LogStr]
        print #window.te, inStr$;    'Display com port data
        open "c:\__tasks\SerialLOG.txt" for output as #out '<=== I added this line, RBSe
        print #out, inStr$
        close #out '<=== I added this line, RBSe
        return

    [quit]
        close #out
        close #window
        close #comm
        end
Robert
RBSe
.