You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
TermApp/TestComm/app/src/main/cpp/SpiPort.cpp

62 lines
1.1 KiB
C++

#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <android/log.h>
#include <string>
#include "SpiPort.h"
SpiPort::SpiPort(const std::string& path) : m_path(path), m_fd(0)
{
}
SpiPort::~SpiPort()
{
if (m_fd > 0)
{
close(m_fd);
}
}
bool SpiPort::Open()
{
if(m_fd <= 0) m_fd = open(m_path.c_str(), O_RDWR/*|O_NDELAY|O_NOCTTY*/);
if(m_fd <= 0 ) {
int err = errno;
m_log = "open spi Error errno=" + std::to_string(err);
__android_log_print(ANDROID_LOG_INFO, "SPi", "open spi Error errno=%d", err);
return false;
}
else __android_log_print(ANDROID_LOG_INFO, "SPi", "open spi Success m_fd=%d",m_fd);
return true;
}
// write
int SpiPort::Write(unsigned char *buf, int len)
{
return write(m_fd, buf, len);
}
// read
int SpiPort::Read(unsigned char *buf, int len)
{
return read(m_fd, buf, len);
}
//close
bool SpiPort::Close()
{
if(m_fd > 0)
{
close(m_fd);
m_fd = 0;
}
return true;
}