Go Down

Topic: Stepped Serial Output (Read 556 times) previous topic - next topic

DanMo

Hello and thanks in advance for any help you all provide.

Ok here goes!

I have an Arduino Diecimila with a protoshield connected to Mac OS X via USB.  I am using Ruby to ask the Arduino for some data.  When I ask the data appears to come out missing pieces.
Code: [Select]

D T=20C
D02B T=20C
D540002B T=20C
DCAE540002B T=20C
D108CAE540002B T=20C
D=108CAE540002B T=20C


Eventually I get all the data as I expect.  The strange thing is that the cycle starts over once I get all of it.  If I put the Arduino in push mode (where it just sends the data) I get all the data as expected.

Here are the two parts to my program.

Arduino
Code: [Select]

//#include <SoftwareSerial/SoftwareSerial.h>
// #include <AFSoftSerial/AFSoftSerial.h>
#include <OneWire/OneWire.h>

/* DS1920 Temperature chip i/o

*/

OneWire  ds(10);  // on pin 10

#define rxPin 2
#define txPin 3
int incomingByte;      // for incoming serial data
int LastPinVal = LOW;
char outSerial[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
 
// Setup the BlueSMiRF on pins 2 & 3
// AFSoftSerial btSerial = AFSoftSerial(rxPin, txPin);
byte pinState = 0;
byte count = 0;


void setup(void) {
 // initialize inputs/outputs
 // start serial port
 //Serial.begin(9600);

 // define pin modes for tx, rx:
 pinMode(rxPin, INPUT);
 pinMode(txPin, OUTPUT);

 Serial.begin(9600);
 Serial.print("\r\nSetup Complete\r\n");

}

void toggle(int pin) {
 digitalWrite(pin, HIGH);
 delay(100);
 digitalWrite(pin, LOW);
}

void read_values() {

 byte i;
 byte present = 0;
 byte data[12];
 byte addr[8];

 if ( !ds.search(addr)) {
     //Serial.print("No more addresses.\n");
     // Serial.print("No more addresses.\n\r");
     delay(100);
     ds.reset_search();
     return;
 } else {
       // If the type is empty (all zeros) then it's not a valid device.
       // Sleep 1 second and return
       if ( addr[0] == 00) {
         delay(100);
         return;
       }
 
       if ( addr[0] != 0x10) {
           Serial.print("Device is not a DS1920 family device.\r\n");
           return;
       } else {
         Serial.print("D=");
         for( i = 0; i < 8; i++) {
           Serial.print(addr[i], HEX);
         }
           Serial.print(" ");
     

         if ( OneWire::crc8( addr, 7) != addr[7]) {
           Serial.print("CRC is not valid!\n");
           return;
         }
       }

       ds.reset();
       ds.select(addr);
       ds.write(0x44,1);         // start conversion, with parasite power on at the end
 
       delay(2000);     // Temperature conversion can take up to 2 seconds
       // we might do a ds.depower() here, but the reset will take care of it.
 
       present = ds.reset();
       ds.select(addr);
       ds.write(0xBE);         // Read Scratchpad
 
       // if (present) {
       //   Serial.print("Present");
       // }
       // Serial.print(present,HEX);
       // Serial.print(" ");
       for ( i = 0; i < 9; i++) {           // we need 9 bytes
         data[i] = ds.read();
         // Serial.print(data[i], HEX);
         // Serial.print(" ");
       }
       Serial.print("T=");
     delay(10);
     //   Serial.print((data[1])?'-':' ');
     // delay(10);
       Serial.print(data[0]/2, DEC);
     delay(10);
       Serial.println("C");
     //  Serial.print(" CRC=");
     //  Serial.print( OneWire::crc8( data, 8), HEX);
       // Serial.println();

 }
}

void loop(void) {
     // read_values();
       // send data only when you receive data:
       if (Serial.available()) {
         // read the incoming byte:
           incomingByte = Serial.read();
         toggle(11);
              switch ((int)incomingByte) {
             case 49:
               //do something when var == 1
               // Serial.print("Reading Values\r\n");
                   read_values();
               break;
               // break is optional
           }
                   
     }
}


Ruby Client
Code: [Select]

require "serialport.so"
require "Date"

#params for serial port
# port_str = "/dev/tty.Arduino-BT"  #may be different for you
port_str = "/dev/tty.usbserial-A4001KMR"  #may be different for you
baud_rate = 9600
data_bits = 8
stop_bits = 1
parity = SerialPort::NONE
sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)
input = ""

# my_file = File.new("/Users/danielmorrigan/temp.data", "a+")

Thread.new {
 
  while true do
    my_file = File.new("/Users/danielmorrigan/temp.data", "a+")
    serial_in = sp.getc
    my_file.printf("%c", serial_in)
    printf("%c", serial_in)
    my_file.close
  end
}
while true do
   sleep(10)
   sp.putc "1"
end


#   my_file = File.new("/Users/danielmorrigan/temp.data", "a+")
# while true do
#
#   serial_in = sp.getc
#   my_file.printf("%c", serial_in)
#   printf("%c", serial_in)
#  
# end
my_file.close
sp.close


Can anyone point me to where I may have gone wrong?

Go Up