「Qtの基礎 - I2C通信」の版間の差分
		
		
		
		
		
		ナビゲーションに移動
		検索に移動
		
				
		
		
	
| 2行目: | 2行目: | ||
| <br><br> | <br><br> | ||
| ==  | == I2C通信の受信 == | ||
| 以下の例では、非同期処理を使用して、I2C通信の受信を行っている。<br> | |||
| 適切なI2Cデバイス名 (例: /dev/i2c-1) とスレーブアドレスを指定する必要があることに注意する。<br> | 適切なI2Cデバイス名 (例: /dev/i2c-1) とスレーブアドレスを指定する必要があることに注意する。<br> | ||
| <br> | <br> | ||
| 111行目: | 111行目: | ||
| <br> | <br> | ||
| 上記のクラスを使用して、I2C通信でデータを受信する。<br> | 上記のクラスを使用して、I2C通信でデータを受信する。<br> | ||
| <br> | |||
|   <syntaxhighlight lang="c++"> |   <syntaxhighlight lang="c++"> | ||
|   #include "I2CReader.h" |   #include "I2CReader.h" | ||
| 122行目: | 123行目: | ||
|      // I2C通信で受信 |      // I2C通信で受信 | ||
|      readerManager.readData(0x50, 10); |      readerManager.readData(0x50, 10); | ||
|     return a.exec(); | |||
|  } | |||
|  </syntaxhighlight> | |||
| <br><br> | |||
| == I2C通信の送信 == | |||
| 以下の例では、非同期処理を使用して、I2C通信の送信を行っている。<br> | |||
| <br> | |||
| 適切なI2Cデバイス名 (例: /dev/i2c-1) とスレーブアドレスを指定する必要があることに注意する。<br> | |||
| <br> | |||
|  <syntaxhighlight lang="c++"> | |||
|  // I2CWriter.hファイル | |||
|  #include <QObject> | |||
|  #include <QI2CDevice> | |||
|  #include <QFuture> | |||
|  #include <QtConcurrent> | |||
|  #include <QDebug> | |||
|  class I2CWriter : public QObject | |||
|  { | |||
|     Q_OBJECT | |||
|  private: | |||
|     QI2CDevice *m_device; | |||
|  public: | |||
|     explicit I2CWriter(const QString &deviceName, QObject *parent = nullptr) : QObject(parent), m_device(new QI2CDevice(this)) | |||
|     { | |||
|        if (!m_device->open(deviceName)) { | |||
|           emit errorOccurred("デバイスのオープンに失敗: " + m_device->errorString()); | |||
|        } | |||
|     } | |||
|     ~I2CWriter() | |||
|     { | |||
|        if (m_device->isOpen()) { | |||
|           m_device->close(); | |||
|        } | |||
|     } | |||
|     QFuture<bool> writeDataAsync(int address, const QByteArray &data) | |||
|     { | |||
|        return QtConcurrent::run([this, address, data]() { | |||
|           if (!m_device->isOpen()) { | |||
|              emit errorOccurred("デバイスが開かれていない"); | |||
|              return false; | |||
|           } | |||
|           m_device->setSlaveAddress(address); | |||
|           if (m_device->write(data.constData(), data.size()) != data.size()) { | |||
|              emit errorOccurred("データの書き込みに失敗: " + m_device->errorString()); | |||
|              return false; | |||
|           } | |||
|           emit writeCompleted(true); | |||
|           return true; | |||
|        }); | |||
|     } | |||
|  signals: | |||
|     void writeCompleted(bool success); | |||
|     void errorOccurred(const QString &error); | |||
|  }; | |||
|  // エラーハンドリングと使用例 | |||
|  class I2CWriterManager : public QObject | |||
|  { | |||
|     Q_OBJECT | |||
|  private: | |||
|     I2CWriter *m_writer; | |||
|  public: | |||
|     explicit I2CWriterManager(QObject *parent = nullptr) : QObject(parent) | |||
|     { | |||
|        m_writer = new I2CWriter("/dev/i2c-1", this); | |||
|        connect(m_writer, &I2CWriter::errorOccurred, this, &I2CWriterManager::handleError); | |||
|        connect(m_writer, &I2CWriter::writeCompleted, this, &I2CWriterManager::handleWriteCompleted); | |||
|     } | |||
|     void writeData(int address, const QByteArray &data) | |||
|     { | |||
|        QFuture<bool> future = m_writer->writeDataAsync(address, data); | |||
|        future.then([this](bool success) { | |||
|           if (success) { | |||
|              qDebug() << "書き込み成功"; | |||
|           } | |||
|           else { | |||
|              qDebug() << "書き込みに失敗"; | |||
|           } | |||
|        }); | |||
|     } | |||
|  private slots: | |||
|     void handleError(const QString &error) | |||
|     { | |||
|        qDebug() << "エラーが発生: " << error; | |||
|        // エラーに応じて適切な処理を行う | |||
|     } | |||
|     void handleWriteCompleted(bool success) | |||
|     { | |||
|        qDebug() << "書き込み完了: " << (success ? "成功" : "失敗"); | |||
|        // 書き込み完了後の処理を行う | |||
|     } | |||
|  }; | |||
|  </syntaxhighlight> | |||
| <br> | |||
| 上記のクラスを使用して、I2C通信でデータを送信する。<br> | |||
| <br> | |||
|  <syntaxhighlight lang="c++"> | |||
|  #include "I2CWriter.h" | |||
|  int main(int argc, char *argv[]) | |||
|  { | |||
|     QCoreApplication a(argc, argv); | |||
|     I2CWriterManager writerManager; | |||
|     // 書き込み操作 | |||
|     QByteArray dataToWrite = QByteArray::fromHex("0102030405"); | |||
|     writerManager.writeData(0x50, dataToWrite); | |||
|      return a.exec(); |      return a.exec(); | ||
2024年9月17日 (火) 16:10時点における版
概要
I2C通信の受信
以下の例では、非同期処理を使用して、I2C通信の受信を行っている。
適切なI2Cデバイス名 (例: /dev/i2c-1) とスレーブアドレスを指定する必要があることに注意する。
 // I2CReader.h
 
 #include <QObject>
 #include <QI2CDevice>
 #include <QFuture>
 #include <QtConcurrent>
 #include <QDebug>
 
 class I2CReader : public QObject
 {
    Q_OBJECT
 
 private:
    QI2CDevice *m_device;
 
 public:
    explicit I2CReader(const QString &deviceName, QObject *parent = nullptr) : QObject(parent), m_device(new QI2CDevice(this))
    {
       if (!m_device->open(deviceName)) {
          emit errorOccurred("デバイスのオープンに失敗: " + m_device->errorString());
       }
    }
 
    ~I2CReader()
    {
       if (m_device->isOpen()) {
          m_device->close();
       }
    }
 
    QFuture<QByteArray> readDataAsync(int address, int size)
    {
       return QtConcurrent::run([this, address, size]() {
          if (!m_device->isOpen()) {
             emit errorOccurred("デバイスが開かれていない");
             return QByteArray();
          }
 
          m_device->setSlaveAddress(address);
          QByteArray data(size, 0);
 
          if (m_device->read(data.data(), size) != size) {
             emit errorOccurred("データの読み込みに失敗: " + m_device->errorString());
             return QByteArray();
          }
 
          emit dataReceived(data);
 
          return data;
       });
    }
 
 signals:
    void dataReceived(const QByteArray &data);
    void errorOccurred(const QString &error);
 };
 
 // エラーハンドリングと使用例
 class I2CReaderManager : public QObject
 {
    Q_OBJECT
 
 private:
    I2CReader *m_reader;
 
 public:
    explicit I2CReaderManager(QObject *parent = nullptr) : QObject(parent)
    {
       m_reader = new I2CReader("/dev/i2c-1", this);
       connect(m_reader, &I2CReader::errorOccurred, this, &I2CReaderManager::handleError);
       connect(m_reader, &I2CReader::dataReceived, this, &I2CReaderManager::handleData);
    }
 
    void readData(int address, int size)
    {
       QFuture<QByteArray> future = m_reader->readDataAsync(address, size);
       future.then([this](const QByteArray &data) {
          if (data.isEmpty()) {
             qDebug() << "受信に失敗";
          }
          else {
             qDebug() << "受信に成功:" << data.toHex();
          }
       });
    }
 
 private slots:
    void handleError(const QString &error)
    {
       qDebug() << "エラーが発生: " << error;
 
       // エラーに応じて適切な処理を行う
    }
 
    void handleData(const QByteArray &data)
    {
       qDebug() << "データを受信しました:" << data.toHex();
 
       // 受信したデータを処理する
    }
 };
上記のクラスを使用して、I2C通信でデータを受信する。
 #include "I2CReader.h"
 
 int main(int argc, char *argv[])
 {
    QCoreApplication a(argc, argv);
 
    I2CReaderManager readerManager;
 
    // I2C通信で受信
    readerManager.readData(0x50, 10);
 
    return a.exec();
 }
I2C通信の送信
以下の例では、非同期処理を使用して、I2C通信の送信を行っている。
適切なI2Cデバイス名 (例: /dev/i2c-1) とスレーブアドレスを指定する必要があることに注意する。
 // I2CWriter.hファイル
 
 #include <QObject>
 #include <QI2CDevice>
 #include <QFuture>
 #include <QtConcurrent>
 #include <QDebug>
 
 class I2CWriter : public QObject
 {
    Q_OBJECT
 
 private:
    QI2CDevice *m_device;
 
 public:
    explicit I2CWriter(const QString &deviceName, QObject *parent = nullptr) : QObject(parent), m_device(new QI2CDevice(this))
    {
       if (!m_device->open(deviceName)) {
          emit errorOccurred("デバイスのオープンに失敗: " + m_device->errorString());
       }
    }
 
    ~I2CWriter()
    {
       if (m_device->isOpen()) {
          m_device->close();
       }
    }
 
    QFuture<bool> writeDataAsync(int address, const QByteArray &data)
    {
       return QtConcurrent::run([this, address, data]() {
          if (!m_device->isOpen()) {
             emit errorOccurred("デバイスが開かれていない");
             return false;
          }
 
          m_device->setSlaveAddress(address);
 
          if (m_device->write(data.constData(), data.size()) != data.size()) {
             emit errorOccurred("データの書き込みに失敗: " + m_device->errorString());
             return false;
          }
 
          emit writeCompleted(true);
 
          return true;
       });
    }
 
 signals:
    void writeCompleted(bool success);
    void errorOccurred(const QString &error);
 };
 // エラーハンドリングと使用例
 class I2CWriterManager : public QObject
 {
    Q_OBJECT
 
 private:
    I2CWriter *m_writer;
 
 public:
    explicit I2CWriterManager(QObject *parent = nullptr) : QObject(parent)
    {
       m_writer = new I2CWriter("/dev/i2c-1", this);
       connect(m_writer, &I2CWriter::errorOccurred, this, &I2CWriterManager::handleError);
       connect(m_writer, &I2CWriter::writeCompleted, this, &I2CWriterManager::handleWriteCompleted);
    }
 
    void writeData(int address, const QByteArray &data)
    {
       QFuture<bool> future = m_writer->writeDataAsync(address, data);
       future.then([this](bool success) {
          if (success) {
             qDebug() << "書き込み成功";
          }
          else {
             qDebug() << "書き込みに失敗";
          }
       });
    }
 
 private slots:
    void handleError(const QString &error)
    {
       qDebug() << "エラーが発生: " << error;
 
       // エラーに応じて適切な処理を行う
    }
 
    void handleWriteCompleted(bool success)
    {
       qDebug() << "書き込み完了: " << (success ? "成功" : "失敗");
 
       // 書き込み完了後の処理を行う
    }
 };
上記のクラスを使用して、I2C通信でデータを送信する。
 #include "I2CWriter.h"
 
 int main(int argc, char *argv[])
 {
    QCoreApplication a(argc, argv);
 
    I2CWriterManager writerManager;
 
    // 書き込み操作
    QByteArray dataToWrite = QByteArray::fromHex("0102030405");
    writerManager.writeData(0x50, dataToWrite);
 
    return a.exec();
 }