Shop OBEX P1 Docs P2 Docs Learn Events
FlexBasic open SendRecvDevice() problem — Parallax Forums

FlexBasic open SendRecvDevice() problem

I am having a bit of trouble implementing the use of input #, and print #, in the link_rpi sub COG. The error is showing that there is something wrong with "open SendRecvDevive(@rpi.tx, @rpi.rx, @rpi.stop) as #3", but it looks like I got the right amount of parameters. Must be some thing that I am missing on the usage part.

Ray

' cr2rpi.bas
'
' Aug 07, 2020
'
'
dim telnet as class using "spin/efds.spin"
dim cr2 as class using "spin/efds.spin"
dim rpi as class using "spin/efds.spin"

telnet.start(31, 30, 0, 115200)
cr2.start(0, 1, 0, 115200)
rpi.start(2, 3, 0, 115200)

open SendRecvDevice(@telnet.tx, @telnet.rx, @telnet.stop) as #2
open SendRecvDevive(@rpi.tx, @rpi.rx, @rpi.stop) as #3

dim inBuff, rpiBuff as string
dim cr_volts, highbyte, lowbyte, cr_vval as ushort


dim rpi_stack(32)

var x_rpi = cpu(link_rpi(),@rpi_stack(32))

cr2.tx(128)   'Passive mode
pausems(300)
cr2.tx(131)   'Safe mode

print #2, "Type 'help' for System Menu!"


do
	print #2, "> ";
	input #2, inBuff
	if inBuff = "help" then
		print #2, "Will be help menu."
	else if inBuff = "crvolts" then
		crVolts()
	else if inBuff = "crpassive" then
		cr_Passive()
	else if inBuff = "crsafe" then
		cr_Safe()
	else if inBuff = "crreset" then
		cr_Reset()
	else if inBuff = "crquit" then
		cr_Quit()			
	else
		print #2, "Invalid Command!"
	end if

loop

sub crVolts
		cr2.tx(142)
		cr2.tx(22)
		pausems(150)
		highbyte = cr2.rx()
		lowbyte = cr2.rx()
		cr_vval = (highbyte << 8) + lowbyte
		cr_volts = (cr_vval / 1000)
		print #2,	cr_volts
end sub

sub cr_Passive
		cr2.tx(128)
end sub

sub cr_Safe
		cr2.tx(128)
		cr2.tx(131)
end sub

sub cr_reset
	cr2.tx(128)
	cr2.tx(7)
end sub

sub cr_Quit
		cr2.tx(173)
		pausems(1000)	
end sub


sub link_rpi
	
	do
		'rpiBuff = rpi.Str()
		input #3, rpiBuff
		if rpiBuff = "help" then
			print #3, "Will be..."
		else
			print #3, "Invalid Command!"
		end if
		pausems(500)	
	loop
	
end sub

"E:/flexgui/bin/fastspin" -D_BAUD=115200 -l -O1 -I "E:/flexgui/include" "E:/programs/flexc/basic/cr2rpi/cr2rpi.bas"
Propeller Spin/PASM Compiler 'FastSpin' (c) 2011-2020 Total Spectrum Software Inc.
Version 4.3.1 Compiled on: Aug 15 2020
cr2rpi.bas
|-efds.spin
|-|-FullDuplexSerial.spin
|-efds.spin
|-efds.spin
E:/programs/flexc/basic/cr2rpi/cr2rpi.bas:15: error: Bad number of parameters in call to _basic_open: expected 4 found 2
child process exited abnormally
Finished at Sun Aug 16 17:10:07 2020

Comments

  • Rsadeika wrote: »
    I am having a bit of trouble implementing the use of input #, and print #, in the link_rpi sub COG. The error is showing that there is something wrong with "open SendRecvDevive(@rpi.tx, @rpi.rx, @rpi.stop) as #3"
    Check the spelling of your function call: I think you wanted "SendRecvDevice", not "SendRecvDevive".
  • Thanks Eric, I could of swore that I had it typed in correctly. Now it compiles without errors.

    A general question, how many COGs is the program using. My efds.spin uses the fds.spin program. I start up efds.spin three times, does the program use three COGs or is it using six COGs? Also, are the SendRecvDevice() subs using any COGs. Again, I note, the program size is 13948 bytes, close to half the available memory is used up. Not sure how much code refinement could be done to reduce the program size.

    I am not sure that the code that I have is starting rpi.start(2, 3, 0, 115200), which is used in the "link_rpi" sub. I did a test by inserting a "print #3, "xxxxx", in the main loop, and nothing is being received on the RPi side. Is there something special about how and where the start() functions should be used. I know the wiring is correct because when I use a SimpleIDE program, it seems to work, as expected.

    Ray
  • Sorry, Ray, but I don't know anything about the internals of efds.spin. FlexBASIC itself doesn't start any cogs, so if you started 3 copies of efds *and* if each copy of efds uses 1 cog then you're using 4 cogs total (the original cog plus the 3 that you started). But that's assuming efds.spin uses only 1 cog, and I don't know if that's true. You could always get a map of what cogs are in use by using cpuchk() like:
    for i = 0 to 7
      if cpuchk(i) then
        print "cog "; i; ": in use"
      else
        print "cog "; i; ": free"
      endif
    next i
    
  • edfs.spin is my name for extendedFullDuplexSerial.spin, is available in OBEX, which starts up or uses FullDuplexSerial.spn which is located in the FlexGUI include folder. When you just start up FullDuplexSerial.spin, how many COGs are being used, in FlexBasic, and is it different for FlexC, in terms of COGs?

    I think that in SimpleIDE, when you use Serial, which basically uses FullDuplexSerial.spin, I think you are using two COGs for that functionality, correct. I just wanted to get a better understanding of what .spin programs that are being linked to FlexBasic and FlexC, and COG usage.

    I will try to implement that snippet for COGs in use and see if it gives me an indication of how many COGs are in use.

    Ray

    P.S. I am starting to lament all this programming stuff.

  • I just tried the little code snippet, in my program:
    >
    cog 0: in use
    cog 1: in use
    cog 2: in use
    cog 3: in use
    cog 4: in use
    cog 5: free
    cog 6: free
    cog 7: free
    >
    If their was a little more info, other than "in use", this snippet would be really, really handy.

    I will "assume" that three of those COGs are the serial("edfs.spin"), and the other one is the "link_rpi" sub. In that case, why is it that when I use "print #3, "xxxxx", in the "link_rpi" COG, I am not getting anything on the RPi side. On the RPi side I used a regular terminal program, and nothing is appearing.

    I guess the next question is, does the "SendRecvDevice()" function work the same way in its own COG, as it does in the main program loop? Does the "SendRecvDevice()" call have to be put in the new COG?

    Also, the code snippet, will that work in FlexC?

    Ray
  • SendRecvDevice() doesn't start a new cog or do anything else. In fact all it does is gather the pointers you give it into a structure and return that; that structure is in turn passed to "open" to tell it how to do I/O.

    I'm guessing your problem is that you're doing the rpi.start() on COG 0, but then trying to use it in the link_rpi COG. As I said, I don't know the details of how efds.spin works, but I assume it sets up some pins for I/O. Normally that has to be done on the COG that's going to use them.

    The code snippet won't work in FlexC because it's written in BASIC; you'd either have to put it into an object and link to it from C that way, or else translate it to C. The C equivalent of "cpuchk()" is "_cogchk()".
  • In the program below, I moved the:
    rpi.start(2, 3, 0, 115200)
    open SendRecvDevice(@rpi.tx, @rpi.rx, @rpi.stop) as #3
    into the "link_rpi" sub COG, after running the COGs snippet, it is now showing that 7 COGs are in use now. I would have thought that only five COGs would be in use. I commented out the "rpi.start(2, 3, 0, 115200)" at the top of the program. Not sure why their was a gain in COGs "in use".

    Ray
    ' cr2rpi.bas
    '
    ' Aug 07, 2020
    '
    '
    dim telnet as class using "spin/efds.spin"
    dim cr2 as class using "spin/efds.spin"
    dim rpi as class using "spin/efds.spin"
    
    telnet.start(31, 30, 0, 115200)
    cr2.start(0, 1, 0, 115200)
    'rpi.start(2, 3, 0, 115200)
    
    open SendRecvDevice(@telnet.tx, @telnet.rx, @telnet.stop) as #2
    'open SendRecvDevice(@rpi.tx, @rpi.rx, @rpi.stop) as #3
    
    dim inBuff, rpiBuff as string
    dim cr_volts, highbyte, lowbyte, cr_vval as ushort
    
    
    dim rpi_stack(32)
    
    var x_rpi = cpu(link_rpi(),@rpi_stack(32))
    
    cr2.tx(128)   'Passive mode
    pausems(300)
    cr2.tx(131)   'Safe mode
    
    print #2, "Type 'help' for System Menu!"
    print #3, "Where am I!"
    
    do
    	print #2, "> ";
    	input #2, inBuff
    	if inBuff = "help" then
    		print #2, "Will be help menu."
    		print #3, "Is it,"
    	else if inBuff = "crvolts" then
    		crVolts()
    	else if inBuff = "crpassive" then
    		cr_Passive()
    	else if inBuff = "crsafe" then
    		cr_Safe()
    	else if inBuff = "crreset" then
    		cr_Reset()
    	else if inBuff = "crquit" then
    		cr_Quit()			
    	else if inBuff = "cogs" then
    		check_cogs()
    	else
    		print #2, "Invalid Command!"
    	end if
    
    loop
    
    sub check_cogs
    for i = 0 to 7
      if cpuchk(i) then
        print #2, "cog "; i; ": in use"
      else
        print #2, "cog "; i; ": free"
      endif
    next i
    end sub
    
    sub crVolts
    		cr2.tx(142)
    		cr2.tx(22)
    		pausems(150)
    		highbyte = cr2.rx()
    		lowbyte = cr2.rx()
    		cr_vval = (highbyte << 8) + lowbyte
    		cr_volts = (cr_vval / 1000)
    		print #2,	cr_volts
    end sub
    
    sub cr_Passive
    		cr2.tx(128)
    end sub
    
    sub cr_Safe
    		cr2.tx(128)
    		cr2.tx(131)
    end sub
    
    sub cr_reset
    	cr2.tx(128)
    	cr2.tx(7)
    end sub
    
    sub cr_Quit
    		cr2.tx(173)
    		pausems(1000)	
    end sub
    
    
    sub link_rpi
    	rpi.start(2, 3, 0, 115200)
    	open SendRecvDevice(@rpi.tx, @rpi.rx, @rpi.stop) as #3
    	do
    		'rpiBuff = rpi.Str()
    		'input #3, rpiBuff
    		'if rpiBuff = "help" then
    		'	print #3, "Will be...\n"
    		'else
    		'	print #3, "Invalid Command!\n"
    		'end if
    		print #3, "Is it."
    		pausems(2000)	
    	loop
    	
    end sub
    

    Running the COGs snippet
    >
    cog 0: in use
    cog 1: in use
    cog 2: in use
    cog 3: in use
    cog 4: in use
    cog 5: in use
    cog 6: in use
    cog 7: free
    >
  • I tried to unclutter the test program a bit, and use .spin files that everybody knows about.

    The program below compiles, but I am getting some weird feedback. I count 4 COGs that should be started, but the check_cogs() sub is showing 5 COGs in use. Maybe I am missing what the fifth COG is? The link_rpi COG is still not pushing any text out.

    Am I missing some big coding idea here?

    Ray

    ' rpi_test.bas
    '
    ' Aug 18, 2020
    '
    
    dim telnet as class using "spin/FullDuplexSerial.spin"  ' COG
    dim rpi as class using "spin/FullDuplexSerial.spin"     ' COG
    
    telnet.start(31, 30, 0, 115200)
    rpi.start(2, 3, 0, 115200)
    
    open SendRecvDevice(@telnet.tx, @telnet.rx, @telnet.stop) as #2
    open SendRecvDevice(@rpi.tx, @rpi.rx, @rpi.stop) as #3
    
    dim rpi_stack(32)
    dim inBuff as string
    
    var x_rpi = cpu(link_rpi(),@rpi_stack(32))  ' COG
    
    ' Main program loop  ' COG
    do
    	print #2, "> ";
    	input #2, inBuff
    	if inBuff = "help" then
    		print #2, "Will be help menu."
    	else if inBuff = "cogs" then
    		check_cogs()	
    	else
    		print #2, "Invalid Command!"
    	end if	
    	'pausems(150)
    loop
    
    ''''''''''''''''''''''''''''''''
    ''''''''''''''''''''''''''''''''
    
    ' COG rpi
    sub link_rpi
    
    	do
    		print #3, "Is it here."
    		pausems(2000)	
    	loop
    	
    end sub
    
    sub check_cogs
    for i = 0 to 7
      if cpuchk(i) then
        print #2, "cog "; i; ": in use"
      else
        print #2, "cog "; i; ": free"
      endif
    next i
    end sub
    
    >>
    Will be help menu.
    >
    cog 0: in use
    cog 1: in use
    cog 2: in use
    cog 3: in use
    cog 4: in use
    cog 5: free
    cog 6: free
    cog 7: free
    >
  • I agree that there should be just 4 COGs in use: COG 0 (the default one), one each for the serial programs, and one for the CPU() function.

    I do see one potential big problem: don't use @rpi_stack(32) for the stack in cpu(), use @rpi_stack(0). That could make a big difference, since it means a stack is being corrupted and almost anything could happen.

  • I fixed the stack call out, now cogs sub is showing 4 COGs in use.

    In the link_rpi sub I used "rpi.tx("a")" to verify that something is working. Yes, it is now showing a "9" instead of an "a". I get a compile error when I tried to use "rpi.tx('a')", for some reason.

    I am not sure, but I think there might be a problem when you try to use SendRecvDevice(), the way I was trying to use it.

    Ray
    ' rpi_test.bas
    '
    ' Aug 18, 2020
    '
    
    dim telnet as class using "spin/FullDuplexSerial.spin"  ' COG
    dim rpi as class using "spin/FullDuplexSerial.spin"     ' COG
    
    telnet.start(31, 30, 0, 115200)
    rpi.start(2, 3, 0, 115200)
    
    open SendRecvDevice(@telnet.tx, @telnet.rx, @telnet.stop) as #2
    open SendRecvDevice(@rpi.tx, @rpi.rx, @rpi.stop) as #3
    
    dim rpi_stack(32)
    dim inBuff as string
    
    var x_rpi = cpu(link_rpi(),@rpi_stack(0))  ' COG
    
    ' Main program loop  ' COG
    do
    	print #2, "> ";
    	input #2, inBuff
    	if inBuff = "help" then
    		print #2, "Will be help menu."
    	else if inBuff = "cogs" then
    		check_cogs()	
    	else
    		print #2, "Invalid Command!"
    	end if	
    	'pausems(150)
    loop
    
    ''''''''''''''''''''''''''''''''
    ''''''''''''''''''''''''''''''''
    
    ' COG rpi
    sub link_rpi
    
    	do
    		'print #3, "Is it here."
    		rpi.tx("a")  ' Terminal side showing a 9 instead of a.
    		pausems(2000)	
    	loop
    	
    end sub
    
    sub check_cogs
    for i = 0 to 7
      if cpuchk(i) then
        print #2, "cog "; i; ": in use"
      else
        print #2, "cog "; i; ": free"
      endif
    next i
    end sub
    
  • RsadeikaRsadeika Posts: 3,837
    edited 2020-08-20 13:27
    Is there something wrong with the latest FlexGUI?

    I did a compile&run on P1, and the terminal screen is repeatedly printing !!! out of memory !!!, this is a first.

    I am also trying to find out if there is a problem with SendRecvDevice(), earlier testing was showing some missteps. Trying to narrow the problem down.

    I am giving up on the WiFi SIP module, to many inconsistencies in the way it is responding. I just had a big problem with FlexGUI scanning an trying to find the WiFi. I always double check by using SimpleIDE to see if it makes the WiFi connection.

    Ray

    ' cr2_net.bas
    '
    ' Aug 19, 2020
    ' 
    dim rpi as class using "spin/FullDuplexSerial.spin"
    
    rpi.start(2, 3, 0, 115200)
    
    open SendRecvDevice(@rpi.tx, @rpi.rx, @rpi.stop) as #2
    
    dim inBuff as string
    
    ''''''''''''''''''''''''''''''
    ''''' main loop '''''
    do
    	input #2, inBuff
    	if inBuff = "help" then
    		print #2, "This cr2"
    	else
    		print #2, "invalid Command!"
    	end if
    	pausems(150)
    loop
    
    Opening file 'E:/programs/flexc/basic/cr2_net/cr2_net.binary'
    Downloading file to port \\.\COM4
    11340 bytes sent
    Verifying RAM
    Download successful!
    [ Entering terminal mode. Type ESC or Control-C to exit. ]
    !!! out of memory !!! !!! out of memory !!! !!! out of memory !!! !!! out of memory !!! !!! out of memory !!! !!! out of memory !!! !!! out of memory !!! !!! out of memory !!! !!! out of memory !!! !!! out of memory !!! !!! out of memory !!! !!! out of memory !!!
  • JRoarkJRoark Posts: 1,215
    edited 2020-08-20 13:38
    You are running out of string space, I think.

    Add “CONST HEAPSIZE=8192” to the top of your code and see if that fixes it.

    EDIT: if this is compiling on the P1, you will need to change HEAPSIZE=1024 or maybe 2048 seeing how precious memory is.

    ADDED: Now that I'm on a real keyboard I can flesh this out a bit better for you. FlexBASIC starts with a default setting for the heap that is very small. The default is 256 bytes for P1 and 4096 bytes for P2. This works well for the P2, but the P1 value is really too small. Even a couple of PRINT statements can swamp that default P1 setting, so start edging the HEAPSIZE value higher until this "out of memory" behavior quits. You can also use _gc_collect() to force the garbage collection to run, but frankly Eric seems to have things so well tuned under the hood that this trick no longer works (ie, FlexBASIC does it automatically, and by the time the programmer tries to tell it what to do, it's already done it and moved along). That being said, an extra _gc_collect() or two is harmless except for the time taken for the call.
Sign In or Register to comment.