Displaying a variable current value?
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
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.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?
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.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
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.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.
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