Shop OBEX P1 Docs P2 Docs Learn Events
Displaying a variable current value? — Parallax Forums

Displaying a variable current value?

lolobondlolobond Posts: 14
edited 2011-05-03 12:08 in Propeller 1
What command in spin language is used or what can I do if I want to display the value stored in a variable. I am sending the number 1 from a Matlab program to the device. I am using the program below to receive the number 1 and turn a led on. The communication blue light on the propeller blinks once when I run the matlab program so it is receiving something but I don't know why the led that is supposed to turn on is not turning on. The code below works because when I use the parallax serial terminal and write 1 the led does turn on.
CON

_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000

OBJ
ser : "FullDuplexSerialPlus"

PUB main | Number, Value

ser.Start(31, 30, 0, 9600)
waitcnt(clkfreq*2+cnt)

repeat
  ser.Str(String("Valor recibido por MATLAB:"))
  Number := ser.rx
  Value := ser.GetDec
  ser.Dec(Number)
  ser.tx(13)
  if Number=="1"
    dira[4]~~
    outa[4]~~

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2011-04-26 20:20
    CON
    
    _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000
    
    OBJ
    ser : "FullDuplexSerialPlus"
    
    PUB main | Number, Value
    
    ser.Start(31, 30, 0, 9600)
    waitcnt(clkfreq*2+cnt)
    
    repeat
      ser.Str(String("Valor recibido por MATLAB:"))
      Number := ser.rx
      [COLOR="red"]Value := ser.GetDec[/COLOR]
      ser.Dec(Number)
      ser.tx(13)
      if Number=="1"
        dira[4]~~
        outa[4]~~
    
    The line in red is a blocking call, i.e. read a numeric string until CR is received then return its numeric value. So when used with PST you probably typed "1" followed by Enter which fires up the LED. If Matlab only sends the "1" then you're stuck in the GetDec call. For a quick test simply remove/comment this line and observe the different behaviour.
  • lolobondlolobond Posts: 14
    edited 2011-04-26 20:39
    Thank You for the quick respones. I tried it but it still does not work.
    This is my Matlab code
    s=serial('COM3','BaudRate',9600);
    fopen(s);
    ledon=[1];
    fwrite(s,ledon)
    fclose(s);
    

    Is there anyway to check what variable am I receiving from Matlab?
  • kuronekokuroneko Posts: 3,623
    edited 2011-04-26 20:46
    s=serial('COM3','BaudRate',9600);
    fopen(s);
    ledon=[1];
    fwrite(s,ledon)
    fclose(s);
    
    OK, that looks like a numeric one rather than the character "1" (numeric 49). To monitor incoming data you can use something like this:
    repeat
      ser.hex(ser.rx, 2)          ' or ser.dec(ser.rx) if you prefer
      ser.tx(" ")
    
    which displays each incoming character (or rather its numeric value) back to you separated by a space.
  • lolobondlolobond Posts: 14
    edited 2011-04-26 21:49
    1) So I can use the PST to monitor incoming data from Matlab by using the code you sent me?
    2) Yes, is a numeric 1, do I need to change any coding to make it work?
    3) For the last block of code you sent me, should I delete everything else in the repeat block and just copy paste that?

    Sorry for the questions, I am a beginner excited to understand and make this work =)

    Thank You
  • kuronekokuroneko Posts: 3,623
    edited 2011-04-26 22:55
    lolobond wrote: »
    1) So I can use the PST to monitor incoming data from Matlab by using the code you sent me?
    2) Yes, is a numeric 1, do I need to change any coding to make it work?
    3) For the last block of code you sent me, should I delete everything else in the repeat block and just copy paste that?

    Sorry for the delay, had a walk at the beach.

    1. yes
    2. if Number == 1 ' no quotes around the 1
    3. That would only be useful for continuous monitoring. Just remove the GetDec call from your original code for now:
    dira[4]~~
    repeat
      ser.Str(String("Valor recibido por MATLAB:"))
      Number := ser.rx
      [COLOR="red"]ser.Dec(Number)
      ser.tx(13)[/COLOR]
      [COLOR="blue"]if Number == 1[/COLOR]
        !outa[4]            ' toggle LED for each incoming 1
    
    The red bit does the monitoring/echo for you.
  • lolobondlolobond Posts: 14
    edited 2011-04-27 11:01
    I tried the code you posted but it is still not working. Also I am trying to use PST to monitor what the propeller is receiving from Matlab but I have to choose between using COM3 for the PST or COM3 for the Matlab program. Can't use COM3 for both at the same time hence can't monitor whatever I get from Matlab...
  • kuronekokuroneko Posts: 3,623
    edited 2011-04-27 16:25
    lolobond wrote: »
    I tried the code you posted but it is still not working. Also I am trying to use PST to monitor what the propeller is receiving from Matlab but I have to choose between using COM3 for the PST or COM3 for the Matlab program. Can't use COM3 for both at the same time hence can't monitor whatever I get from Matlab...
    OK, apologies. I was under the impression that you had the ability to split the serial connection. Then lets take a step back. Change your loop to something like this:
    dira[4]~~
    repeat
      Number := ser.rx
      !outa[4]            ' toggle LED for each incoming character
    
    ser.rx will block until there is a character available. Then send either more than one or a single character repeatedly from Matlab. This will at least verify that the data is arriving at this end (LED should flash). If this works then we can reintroduce the numeric filter again.

    I'm not familiar with Matlab but it would be worth checking how bidirectional file I/O works. There must be an equivalent fread() call which would allow you to see debug output in Matlab.
  • StefanL38StefanL38 Posts: 2,292
    edited 2011-04-27 22:58
    Hi lolobond,

    please tell us what you want to do in the end.
    if we (the other forum-members) have an overview about your project much better solutions can be found.

    If you are just want to learn the basics about using the propeller and are using mathlab just because you are familiar with it
    I would recommend to switch to EZLog from hannoware instead of using mathlab.

    If you are forced to use mathlab then it would be interesting to know which board you are using.

    On a PPDB you have a second serial interface ready to use which could be used for debugging parallel to the communication with mathlab.
    on other boards you can use any propeller-IO-pins to establish another interface.

    So what do you want to do in the en?

    best regards

    Stefan
  • lolobondlolobond Posts: 14
    edited 2011-05-03 12:08
    Just wanted to thank you and let you know that I solved the problem. The problem was in the Matlab Code, I needed to pause the program for two seconds right after opening the port for serial communication. Now, it works perfectly.
Sign In or Register to comment.