Hello, I'm a beginner at Cpp.
The code at the bottom is the example code that I referred to. The first code is the code that I modified.
참고한 git는 https://github.com/gbmhunter/CppLinuxSerial 입니다. '#define _SERIAL_SEND_BYTE(c) '#define _SERIAL_RECV_BYTE() I don't know if it's written in the section. If possible, please check it out.
'#define _SERIAL_IS_RXBUFF_EMPTY() I need to fill out the parts, but I'm still thinking about it, so I'll think about it more.
// The code I modified
#include <CppLinuxSerial/SerialPort.hpp>
#ifndef _COMM_SETTING_H_
#define _COMM_SETTING_H_
//===========================================================================
// // Serial Communication Functions
//===========================================================================
using namespace mn::CppLinuxSerial;
// // Create serial port object and open serial port at 115200 buad, 8 data bits, no parity bit, one stop bit (8n1),
// // and no flow control
SerialPort serialPort("/dev/ttyS0", BaudRate::B_115200, NumDataBits::EIGHT, Parity::NONE, NumStopBits::ONE);
// // Use SerialPort serialPort("/dev/ttyACM0", 13000); instead if you want to provide a custom baud rate
serialPort.SetTimeout(-1); // Block when reading until any data is received
serialPort.Open();
std::string readData;
Implement the command to enable #define_SERIAL_TX_ENABLE() do{}while(0) // Tx
Implement the command to enable #define_SERIAL_RX_ENABLE() do{}while(0) // Rx
// // send/recv
#define _SERIAL_SEND_BYTE(c) do{serialPort.Write(c);}while(0) // 'char' 1byte를 send 하는 명령을 구현
#define _SERIAL_RECV_BYTE() (serialPort.Read(readData), readData[0])
// // #define _SERIAL_RECV_BYTE() (serialPort.Read(readData);) // 'char' 1byte를 receive 하는 명령을 구현
// // check rx buffer
#define_SERIAL_IS_RXBUFF_EMPTY() (serialPort.Available() == 0) // Implement a command to verify that the Rx buffer is empty
#endif // _COMM_SETTING_H_
// Example Code
#ifndef _COMM_SETTING_AVR128_H_
#define _COMM_SETTING_AVR128_H_
// // Include definition files.
//===========================================================================
// // Serial Communication Functions
//===========================================================================
// // switching tx/rx
#define _SERIAL_TX_ENABLE() do{PORTE.6 = 1; PORTE.7 = 1;}while(0) // tx, rx port 설정?
#define _SERIAL_RX_ENABLE() do{PORTE.7 = 0; PORTE.6 = 0;}while(0)
// // send/recv
#define _SERIAL_SEND_BYTE(c) do{while((UCSR0A&0x20) == 0x00); UDR0 = c; delay_us(100);}while(0) // send ready까지 wait?
#define _SERIAL_RECV_BYTE() (UDR0)
// // check rx buffer
#define _SERIAL_IS_RXBUFF_EMPTY() ((UCSR0A&0x80) == 0x00) // data를 받을 때까지 wait?
#endif // _COMM_SETTING_AVR128_H_
The SERIAL_SEND_BYTE part of the revised code seems appropriate. However, in the _SERIAL_RECV_BYTE portion, serialPort.Read is called and then readData[0] is returned. If the size of readData is 0, an exception occurs when you try to access index 0. It would be better to modify the following to return the value returned by serialPort.Read.#define _SERIAL_RECV_BYTE() (serialPort.Read(readData), (readData.empty() ? 0 : readData[0]))
And the _SERIAL_IS_RXBUFF_EMPTY part can be modified as follows.#define _SERIAL_IS_RXBUFF_EMPTY() (serialPort.Available() == 0)
The entire _COMM_SETTING_H header file applying the above codes is as follows.
#include <CppLinuxSerial/SerialPort.hpp>
#ifndef _COMM_SETTING_H_
#define _COMM_SETTING_H_
using namespace mn::CppLinuxSerial;
// // Create serial port object and open serial port at 115200 buad, 8 data bits, no parity bit, one stop bit (8n1),
// // and no flow control
SerialPort serialPort("/dev/ttyS0", BaudRate::B_115200, NumDataBits::EIGHT, Parity::NONE, NumStopBits::ONE);
// // Use SerialPort serialPort("/dev/ttyACM0", 13000); instead if you want to provide a custom baud rate
serialPort.SetTimeout(-1); // Block when reading until any data is received
serialPort.Open();
std::string readData;
Implement the command to enable #define_SERIAL_TX_ENABLE() do{}while(0) // Tx
Implement the command to enable #define_SERIAL_RX_ENABLE() do{}while(0) // Rx
// // send/recv
#define _SERIAL_SEND_BYTE(c) do{serialPort.Write(c);}while(0) // 'char' 1byte를 send 하는 명령을 구현
#define _SERIAL_RECV_BYTE() (serialPort.Read(readData), (readData.empty() ? 0 : readData[0]))
// // check rx buffer
#define _SERIAL_IS_RXBUFF_EMPTY() (serialPort.Available() == 0)
#endif // _COMM_SETTING_H_
© 2024 OneMinuteCode. All rights reserved.