News:

;) This forum is the property of Proton software developers

Main Menu

C++

Started by Dolci, Feb 13, 2024, 08:17 AM

Previous topic - Next topic

Dolci

Hello everyone,

Can someone please translate what is the equivalent of this c++ (not sure) code to positron. I have a fair of UHF-STD Transceiver on hand which I want to integrate into my plant watering system. I have zero knowledge regarding c language. The short code is only an example given on their site. Thank you.

UHF TRANSCEIVER

//Codes for Receiver
// it can transmit and received

String inputString = "";         // a string to hold incoming data
String COMMAND = "";

void setup() { 

  Serial.begin(9600);
  inputString.reserve(200); 

}

void loop() {
  //Transmitting data
   Serial.println("Receiver");
    delay(1000);
   
        Serial.println("1");
    delay(1000);
   
        Serial.println("2");
    delay(1000);
   
        Serial.println("3");
    delay(1000);

}

void serialEvent() {

  while (Serial.available() > 0) {

    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    inputString.trim();

    if (inChar == '\n') {
     
      COMMAND = inputString.substring(0);
      COMMAND.trim();

      Serial.println(COMMAND);
       
      inputString= "";
      COMMAND = "";

    }
  }
}

//Codes for Transmitter
// it can transmit and received

String inputString = "";         // a string to hold incoming data
String COMMAND = "";

void setup() {
  Serial.begin(9600);
  inputString.reserve(200);
}

void loop()
{
  //Trasmitting data
    Serial.println("Transmitter");
    delay(1000);
   
        Serial.println("1");
    delay(1000);
   
        Serial.println("2");
    delay(1000);
   
        Serial.println("3");
    delay(1000);

}


void serialEvent() {
  while (Serial.available() > 0) {

    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    inputString.trim();

    if (inChar == '\n') {

      COMMAND = inputString.substring(0);
      COMMAND.trim();
     
     Serial.println(COMMAND);
     
     inputString= "";
     COMMAND = "";
   }
  }
}


JonW

#1
Dolci, don't look at the conversion to basic; I would write new positron code based on the datasheet.  These look like transparent serial to RF - RF - Serial modules.  The comms is 9600,8,n,1 with a 96-byte transmit buffer terminated by adding \n. So I presume the modules keep topping up their buffer and send the data on reception of the \n or LF or (0Ah) character.  The receiver will likely accumulate the data and then transmit it to your serial port when it receives the LF(0Ah) character, which is also transmitted to the host MCU, terminating the packet so you can process the data.

I would first use 2 PC serial ports with a VCP port IC (FTDI, etc.) and two serial terminals to ensure the channels are correct and data is bidirectional and correct.  You will need a serial interrupt buffer to capture the received data and accumulate it, wait for a termination character of LF (0ah) that will signal the end of the packet and set a flag to process the buffer.

The code above

Transmit: Waits for data to accumulate in the serial buffer and transmits the buffer including the \n to the module serial input.
Receive:  Waits for a byte to be received and adds it to a receive buffer, on reception of the \n character it processes the packet.

EDIT: I just looked at the datasheet again and a CR character may terminate the transmission and the \n is used as a host terminator.  Just connect to two terminals using USB to TTL converters and have a play

Dolci

Thank you JonW, I will try to make some test after I finish my developement board PCB. Which usb to ttl can you recommend?

JonW

I would add one onto your development board, and then you also have a PC to RF RF to PC board.  I use the Silabs CP2102 with USBC if I build in JLC, as these are in stock and cheaper than FTDI parts.
There are loads of cheap FTDI ones on Amazon if you require a standalone one.

FTDI USB to TTL

I dont like using WCH ones as these tend to have driver issues.  CP21012 may also be an issue with some win versions but there are drivers.

John Lawton

I've used & like the FTDI adaptor cable to interface to TTL level serial on boards:
https://uk.rs-online.com/web/p/development-tool-accessories/6877786
It may be cheaper from other suppliers :)