Recieve Sensor Data Over Uarth

Hello everyone; There is a flow sensor and I am trying to read data from it with uart. I wasn't succesful and wanted to get support from you. I will briefly talk about the system. The baud rate required for communication is 38400. I need to send request for the sensor to start exchanging data. For the request, I need to send the start frame (0xFF) first. Then I need to set the required flags (flow and temperature) for the request. Finally, I need to send checksum.

tempsnip

As seen in the picture above, I need to send 0xC0 to set the flags. Finally, I need to set checksum. In short, this is how the systme works and I wrote a code that I don't like very much and it didm't work.

#include<SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);

uint8_t start = 0xFF;
uint8_t request[] = {0xC0};
uint8_t checksum = 0;

void setup()
{
  Serial.begin(38400);
  mySerial.begin(38400);
}

void loop()
{
  for (int j = 0; j< sizeof(request); j++)
  {
    checksum ^= request[j]; 
  }
  mySerial.write(start);
  for(int k=0; k<sizeof(request); k++)
  {
    mySerial.write(request[k]);
  }
  mySerial.write(checksum);
  
if (mySerial.available())
    Serial.write(mySerial.read());
}

Can you help me with this issue? Can you write a sample code that I can inspiration from? Thank you very much in advance for your support.

Hello again;

I did some revision in the code using Arduino MEGA. I start to recieve data, albeit meaningless, over the serial monitor. How can I make the data I recieved meaningful ?

byte request[] = {0xFF, 0xC0};
uint8_t checksum = 0;
byte data;

void setup()
{
  Serial.begin(38400);
  Serial1.begin(38400);
  
}

void loop()
{
  for (uint8_t j = 0; j< sizeof(request); j++)
  {
    checksum ^= request[j]; 
  }
  
  for(uint8_t k=0; k<sizeof(request); k++)
  {
    Serial1.write(request[k]);
  }
  Serial.write(checksum);
  delay(1000);
  
  while(Serial1.available() > 0)
  {
    Serial.println(Serial1.read());
  }

}

Can you post the datasheet of the sensor.

Do you know if your sensor is expecting TTL serial, or is it expecting RS232 voltage levels?

The sensor is a board, and it is managed by a microcontroller Thanks to the algorithm in the mcu. The flow and temperature are measured and transmitted by uart. that's why I think it is TTL serial. what I want to see the decimal value read from sensor board on the serial monitor.

Easy to verify with your DVM.

Shouldn't the checksum for the transmission be going to the device over Serial1?

Good spot by @cattledog!

@Atahan-Uyanik , if you don't have a datasheet for the device, then how do you know how to control it. How do you know what algorithm is needed to create the checksum?

@markd833 I know this info because I talked the company that designed the sensor board. There is no datasheet. They told me the flags that need to be set and the mcu won't respond without sending a request. They specified how to send request. Before meeting with them again, I wanna ask the forum first in case there is someone who has experience in such a subject before.

@cattledog thank you. You're right, I missed it. I update the code immediately. I have not set up checksum algorithm before. I searched the internet and prepare a code block like above. I wonder if I have a mistake in it, is the checksum value I sent correct ?

How can we know this. You've not told us what checksum method is required.

It looks from your code that the sensor requires a simple exclusive-OR checksum. You should initialise the checksum to zero before the for loop that performs the checksum calculation.

I understand. I don't know anything about checksum method. They stated that I could send a request as in the table in the image.

I have specified the request flags that I should send in my previous messages (0xC0).

I have one more question. I am sending the flow and temperature flags collectively with 0xC0 value. Would it be more appropriate to send them separately instead? I mean, I'm talking about sending two separate values as 0x40 and 0x80 instead of 0xC0. What is your comment ?

This is a key part of the message. Can you ask what checksum method they use. Have they provided example messages to you? We might be able to figure out the checksum calculation from them.

Does this mysterious protocol support requesting both at the same time?

Thank you very much for your support, I will get detailed info about checksum method and get support from you again. What if there was no checksum, is the request command I sent in the code I shared, and the codes I wrote to read the incoming data correct ?

No, not even then.

You have no start of frame byte (0xFF).

@Whandall , the 0xFF is the first byte of their request array defined at the top of the sketch.

If there's no checksum, then it does look like you are writing out 0xFF 0xC0 to Serial1. But why mention the checksum if you suspect that there might not be one?

We're almost at 20 posts into this discussion and the exact protocol needed to communicate with the sensor is still subject to guesswork.

No, you misunderstood me. The protocol has Checksum. Only if my checksum calculation is wrong, it means that if I learn the checksum method in detail, the problem is solved directly. That's why I asked such a question.

You definitely need to know the method for calculating the checksum. Once you have that, then the chances of the sensor responding are much higher. :grinning: