hello everyone,
I am trying to interface CO2 module with arduino. As per the instructions of the data, to get the data on serial port through TTL. I am suppose to follow the following instruction:
Initialize UART as: baud rate - 19200, data - 8 bits, stop bit - 1 bit, parity- none.
Send command 80h 02h 00h 7Eh to CO2 system until uplink response with type being 80h
Anyone can help me establish a arduino code to retrieve the data from CO2 module in serial using TTL cable.
Dear Sir,
I am using CO2CGM Module. I have included softwareserail.h library. As per the guideline, i need to send a command 80h 02h 00h 7Eh to co2 module to get the data. How could i write the mentioned code in arduino. As it is written in assembly, if i m not wrong.
I would really appreciate if u can provide me the arduino code for mentioned one (80h 02h 00h 7Eh).
i am unable to get any output at the end: i m looking for the data on serial port. pls correct me if anything worng.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
String val= ""; //holds the string of the value
double co2 =0; // holds the actual value
uint8_t buffer[128];
uint8_t ind =0;
byte initPort[] = {0x80,0x02,0x00,0x7E};
void setup() {
// initialize serial:
Serial.begin(19200);
// print the string when a newline arrives:
mySerial.begin(19200);
Serial.write(initPort, sizeof(initPort));
int hexIn = Serial.read();
}
void loop() {
//print the value on serial
while(buffer[ind-1] != 0x80)
{
if(mySerial.available())
{
buffer[ind] = mySerial.read();
ind++;
}
}
Serial.print( "Co2 = ");
Serial.print(co2);
Serial.println(" mmHg");
ind=0; //Reset the buffer index to overwrite the previous packet
}
Serial.write(initPort, sizeof(initPort));
int hexIn = Serial.read();
Where are you sending the initPort?? Is that your device on Serial?? What are you reading back?
In the loop()
uint8_t buffer[128];
uint8_t ind =0;
....
while(buffer[ind-1] != 0x80) // <-- first Time you arrive here what do you think ind-1 is?
Rethink your code structure. The loop should not need a while
If something arrives on serial
handle it and add to buffer at index position
If what I just recieved is the end of the communication
set a '\0' at the end of the buffer
print the buffer
Reset the buffer index to zero
Else increase index and check for buffer end/ overflow
End if
End if
19200 is no pb for software serial. Yes can't do both at the same time but in his case it's not needed so should be fine. His issue is that he is confusing his hardware Serial port with the software one and sending commands to the wrong place most likely.
Dear Sir, I have written the general code and getting output as a ~€. #include <SoftwareSerial.h>
SoftwareSerial mySerial(4, 5); // RX, TX
byte GetData[] = {0x80,0x02,0x00,0x7E};
void setup() {
// initialize serial:
Serial.begin(19200);
}
void loop()
{
Serial.write(GetData,sizeof(GetData));
}
while compiling the code, i am also getting overbaudrate 115200.
Is that the complete program?
It does not use SoftwareSerial at all.
You need a delay() - perhaps delay(300); - in loop() to slow it down so it does not overwhelm the serial interface.
Are you viewing the output in the Serial Monitor?
0x7E is the only one of your values that matches a printable character - what are you expecting to see?
dear friend,
i m able to receive the data as per need. now i need to formulate. on serial port, i m getting 5 values for example A B C D E and i wanna use in formula 4 and 5.
the formula for this as following:
((128*D+ E)-1000)/100
I would really appreciate you guys. if u can give some solution.
the above code i have used to extract the data. now the problem is that as per the command i need to get only 5 bytes. I am getting frequently the same but in rare i am getting 4 and 7 bytes which i am suppose to discard.
I would really appreciate to you if u can provide some idea.