// FILE: serialPc-M-1_0a.sce - Works!
// DATE: 04/14/20 20:49
//       04/15/20 11:40
// AUTH: P.Oh
// DESC: PC USB RS485 connect to (Master) NXT.  Scilab running on PC sends
//       serial message e.g. " @90, -90" (without quotes) containing desired
//       angles for XL-320 Lego-based 2-DOF planar manipulator.  Master
//       (running PC-M-S-1_0a.nxc) processes this message (and sends to Slave
//       via Bluetooth).
// VERS: 1_0a: based on scilabPcSerialToNxt0_1h.sce
// REFS: Works with Master NXT running PC-M-S-1_0a.nxc and Slave running
//       btS-R-1_0a.nxc

h = openserial(10,"4800,n,8,1"); // initialize PC's serial port
strHeader = " @"; // white space + at character
stringRoger = "ROGER";
stringRogerFound = 1; // not TRUE

for i = 1:4 // four different angle pairs
    if i == 1 then
        position01 = 0; 
        position02 = 90; 
    end
    if i == 2 then
        position01 = 90; 
        position02 = 0; 
    end
    if i == 3 then
        position01 = 0; 
        position02 = -90; 
    end
    if i == 4 then
        position01 = 90; 
        position02 = 90; 
    end
    
    strPosition01 = string(position01);
    strPosition02 = string(position02);
    strI = strcat([strHeader, strPosition01, ", ", strPosition02]);
    disp(strI);
    writeserial(h, strI); // transmit serially to Master NXT

    buf = readserial(h);
    // Check if Master ready to receive next string
    stringRogerFound = strcmp(stringRoger, buf); // 0: means identical strings
    while (stringRogerFound ~=0) // then NXT -> PC string not ROGER, so wait
       buf = readserial(h);
       stringRogerFound = strcmp(stringRoger, buf);
       sleep(200); // min about 50 ms before reading serial port again
    end; // exit reading serial port when ROGER received           
    disp(buf);
    sleep(5000); // just slows down loop so user can see what's happening    
end

disp("All done!");
closeserial(h)

