0

ARDUINO ボードに 1/0 を送信しようとしています & 応答としてボードからいくつかのデータを受信しようとしています。これには QextSerialPort (Qt lib) を使用していますが、ボードにデータを書き込むことができません & できません任意のデータも受信します。

QextSerialPort

qDebug() << "send.size() : " << send.size() << " data = " << send.data() <<" Written = " << port->write(send, send.サイズ()); この印刷 : send.size() : 1 data = 1 Written = 0 //毎回 0 バイトを書き込むことを意味します

私のコードに問題はありますか??

void MainWindow::ledOnOff(bool on)
{
    if(port == 0)
    {
        port = new QextSerialPort("COM6", QextSerialPort::EventDriven);     //QextSerialPort* port  is class member 
        port->setBaudRate(BAUD9600);
        port->setFlowControl(FLOW_OFF);
        port->setParity(PAR_NONE);
        port->setDataBits(DATA_8);
        port->setStopBits(STOP_2);

        connect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));

        if(port->open(QIODevice::ReadWrite) == true)
        {
            qDebug() << "Port open success";
        }
        else
        {
            qDebug() << "Port open success";
        }
    }
    else
    {
        port->close();
    }

    quint8 writeByte = 0;

    if(on)
    {
        writeByte = 1;
    }

    if(port->isOpen() || port->open(QIODevice::ReadWrite) == true)
    {
        QByteArray send;
        send.resize(writeByte );
        send = QByteArray::number(writeByte);

        port->flush();
        qDebug() << "send.size() : " << send.size() << "  data = " << send.data()
                 <<"  Writtend = " <<  port->write(send, send.size());
    }
    else
    {
        qDebug() << "device failed to open:" << port->errorString();
    }
}


void MainWindow::onReadyRead()
{
    QByteArray bytes;
    quint8 a = port->bytesAvailable();
    bytes.resize(a);
    port->read(bytes.data(), bytes.size());

    qDebug() <<  bytes.constData();
}

私のArduinoコードは次のとおりです。

  uint8_t chosen = 0;

  void setup() 
  { 
    pinMode(13, OUTPUT);
    Serial.begin(9600);
  } 

  void loop() 
  { 

if(Serial.available())
{
    switch(Serial.read())
    {
    case '1':
      chosen = 1;
      break;
    case '2':
      chosen = 2;
      break;
    default:
      Serial.println("Bad Choice.");
      chosen = 0;
    }

    Serial.println(chosen, DEC);

    switch(chosen)
    {
      case 1:
        digitalWrite(13, HIGH);
        break;
      case 2:
        digitalWrite(13, LOW);
        break;
      default:
        ;
    }
  }
}

解決済み:

Qt5.5でQSerialPortクラスに切り替えましたが、うまくいきました。

&私のaurdinoコードにも問題があります(コピーペースト効果)

 switch(Serial.read())
    {
    case 1:        //It should be 1 not '1'
      chosen = 1;
      break;
    case 2:       //It should be 2 not '2'    
      chosen = 2;
      break;
    default:
      Serial.println("Bad Choice.");
      chosen = 0;
    }
4

1 に答える 1