1 工程建立

(1)在之前的工程上,新建serial.cpp、serial.h、serial.ui。

(2)设计ui文件。

(3)初始化发送框以及ComboBox控件。

1
2
3
4
5
6
7
8
9
/* 初始化发送框 */
ui->LineEditSend->setText("HI3516 Serial Test!");

/* 初始化选择控件 */
ui->ComboBoxSerialPort->addItems({"UART1", "UART4"});
ui->ComboBoxSerialbaud->addItems({"115200", "19200", "9600", "4800", "2400", "1200", "300"});
ui->ComboBoxSerialStop->addItems({"1", "1.5", "2"});
ui->ComboBoxSerialNum->addItems({"8", "7", "6", "5"});
ui->ComboBoxSerialCheck->addItems({"无", "奇校验", "偶校验"});

(4)设计打开串口、发送按钮槽函数

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
/*
* 函数名称:void on_PbSerialOpen_released()
* 函数功能:打开串口按钮的槽函数
* 作者:JR.Wang
* 日期:2024.07.29
*/
void serial::on_PbSerialOpen_released()
{
char HiSerialDev[32];
char devbuf1[32]="/dev/ttyAMA1"; // 串口1
char devbuf2[32]="/dev/ttyAMA4"; // 串口4
int baudbit; // 波特率
int numbit; // 数据位
int stopbit; // 停止位
int checkbit; // 奇偶校验位

/* 串口选择控件 */
if(!ui->ComboBoxSerialPort->currentText().compare("UART1"))
memcpy(HiSerialDev,devbuf1,32);
else
memcpy(HiSerialDev,devbuf2,32);

/* 波特率、数据位、停止位、奇偶校验位控件 */
baudbit = ui->ComboBoxSerialbaud->currentText().toInt();
numbit = ui->ComboBoxSerialNum->currentText().toInt();
stopbit = ui->ComboBoxSerialStop->currentText().toInt();
if(!ui->ComboBoxSerialCheck->currentText().compare("奇校验"))
checkbit = 'o';
else if(!ui->ComboBoxSerialCheck->currentText().compare("偶校验"))
checkbit = 'e';
else
checkbit = 'N';

/* 打开串口 */
HiSerfd = HI_Serial_Open(HiSerialDev);
reg = HI_Serial_Init(HiSerfd, baudbit,0,numbit,stopbit,checkbit);
if(reg==HI_TRUE)
{
ui->ComboBoxSerialPort->setDisabled(true);
ui->ComboBoxSerialbaud->setDisabled(true);
ui->ComboBoxSerialStop->setDisabled(true);
ui->ComboBoxSerialNum->setDisabled(true);
ui->ComboBoxSerialCheck->setDisabled(true);
ui->PbSerialOpen->setDisabled(true);
/* 开启数据接收线程 */
if (!ThreadRecv->isRunning())
ThreadRecv->start();
}
}
/*
* 函数名称:void on_PbSerialSend_released()
* 函数功能:发送按钮的槽函数
* 作者:JR.Wang
* 日期:2024.07.29
*/
void serial::on_PbSerialSend_released()
{
qDebug()<<"Sending......";
char sendbuf[1024];
memcpy(sendbuf,ui->LineEditSend->text().toLatin1(),1024);
HI_Serial_Send(HiSerfd, sendbuf, strlen(sendbuf)+1);
}

(5)创建数据接收线程。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class MyThreadSerial : public QThread
{
/* 用到信号槽即需要此宏定义 */
Q_OBJECT

public:
MyThreadSerial(QWidget *parent) {
Q_UNUSED(parent);
}

/* 重写run方法,继承QThread的类,只有run方法是在新的线程里 */
void run();

signals:

/* 声明一个信号,译结果准确好的信号 */
void ResultReadyRecv(const QString &s);

};

重写run()函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
* 函数名称:void run()
* 函数功能:接收串口数据线程运行
* 作者:JR.Wang
* 日期:2024.07.29
*/
void MyThreadSerial::run()
{
int ret = 0;
while(1)
{
ret = HI_Serial_Recv(HiSerfd, recvbuf, sizeof(recvbuf));
if (ret > 0)
{
emit ResultReadyRecv(recvbuf);
memset(recvbuf,0,1024);
}
}
}

(6)定义线程信号接收函数。

1
2
/* 设置槽连接 */
connect(ThreadRecv, SIGNAL(ResultReadyRecv(QString)), this, SLOT(handleResultsRecv(QString)));
1
2
3
4
5
6
7
8
9
10
11
12
/*
* 函数名称:void handleResultsRecv()
* 函数功能:返回接收数据线程信息的槽函数
* 作者:JR.Wang
* 日期:2024.07.29
*/
void serial::handleResultsRecv(const QString &result)
{
/* 打印出线程发送过来的结果 */
qDebug()<<result<<endl;
ui->TextEditReceive->append(result);
}

2 移植到开发板运行

(1)海思Hi3516串口引脚如下图,使用UART1。

(2)运行程序