1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// the setup function runs once when you press reset or power the board
unsigned char dataBuf[15= {0, };
 
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  Serial.begin(115200);
  
  while (!Serial) {
     // wait for serial port to connect. Needed for native USB port only
  }
}
 
// the loop function runs over and over again forever
void loop() {
 if (Serial.available()) {
    
    Serial.readBytes(dataBuf, 15);
    Serial.write(dataBuf, 15);
    
    Serial.flush();
 }
  
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "mainwindow.h"
#include "ui_mainwindow.h"
 
#include <QMessageBox>
#include <QDebug>
#include "3vmw9.hpp"
#include "QtSerialPort/QSerialPort"


using namespace rv2;
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
 
   QMessageBox box;
 
   port = new QSerialPort();
   port->setPortName("COM3");
   port->setBaudRate(QSerialPort::Baud115200);
   port->setDataBits(QSerialPort::Data8);
   port->setParity(QSerialPort::NoParity);
   port->setStopBits(QSerialPort::OneStop);
   port->setFlowControl(QSerialPort::NoFlowControl);
 
   if(!port->open(QIODevice::ReadWrite)){
       box.setText("serial open failed");
       box.exec();
   }
   qDebug() << "con sucessed" << endl;
   connect(ui->pushButton,SIGNAL(clicked(bool)), this, SLOT(text_Sending()));
   connect(port,SIGNAL(readyRead()), this, SLOT(text_Reading()));
 
 
}
 
MainWindow::~MainWindow()
{
    delete ui;
 
}
void showBytes(y::p bytes){
    QMessageBox box;
    QString msg;
 
    msg.sprintf("0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X",
                bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7], bytes[8], bytes[9], bytes[10]);
 
    box.setText(msg);
    box.exec();
 
}
 
void MainWindow::text_Reading(){
 
    qDebug() << "text_Reading" << endl;
 
    QByteArray read_Data;
    read_Data = port->readAll();
 
    y::p data = new y::t[read_Data.size()];
    memcpy(data, read_Data, read_Data.size());
 
    showBytes(data);
}
 
void MainWindow::text_Sending(){
 
    qDebug() << "text_Sending" << endl;
 
    y::t send_Data[15= {0xFF0xFE0x000xEE00xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0 };
    QByteArray send_Data_Bytes = QByteArray::fromRawData((char*)send_Data, 15);
 
    //QByteArray send_Data_Bytes("\xFF\xFE\x00\xFF\xFE\x00\xFF\xFE\x00\xFF\xFE\x00",15);
 
    qDebug() << "send_data _Sending" << endl;
 
    //QByteArray send_Data;
    //send_Data = ui->lineEdit->text().toUtf8().left(1);
 
    port->write(send_Data_Bytes);
    qDebug() << "write end" << endl;
 
}
 
void MainWindow::on_pushButton_clicked()
{
 
}
 
cs


QT       += core gui serialport


+ Recent posts