• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

C++ How to print "Hello world" in OTCv8 in terminal

lexus21

Active Member
Joined
Dec 14, 2022
Messages
86
Reaction score
25
Hello, i want to write simple code in c++ to otcv8. I created a file called com.cpp in the project (microsoft visual studio) in sources->client.
Now I put the following code there:
#include <iostream>

int badname() {
std::cout << "Hello, world!" << std::endl;
return 0;
}

otclient is comiling but, i can't see Hello world! after launch otclient in terminal, how to print "Hello World" in otclient in terminal every 1 sec?
 
Last edited:
Hello, i want to write simple code in c++ to otcv8. I created a file called com.cpp in the project (microsoft visual studio) in sources->client.
Now I put the following code there:


otclient is comiling but, i can't see Hello world! after launch otclient in terminal, how to print "Hello World" in otclient in terminal every 1 sec?

g_logger.info("Hello, world!"); --> Hello, world!
int myNumber = 7; g_logger.info(stdext::format("This is my number: %d!", myNumber)); --> This is my number: 7!
 
Last edited:
so, I managed to print "Hello world" in the terminal. Thank you for your help.
I needed this to generally understand how it works.
Now, I want the code to display the value from COM5 every second.
I hope I'm doing well.

com.cpp

C++:
#include <boost/asio/read.hpp>
#include <boost/asio/write.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include "framework/global.h"
#include "com.h"


COM g_com;

COM::COM() : port(io_service, "COM5") {
    // Configure serial port settings (move from init() to constructor)
    port.set_option(boost::asio::serial_port_base::baud_rate(9600));
    port.set_option(boost::asio::serial_port_base::character_size(8));
    port.set_option(boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none));
    port.set_option(boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one));
    port.set_option(boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::none));
}

void COM::init() {
    // Create io_service object
    boost::asio::io_service io_service;

    // Declare a buffer to store received data

    // Asynchronously read data into the buffer
    boost::asio::async_read(port, buffer,
        [this](boost::system::error_code error, std::size_t bytes_transferred) {
            if (!error) {
                // Process the received data
                std::istream is(&buffer);
                std::string line;
                while (std::getline(is, line)) {
                    std::cout << line << std::endl;
                }

                // Asynchronously read the next fragment of data
                boost::asio::async_read(port, buffer,
                    [this](boost::system::error_code error, std::size_t bytes_transferred) {
                        if (!error) {
                            // Process the received data
                            g_logger.info(stdext::format("This is my output form com: %s", boost::asio::buffer_cast<const char*>(buffer.data())));

                        }
                        else {
                            // Error handling
                            g_logger.info("Error");
                        }
                    });
            }
            else {
                // Error handling
                std::cerr << "Error reading from serial port: " << error.message() << std::endl;
            }
        });

    // Run the event loop to process asynchronous operations
    io_service.run();
}

void COM::terminate() {
    g_logger.info("Bye!");
}


and com.h

C++:
#ifndef COM_H
#define COM_H

#include <boost/asio.hpp>

class COM {
public:
    COM(); // Constructor
    void init();
    void terminate();

private:
    boost::asio::io_service io_service;
    boost::asio::serial_port port;
    boost::asio::streambuf buffer;
};

extern COM g_com;

#endif

i don't know why but i have
1706729689208.png
the port is underlined (
(local variable) boost::asio::serial_port port)

can't compile the code

how to solve it?
What could be changed in the code?

I don't know if this will work.



edit. program is compiling but no output in terminal in otclient - i know this code is not easy but thank you for any tip
 
Last edited:
Back
Top