Shop OBEX P1 Docs P2 Docs Learn Events
#28044 laser-range-finder: some serial-spin-code into c-code — Parallax Forums

#28044 laser-range-finder: some serial-spin-code into c-code

nomadnomad Posts: 276
edited 2016-09-23 12:55 in Learn with BlocklyProp
hello,

i have some problems to convert spin-code into c-code for the simpleIDE.

-1) spin-code:
pst.Str(String("Waiting for LRF module..."))
waitcnt(clkfreq << 1 + cnt) ' Delay for 2 seconds to let the LRF module start-up
serial.Tx("U") ' Send character
repeat until serial.RxCheck == ":" ' When the LRF has initialized and is ready to go, it will send a single ':' ve it
pst.Str(String("Ready!", pst#NL, pst#NL)) ' Ready to go!

c-code:
serial* serLrf = serial_open(0, 1, 0, 115200);

print("Waiting for LRF-module...\n");
pause(2000);
writeChar(serLrf, 'U');
while(serial_rxChar(serLrf) == ':');
print("READY\n");

- 2)
spincode:
serial.Tx("R")
repeat until serial.RxCheck == "D"
repeat until serial.RxCheck == "="
repeat until serial.RxCheck == " "
range := serial.RxDec

c-code: From the elev lrf sf02
memset(s, 0, 6);
for (i = 0; i < 6; i++)
{
s = readChar(sf02);
if(s == '\r') break;
}
sscan(s, "%f", &dist);

are the c-code correct or wrong ?

thanks for tips and hints
regards
nomad

Comments

  • In the first C snippet, shouldn't
    while(serial_rxChar(serLrf) == ':');
    

    be changed to
    while(serial_rxChar(serLrf) != ':');
    


    The Spin is repeating UNTIL the received value == a desired value.
    When using a WHILE statement in C you would repeat as long as the received value doesn't equal the desired value.

    tom
  • I'm agree with Tom that in the first C snipped the == should be changed to != (difference between while and until).
    .
    In the second example the Spin code is transmitting "R" and then waiting until "D=" is received. It then reads an integer. The C code is very different; it's not sending anything or waiting for anything, and it's reading a floating point number.

    BTW, are you aware of the spin2cpp tool? It can convert Spin to C or PASM, so it makes comparing Spin code with those other languages much easier. The spincvt GUI for spin2cpp is pretty easy to use, I think.

    Eric
  • hello
    thanks for your answer
    today i make it.
    regards
    nomad
Sign In or Register to comment.