Shop OBEX P1 Docs P2 Docs Learn Events
Windows Interface to send/recieve data with Protoboard - Page 2 — Parallax Forums

Windows Interface to send/recieve data with Protoboard

2»

Comments

  • tae2010tae2010 Posts: 38
    edited 2010-05-16 03:33
    Well I changed your code just a tad, but the flashing stops after about 15 flashes. How can I change it so that it goes on forever?

    One more thing: the original send commands I have to send from the board TO the pc dont seem to work (pst vs serial), is there a command to send data TO the pc, as there is an rxcheck for checking of data FROM the pc?

    The pst object is the thing I thought would get the send (to the pc) part working again. It didnt work.

     pst    : "Parallax Serial Terminal" 
      
    PUB Main
      serial.start(31, 30, 0, 115200)
    
      'dira := 1                   'Set P24 to output
      'outa := 1                   'Set P24 to high
    
      repeat
    
        { Read a byte }
        command := serial.rxcheck
    
        if command == 114                          'hit r to toggle pin 02
          repeat until serial.rxcheck == 101         'hit e to stop
           Toggle(02)
           On(05)
    
        if command == 115                          'hit s to toggle pin 18
          repeat until serial.rxcheck == 101 
           Toggle(18) 
           
    PUB Toggle(pin)
      dira[noparse][[/noparse]pin]~~
      i=0
      repeat
        !outa[noparse][[/noparse]pin]
        waitcnt(clkfreq/2 + cnt)
        i+1
        pst.Str(String(pst#NL, "1 for On, 0 for Off: ", i))
        i-1
    
    



    Thanks.

    Post Edited (tae2010) : 5/16/2010 3:43:03 AM GMT
  • Mike GMike G Posts: 2,702
    edited 2010-05-16 14:56
    tae2010 said...
    Well I changed your code just a tad, but the flashing stops after about 15 flashes. How can I change it so that it goes on forever?
    First off, your code is riddled with syntax errors and does not compile. You'll need to post the working code describing above for help with run-time debugging.
    tae2010 said...
    One more thing: the original send commands I have to send from the board TO the pc dont seem to work (pst vs serial), is there a command to send data TO the pc, as there is an rxcheck for checking of data FROM the pc?
    Yes, there are several methods to transmit data using FullDuplexSerial; tx, str, dec, hex, and bin. This information is openly published in FullDuplexSerial.

    PUB Toggle(pin)
      dira[noparse][[/noparse]pin]~~
      repeat 2
        !outa[noparse][[/noparse]pin]
        serial.hex(outa, 5)
        serial.tx(13)
        waitcnt(3_000_000 + cnt)
    
    
  • TtailspinTtailspin Posts: 1,326
    edited 2010-05-16 21:00
    Hang In there tae2010, What I have been finding is... this stuff is so Simple, it's Hard...

    I TRY NOT to change to much at once, and TEST each change...Well, at least press F9 to see if I am even close, and IF I
    don't get a Syntax Error, then I will move on to the Next line of Code for the Function/Procedure/very small peice of code
    that I am playing with..

    Anyways, What is the Sensor Data from? Or, What kind of Sensor Data are You wanting to use?, I ask because, I could mess
    with whatever Sensor You were planning on working with, Otherwise..

    I am going to start playing around with The PIR sensor and the TSL230R(Light to Frequency Converter) ,
    and see if I can Make something Usefull happen in VB, like...Use the TSL230 to tell me if it gets dark at night...Or..
    Maybe I could Use the PIR to see if somebody is standing next to me... or maybe.. Well never mind, I will think of something.

    I did Manage to get a Standard Servo to do just about everything I ask...
    I can Use a TrackBar to move it, or ListBox to select a Command, or I can just Type a Command into a Textbox.

    I am sure this is trivial to some of the more "Advanced" PropHeads out there, But It's Pretty cool stuff for a Newbie like Myself though..
  • tae2010tae2010 Posts: 38
    edited 2010-05-16 21:13
    Sorry for the mistakes, I'll review the code and upload a working version. But Ive got one important thing that Im stuck on. Some people seem to prefer working with FullDuplexSerial and others with ParallaxSerialTerminal. Ive noticed that the Parallax Terminal has more features than the other. So is it possible to adapt your previous example code to the Parallax Terminal commands? Or would I have to change how your code functions as well?

    @Tailspin

    The sensor Im using is a Pressure sensor that outputs a voltage. So I will be using an ADC circuit to get a digital signal into the board. (Im waiting for the ADC to come in the mail) The signal will come in on one of the pins and Ive got some preliminary code to get my started on that once I get everything together.

    I wanted to ask you if you added strcomp to the Parallax Serial Terminal code, because I cant seem to find it there, and it would come in handy if I had it for the string inputs.

    Post Edited (tae2010) : 5/16/2010 9:30:09 PM GMT
  • Mike GMike G Posts: 2,702
    edited 2010-05-16 22:17
    tae2010 said...
    Some people seem to prefer working with FullDuplexSerial and others with ParallaxSerialTerminal. Ive noticed that the Parallax Terminal has more features than the other. So is it possible to adapt your previous example code to the Parallax Terminal commands? Or would I have to change how your code functions as well?

    You can add the PUB rxNoBlock method to the Parallax Serial Terminal object. That will expose the PRI RxCheck method to the outside world. PRI means private.

    {
      Added by Mike Gebhard
      so we can get to the RxCheck method
    }
    PUB rxNoBlock : bytechr
      bytechr := RxCheck
      
        
    PRI RxCheck : bytechr
    {Check if character received; return immediately.
      Returns: -1 if no byte received, $00..$FF if character received.}
    
      bytechr~~
      if rx_tail <> rx_head
        bytechr := rx_buffer[noparse][[/noparse]rx_tail]
        rx_tail := (rx_tail + 1) & BUFFER_MASK 
    
    



    The code might look something like

    CON
        _clkmode = xtal1 + pll16x
        _xinfreq = 5_000_000
    
    VAR
      byte  command
      byte  temp
        
     
    OBJ
      serial : "Parallax Serial Terminal"
    
      
    PUB Main
      serial.StartRxTx(31, 30, 0, 57600)
    
      repeat
    
        { Read a byte }
        temp := serial.rxNoBlock
        if temp <> 255
          command := temp
    
        if command == 119
           Toggle(16)
    
        if command == 115 
           Toggle(23)
    
        if command == 13 
           
    
        waitcnt(1_000_000 + cnt)
    
    
          
       
    PUB Toggle(pin)
      dira[noparse][[/noparse]pin]~~
      repeat 2
        !outa[noparse][[/noparse]pin]
        serial.hex(outa, 5)
        serial.NewLine
        waitcnt(3_000_000 + cnt)
    
    



    I want to make one comment about .NET languages and please let's not turn this into a evangelical debate, I will not comment beyond this post. Ttailspin and tae2010, consider using C# over VB.NET. Even though .NET languages compile to the similar MSIL code, generally there is a higher demand for C# programmers than VB.NET.

    Post Edited (Mike G) : 5/17/2010 12:37:00 AM GMT
  • TtailspinTtailspin Posts: 1,326
    edited 2010-05-17 00:49
    @tae2010,
    What kind·of Voltage Ranges·are You·expecting from Your "Pressure Sensor"? Or, What kind/Brand of Pressure Sensor?
    Also, which Analog Digital Converter are You waiting for? Just asking so I might try and duplicate the expected data.

    So far as this,
    tae2010 said...
    I wanted to ask you if you added strcomp to the Parallax Serial Terminal code, because I cant seem to find it there, and it would come in handy if I had it for the string inputs.
    STRCOMP is a Keyword ,
    Information for "strcomp", is located in the "Memory" Section of the Propeller Manual...I could'nt find it at first either,
    Cuz it sounds like a "Directive" to Me,(not a complaint)·Just goes to shows what I don't knows..

    Just to be clear, Right now, "Parallax Serial Terminal.spin" and "FullDuplexSerial.spin" are one in the same to Me.
    You need to Pick one, For now.. sort of master it, then move on to the next one. also check out FullDuplexSerialPlus.
    In fact, Look close at the Code for the two, It·seemed to Me, the Calls to them were close to the same thing.
    It stands to reason that one could make two different Pin's/Led's. flash from both Programs at once...

    @MikeG,
    ·· Not making any debate either, but being as the subject has been broached...
    I Believe You are correct about C#, It is more Popular among'st Prospective Employers. and should be pursued
    by those seeking gainfull Employment, However, in My case... My career choices have been made long ago..
    I recall it went something like: "no ma, i dont wanna go to school and be a doctor, I wanna be a carpenter...."
    Oh well, I guess lifting big heavy boards way up high is just what I wanted..sigh.
    I suppose the biggest draw to VB for Me,·was·Probably the Name itself, VISUAL·and BASIC, how could I go wrong?turn.gif
    ·
  • tae2010tae2010 Posts: 38
    edited 2010-05-17 02:20
    Ttailspin said...
    @tae2010,
    What kind of Voltage Ranges are You expecting from Your "Pressure Sensor"? Or, What kind/Brand of Pressure Sensor?

    Also, which Analog Digital Converter are You waiting for? Just asking so I might try and duplicate the expected data.

    Im working with an MSI 0-100PSI Pressure Transducer, that outputs 0.5v to 4.5 v. I will be using a MCP3202 ADC. Ive already found some code to get me started, you can find the links here:

    obex.parallax.com/objects/search/?q=MCP3202

    The last two down are the ones Im gonna start with. Ive already taken a look through the code, and it seems simple. The problem is calibrating the ADC, then integrating the code into my original program. I wonder if I can include the spin code as an object instead.
    Mike G said...

    First off, your code is riddled with syntax errors and does not compile. You'll need to post the working code describing above for help with run-time debugging.

    Im working on redoing the code because Ive found that the Propeller Terminal object has more functions than the Full Duplex Serial object. I'll post once Ive finished it.

    As a side question, how can I declare this variable Temp to 32bits so that it works? I have this statement Im trying to integrate into my code:

    
    Temp := INA[noparse][[/noparse]pin] 
    
    
    

    Post Edited (tae2010) : 5/17/2010 6:20:20 AM GMT
  • Mike GMike G Posts: 2,702
    edited 2010-05-17 14:00
    tae2010 said...
    As a side question, how can I declare this variable Temp to 32bits so that it works? I have this statement Im trying to integrate into my code:

    VAR
      long temp
    
    



    This is in the Propeller Manual starting on page 210; "VAR – Spin Language Reference".

    I suggest that you go through the "Propeller Education Kit Labs: Fundamentals"
    http://www.parallax.com/tabid/442/Default.aspx
  • tae2010tae2010 Posts: 38
    edited 2010-05-17 22:18
    Thanks Mike G for all your help, your a great resource to ask. smilewinkgrin.gif

    So here's my latest code. Ive got a few problems that are bugging me now. First any pin wont turn off even when I type in exit or stop. Second, the function that gives me the status (and sends it to the terminal) of my pin doesnt work. Only a reset of the board seems to stop the loop.

    CON
        _clkmode = xtal1 + pll16x
        _xinfreq = 5_000_000
        Baud =  115200
        relaypwr = 02
        ledpwr = 05
        
        otherpin = 23
    
    VAR
            byte  command
            byte  rxString[noparse][[/noparse]50]  'Holds [noparse][[/noparse]50] incoming serial bytes,
                          'that should hold 49 letters or numbers, and the terminator char.
            Long  Temp '[noparse][[/noparse]32]
            
    OBJ
            
            pst    : "Parallax Serial Terminal"
      
    DAT       ''Line up all the expected Strings
    
            pin02 byte "PIN02", 0
            pin23 byte "PIN23", 0
            stop  byte "stop", 0
            exit  byte "exit", 0
            on    byte "ON", 0
            off   byte "OFF", 0
            WrongData byte "Bad Data", 0
            high byte "High", 0
            low byte "Low", 0
            
    PUB Main
      
       pst.StartRxTx(31,30,0,Baud)
       pst.RxFlush
    
       DisplayClkFreq
       
      repeat
    
        { Read a byte }
        'command := serial.rxcheck
        pst.StrIn(@rxString)   'Recieve the String from PC, and place it starting at (@rxString) for [noparse][[/noparse]50] byte's..
     
        if(StrComp(@rxString, @pin02))
          repeat until strcomp(@rxString, @stop OR @exit)
           Toggle(relaypwr)
           dira[noparse][[/noparse]ledpwr]~~
           outa[noparse][[/noparse]ledpwr]~~
        
        if(StrComp(@rxString, @pin23))
          repeat until strcomp(@rxString, @stop OR @exit)
           Toggle(otherpin) 
    
        BadData
        
        waitcnt(10_000_000 + cnt)
     
    PUB Toggle(pin)
      
      dira[noparse][[/noparse]pin]~~
      repeat 2
        Temp := 0 
        !outa[noparse][[/noparse]pin]
         Temp := INA[noparse][[/noparse]pin]      'pg 119 in the manual, another helpful page is pg 222
         if (Temp == @high)
           pst.str(@on)
         if (Temp == @low)
           pst.str(@off)  
        waitcnt(clkfreq*2 + cnt)
    
    PUB BadData  
                       
      dira[noparse][[/noparse]8..15]~~
     
      pst.str(@WrongData)
     
      repeat 50                  
        !outa[noparse][[/noparse]8..15]
        waitcnt(clkfreq + cnt)
    
    PUB DisplayClkFreq
    
     pst.str(clkfreq)
    
    
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-05-17 22:28
    strcomp(@rxString, @stop OR @exit) should be strcomp(@rxString, @stop) OR strcomp(@rxString, @exit)

    pst.str(clkfreq) should be pst.dec(clkfreq)
  • tae2010tae2010 Posts: 38
    edited 2010-05-17 22:44
    Nope, I just gave those fixes a try, and still nothing. The pin wont turn off as I tell it to, and all the send TO the pc commands arent working.

    Id like to point out that the pin would turn off and on when I used the Full Duplex Serial. Now when using the Parallax Serial Terminal, the receive from the pc works (so rx), but the send to the pc wont work correctly (tx). It works occasionally to send me the message that Im entering in wrong data, but it wont tell me if the pin is on or off as in my code (or sending the clock freq as a string to the terminal).

    Post Edited (tae2010) : 5/18/2010 1:04:17 AM GMT
  • Mike GMike G Posts: 2,702
    edited 2010-05-18 02:11
    Well, you drop into a loop and never leave. The first loop starts with

    pst.StrIn(@rxString)
    


    The prop waits for someone to enter a string that ends with a 13.

    If one of the two conditions are meet, a code block runs. If no conditions are meet then we wait for more data that ends with a 13. If one of the conditions are meet then we enter a loop... forever. There's two problems that I can see.

    1) You're using a method that blocks. When your code hits pst.StrIn(@rxString) it stops until the it sees a 0. These are the code comments.
    {{Receive a string (carriage return terminated) and stores it (zero terminated) starting at stringptr.
    Waits until full string received.
      Parameter:
        stringptr - pointer to memory in which to store received string characters.
                    Memory reserved must be large enough for all string characters plus a zero terminator.}}
    
    



    2) Once you enter a loop, your stuck in the loop. You need to have some way to break out of the loop. Usually, that logic is like "Ok I'm working on this very important piece of code but if but if my wife calls me, for whatever reason, I'd better leave my desk ASAP".

    Let's say you're in this loop...

        if(StrComp(@rxString, @pin02))
          repeat until strcomp(@rxString, @stop) OR strcomp(@rxString, @exit)
           Toggle(relaypwr)
           dira[noparse][[/noparse]ledpwr]~~
           outa[noparse][[/noparse]ledpwr]~~
    
    



    How do you want to get out of the loop? Send a command through the serial port, through a PIN or another device?

    Edit: StrIn waits for a 13 not 0

    Post Edited (Mike G) : 5/19/2010 2:38:52 PM GMT
  • tae2010tae2010 Posts: 38
    edited 2010-05-18 03:07
    Mike G said...

    2) Once you enter a loop, your stuck in the loop. You need to have some way to break out of the loop. Usually, that logic is like "Ok I'm working on this very important piece of code but if but if my wife calls me, for whatever reason, I'd better leave my desk ASAP".

    Let's say you're in this loop...

        if(StrComp(@rxString, @pin02))
          repeat until strcomp(@rxString, @stop) OR strcomp(@rxString, @exit)
           Toggle(relaypwr)
           dira[noparse][[/noparse]ledpwr]~~
           outa[noparse][[/noparse]ledpwr]~~
    
    



    How do you want to get out of the loop? Send a command through the serial port, through a PIN or another device?

    Im gonna need to send a command via the serial port and also within the code. The goal is a manual override (on screen), and a automatic feature that stops the loop (ie some pressure greater than a constant pressure, then exit the loop).

    How is it that it expects a 0 at the end of a command in order to exit? Cant the command 'exit' or 'stop' be enough for it to leave the loop?
  • Mike GMike G Posts: 2,702
    edited 2010-05-18 12:37
    Let me put this another way. Your code is functioning as written. Your brain has a different thing in mind. Let's look at this code snippet.

        pst.StrIn(@rxString)   'Recieve the String from PC, and place it starting at (@rxString) for [noparse][[/noparse]50] byte's..
     
        if(StrComp(@rxString, @pin02))
          repeat until strcomp(@rxString, @stop) OR strcomp(@rxString, @exit)
           Toggle(relaypwr)
           dira[noparse][[/noparse]ledpwr]~~
           outa[noparse][[/noparse]ledpwr]~~
    
    



    When execution gets to pst.StrIn(@rxString), it sits there until it detects a carriage return. When a carriage return is detected a string is stored in memory starting at address @rxString. Let's say that "PIN02" was sent. Now @rxString = "PIN02" and we enter the code block. I'm using @rxString = but I mean the string starting at @rxString and ending with a 0.

        if(StrComp(@rxString, @pin02))
          repeat until strcomp(@rxString, @stop) OR strcomp(@rxString, @exit)
           Toggle(relaypwr)
           dira[noparse][[/noparse]ledpwr]~~
           outa[noparse][[/noparse]ledpwr]~~
    
    



    Next, execution repeats until @rxString = "stop" or @rxString = "exist". Well, @rxString = "PIN02" and it will equal "PIN02" forever. Remember pst.StrIn(@rxString) loaded @rxString in the first place. You might think, OK I'll insert another pst.StrIn(@rxString) in the loop. That will NOT work because the StrIn method blocks (just sits there) until it sees a carriage return. You have logic errors. Go through your code line by line and the find the places where your code is doing X but you think it's doing Y.

    You should thank Dave Hein for pointing out your logic errors. It's takes considerable time to read someone's code and provide feedback.

    Post Edited (Mike G) : 5/18/2010 12:46:06 PM GMT
  • TtailspinTtailspin Posts: 1,326
    edited 2010-05-18 14:08
    It might seem a little thankless at times, when giving time to help newbies on any forum, You wonder if they are ever going to get it.
    LOL, specially when the code is written and commented by newbies like Myself, The Keyboard is a dangerous thing in the hands
    of a Carpenter... AnyWays, I really do appreciate the time spent By Others to offer Helpfull Input,
    Even If Your Name is not used directly, the input given is Appreciated very much, I have read/studied every line of code in this thread.
    Thank You again. [noparse]:)[/noparse]

    tae2010,
    Just Put the (@stop) and (@exit) "commands" into thier own IF(StrComp(@rxString, @????)) loops,
    and loose the repeat until, That should Work more like what You are thinking, (I'm tellin you, this stuff is so easy, it's hard..)
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-05-18 15:29
    I only looked at your code for a minute or two when I made my first post.· That's why I only pointed out a couple of the obvious errors.· I modified your program and added another method called GetCommand.· It is attached below.· It prints out a prompt message and reads a string from the input.· Your program has varous waitcnt delays in it, and it was hard for me to figure out when it was waiting for me to type something.

    I added calls to GetCommand within the PIN02 and PIN23 loops, otherwise you would never exit the loops.· I also added "next" statements after these loops so you go back to the main prompt, and don't alway print "Bad Data".· I reduced the number of loops in the BadData method to 5 instead of 50.· I got tired of waiting 50 seconds for it to return.

    The StrIn method blocks, which means·your program·will wait until you push the enter key before it will do anything else.· The way to make this not block is to either use the RxCheck method or put GetCommand in another cog.· However, I suggest that you don't do that until you are more experience with programming in Spin.

    Dave

    Edit:· I changed the value of Baud to 57600.· You need to change that back to 115200 to run at that baudrate.· Sorry, my PST was set at 57600, so I changed the baudrate in the program to match.

    Post Edited (Dave Hein) : 5/18/2010 3:36:16 PM GMT
  • tae2010tae2010 Posts: 38
    edited 2010-05-18 22:49
    Mike G said...

    You should thank Dave Hein for pointing out your logic errors. It's takes considerable time to read someone's code and provide feedback.

    Was in a bit of a hurry, but normally I do. Thanks for your time and input as well.
    Dave Hein said...
    I only looked at your code for a minute or two when I made my first post. That's why I only pointed out a couple of the obvious errors. I modified your program and added another method called GetCommand. It is attached below. It prints out a prompt message and reads a string from the input. Your program has varous waitcnt delays in it, and it was hard for me to figure out when it was waiting for me to type something.

    I added calls to GetCommand within the PIN02 and PIN23 loops, otherwise you would never exit the loops. I also added "next" statements after these loops so you go back to the main prompt, and don't alway print "Bad Data". I reduced the number of loops in the BadData method to 5 instead of 50. I got tired of waiting 50 seconds for it to return.

    Thanks Dave for your input. This will really help a lot. I know it does take a long time to review code and make edits, so thanks a lot for this. I'll go through the code and try to learn what changes happened.

    Post Edited (tae2010) : 5/19/2010 12:34:27 AM GMT
  • tae2010tae2010 Posts: 38
    edited 2010-05-28 05:36
    Well I made some headway on the program. Thanks Dave for your code edit. That worked really nicely. Now Ive moved into the VB.net and Im working on the interface program. Ive decided to keep the string system that Ive been working off of, instead of switching back to bytes ( the original reason for strings was to troubleshoot through the terminal).

    Ive got a question though. How can I coordinate the sending of data from the program to the board, and then the board receiving that data? Is there some command I can send to the board via the serial to enable the function that waits for incoming data? Then the data can activate whatever its supposed to activate... ie send the command from the program to the board: "Enable Rx", then send it "Turn on relay 1", then it should be able to do that. Would that just be an if statement? Or how would that work?

    Thanks again.
  • Mike GMike G Posts: 2,702
    edited 2010-05-28 13:00
    tae2010 said...
    Ive got a question though. How can I coordinate the sending of data from the program to the board, and then the board receiving that data? Is there some command I can send to the board via the serial to enable the function that waits for incoming data?
    It's up to you. For example, send "relay1On" out the PC serial port. On the Prop look for the string "relay1On". If "relay1On" is received then run some code.
    tae2010 said...
    ie send the command from the program to the board: "Enable Rx", then send it "Turn on relay 1", then it should be able to do that. Would that just be an if statement?
    You can't enable the serial port by sending "Enable Rx" on the serial port... Chicken egg thing. Again the strings you use to communicate between the PC and propeller are up to you.
    tae2010 said...
    Or how would that work?
    It works just like all the sample code posted in this thread.
  • tae2010tae2010 Posts: 38
    edited 2010-05-28 22:58
    Mike G said...
    tae2010 said...
    ie send the command from the program to the board: "Enable Rx", then send it "Turn on relay 1", then it should be able to do that. Would that just be an if statement?
    You can't enable the serial port by sending "Enable Rx" on the serial port... Chicken egg thing. Again the strings you use to communicate between the PC and propeller are up to you.

    So basically the serial is always enabled, and the board is always looking for a command that you send it. Got it. smilewinkgrin.gif

    Also, can I include a program that Ive written as an object? Or would it be more efficient to just copy and paste that code into the current program Im working as a SubFunction?
  • Mike GMike G Posts: 2,702
    edited 2010-05-29 00:04
    tae2010 said...
    Also, can I include a program that Ive written as an object? Or would it be more efficient to just copy and paste that code into the current program Im working as a SubFunction?

    I'm not sure, it depends on your application.
  • tae2010tae2010 Posts: 38
    edited 2010-05-29 03:37
    Well, Im planning to have the board take sensor samples on regular intervals, and the code for that is decent sized. So I need to be able to include that code without making my entire program look bulky. Im gonna give it a try and see if it works.

    Thanks.
Sign In or Register to comment.