Go Down

Topic: Serial Communication using NewSoftwareSerial and Android Meets Arduino (Read 514 times) previous topic - next topic

erwinreu

Hey guys quick question.

I am communicating with this CO2 sensor. When I pass it a hex string over serial to the arduino and then arduino communicates to the sensor over the software serial it works perfectly.

When I am passing a hex string from python, this code works perfectly.



Code: [Select]
 if (mySerial.available())
 {        
    Serial.print((char)mySerial.read());
 

  }
 if (Serial.available())
 {  
     mySerial.print((char)Serial.read());
     
 }


Except that when I try to replace this
Code: [Select]
if (Serial.available())
 {  
     mySerial.print((char)Serial.read());
     
 }


with hard coded bytes I'm passing to the sensor

Code: [Select]

   mySerial.print(byte(254));
   mySerial.print(byte(04));
   mySerial.print(byte(00));
   mySerial.print(byte(03));
   mySerial.print(byte(00));
   mySerial.print(byte(01));
   mySerial.print(byte(213));
   mySerial.print(byte(197));


It only works once and any other communication back is empty.

Any suggestions.

BTW the hex string I'm sending from python over serial is
'\xfe\x04\x00\x03\x00\x01\xd5\xc5'

Any help or advice is appreciated.

Alternatively if someone can tell me if it's possible to send a hex string using Amarino (android meets arduino) such that I can pass the hex string to the sensor and use the (working) first code.

PaulS

If you are passing bytes to the serial port, the Serial.write() method is more appropriate. Or, use the optional second argument, BYTE, in the Serial.print() call.

erwinreu

Tried using Serial.write but that didn't change anything. What astounds me is that when I pass the hex from the computer I have it connected to it works, yet when it's passed from the arduino, it only works the first time and all subsequent times the sensor (it appears) doesn't return anything.

Here is my full code.

Code: [Select]
#include <NewSoftSerial.h>

#define rxPin 2
#define txPin 4


NewSoftSerial mySerial =  NewSoftSerial(rxPin, txPin);


// This function prints "siz" HEX values from the "buf" array
void PrintHexBuffer()
{
    mySerial.print(byte(254));
    mySerial.print(byte(04));
    mySerial.print(byte(00));
    mySerial.print(byte(03));
    mySerial.print(byte(00));
    mySerial.print(byte(01));
    mySerial.print(byte(213));
    mySerial.print(byte(197));

}


void setup()                    // run once, when the sketch starts
{
  mySerial.begin(9600);
  Serial.begin(9600);    // set up Serial library at 9600 bps
  PrintHexBuffer();
}

void loop()                       // run over and over again
{         // do nothing!
  while (mySerial.available()==0)
  {}
  while (mySerial.available())
  {       
    Serial.write((char)mySerial.read());

  }
  PrintHexBuffer();
  while(mySerial.available()==0)
  {}
 

 
}

Go Up