usb driver problem

Thomas McGrath III mcgrath3 at mac.com
Fri Jan 28 00:46:44 EST 2011


Well, Claudi and I have been very busy sharing code snippets and stories and what not and I have also come to most of the same conclusions as he has. The problem is definitely in the "until EOF" "until empty" etc. and in trying to access the serial buffer. My solution is a little different and it seems we are both getting some real data back. The main thing that seems to be a work around is to simulate a software Flow Control by having the Arduino Uno wait to read a char and then send it's data back. Then LC can read with out hanging up on the buffer.

I have a photo resistor hooked up to analog input pin 2 and code to read the analog data and send it out to LC only when an "A" is received. I put the code into different fields in LC and then a do command to run (so no mouseUps etc. here):

To get the port I use Marks method to get the /dev/cu.usbmodem1a21 on my machine and put it into thePort also with this method of Flow Control there is a slight time delay on starting the data send to get back results but the board doesn't reset as before so this is good and it is a slight delay just to do the handshake.  

To open the port:
put "BAUD=" & "9600" & " PARITY="& "N" & " DATA=" & "8" & " STOP=" & "1"  into tSerial -- Typical setup
set the serialControlString to tSerial
put the result into field "Results" -- Empty is good
open driver thePort for text update -- binary will hard crash the cu
put the result after field "Results" -- Empty is good

For a simple one time read of three values and a return i.e. 489\r :
write "A" & CR to driver thePort -- Flow Control
wait 25
read from driver thePort for 4 char -- Empty buffer and collect data
put it after field "Data"

For a continuous read:
-- Wake up the Arduino
 write "A" & CR to driver thePort -- Flow Control
 wait 25 -- Additional time may remove the extra line of data shown below
 read from driver thePort for 4 chars -- Just clear the buffer - no need for data at this time

 repeat until it is empty -- no hang continuous loop
        if the mouse is down then exit repeat -- hold mouse to interrupt the Flow Control
        write "A" & cr to driver thePort -- Flow Control
        wait 1
        read from driver thePort for 4 chars -- This time we do want the data and process it as it comes in - very smooth
        put it into theData
        if the last char of theData is LF then delete the last char of theData -- Clean up so it is all on one line to see better
        if the last char of theData is CR then delete the last char of theData -- Clean up so it is all on one line to see better
        put  theData & comma & " "  after field "Data" -- Output so it is all on one line to see better
        set the vscroll of field "Data" to 6000 -- Not needed if all on one line
    end repeat

To close the port, of course:
close driver thePort

----------------- ARDUINO CODE for Flow Control -----------------
//Language: Arduino
 //Reads one analog input and outputs the value
 //Connections: analog sensors on analog input pin 2

int lightSensor = 2;
int lightValue = 0; 

void setup() { 
  Serial.begin(9600);   // configure the serial connection: 
  pinMode(lightSensor, INPUT);   // configure the digital input:

  while (Serial.available() <= 0) {
    delay(500);
    Serial.println("A");
    delay(300);
  }
}

void loop() { 
  // check to see whether there is a byte available to read in the serial buffer:
  if (Serial.available() > 0) {
    // read the serial buffer: we don't care about the value now of the incoming byte,
    // just that one was sent and removed from buffer:
    int inByte = Serial.read();  // Serial.peek() will leave the byte untouched
    
    lightValue = analogRead(lightSensor);  // read the analog sensors: 
    Serial.println(lightValue, DEC);  // send the value to LiveCode
  }
}


The first Serial.println("A"); is sending out a call to serial so that it can be received by LC when an "A" is sent from LC

Output looks like this (The A's are the Flow Control) -- Notice that he first line is left over in the buffer and then the rest is real time results as I place my hand over the photosensor Lower numbers are darker. A Serial.flush() may help in cleaning up the first line but it is easy enough to count how many chars in a line and know that it is not continous :

AA
4 489 
485 487 488 486 490 490 485 490 491 486 490 488 490 488 486 493 486 492 490 486 492 491 487 487 490 492 487 487 488 490 491 486 491 486 490 487 491 485 491 486 492 486 490 487 489 489 489 487 489 489 491 489 489 345 270 192 156 135 87 53 38 35 32 30 29 27 25 23 22 25 56 162 210 243 260 288 365 463 482 482 488 485 485 491 484 491 488 489 490 487 489 490 

mouseDown

Still would like a way to wake from incoming serial input but that is next up in the Arduino Uno saga.


-- Tom McGrath III
http://lazyriver.on-rev.com
3mcgrath at comcast.net

On Jan 27, 2011, at 9:49 PM, Mark Wieder wrote:

> Claudi-
> 
> Congratulations on getting this working. And that's quite a story. I
> see a bright future ahead for LiveCode-Arduino cyborgs.
> 
> -- 
> -Mark Wieder
> mwieder at ahsoftware.net
> 
> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list