127 lines
4.3 KiB
C++
127 lines
4.3 KiB
C++
//serial.h的具体实现,要注意的地方是数据类型的转换,QLineEdit的text().toASCII返回的是QByteArray,而PortSend要发送的是
|
|
//char类型的数据,所以就需要转换,在Qt中使用.data()就可以实现转换。还有要注意的地方就是QSocketNotifier不要忘记添加了。
|
|
#include "serial.h"
|
|
portinfo_t portinfo = {
|
|
'0', // print prompt after receiving
|
|
115200, // baudrate: 115200
|
|
'8', // databit 8
|
|
'0', // debug: off
|
|
'0', // echo off
|
|
'0', // flow control: software
|
|
'0', // default tt: SAC0
|
|
'0', // parity: none
|
|
'1', // stopbit: 1
|
|
0 // reserved
|
|
};
|
|
/*
|
|
typedef struct //define the uart value struct
|
|
{
|
|
char prompt; //prompt after reciving data
|
|
int baudrate; //baudrate
|
|
char databit; //data bits, 5, 6, 7, 8
|
|
char debug; //debug mode, 0: none, 1: debug
|
|
char echo; //echo mode, 0: none, 1: echo
|
|
char fctl; //flow control, 0: none, 1: hardware, 2: software
|
|
char tty; //tty: 0, 1, 2, 3, 4, 5, 6, 7
|
|
char parity; //parity 0: none, 1: odd, 2: even
|
|
char stopbit; //stop bits, 1, 2
|
|
//const int reserved; //reserved, must be zero
|
|
int reserved; //reserved, must be zero
|
|
}portinfo_t;
|
|
*/
|
|
static int serial_fd;
|
|
char databuffer[20];
|
|
int datalen;
|
|
serial::serial(QWidget *parent)
|
|
: QWidget(parent)
|
|
{
|
|
|
|
// layout();//lay out
|
|
|
|
// open_button->setEnabled(true);
|
|
// close_button->setEnabled(false);
|
|
// send_button->setEnabled(false);
|
|
|
|
// connect(open_button,SIGNAL(clicked()),this,SLOT(open_serial()));
|
|
// connect(close_button,SIGNAL(clicked()),this,SLOT(close_serial()));
|
|
// connect(send_button,SIGNAL(clicked()),this,SLOT(send_setial()));
|
|
|
|
QSocketNotifier *notifier=new QSocketNotifier(serial_fd,QSocketNotifier::Read,this);
|
|
connect(notifier,SIGNAL(activated(int)),this,SLOT(read_serial()));
|
|
read_serial();
|
|
}
|
|
serial::~serial()
|
|
{
|
|
|
|
}
|
|
void serial::init_serial( int baudrate, int portNumber,int stopbit )
|
|
{
|
|
portinfo.baudrate = baudrate;
|
|
portinfo.tty = portNumber+'0';
|
|
portinfo.stopbit = stopbit+'0';
|
|
portinfo.fctl = '0';
|
|
}
|
|
void serial::open_serial()
|
|
{
|
|
serial_fd=PortOpen(&portinfo);
|
|
if(serial_fd<0)
|
|
{
|
|
perror("Error:open serial port error");
|
|
//exit(1);
|
|
}
|
|
PortSet(serial_fd,&portinfo);
|
|
QSocketNotifier *notifier=new QSocketNotifier(serial_fd,QSocketNotifier::Read,this);
|
|
connect(notifier,SIGNAL(activated(int)),this,SLOT(read_serial()));
|
|
read_serial();
|
|
}
|
|
void serial::close_serial()
|
|
{
|
|
PortClose(serial_fd);
|
|
|
|
}
|
|
void serial::read_serial()
|
|
{
|
|
datalen=PortRecv(serial_fd,databuffer,10,115200);
|
|
//textbrowser->insertPlainText(databuffer);
|
|
qDebug()<<"read:"<<datalen;
|
|
}
|
|
|
|
|
|
void serial::send_serial(char *buf,int len)
|
|
{
|
|
PortSend(serial_fd,buf,len);
|
|
}
|
|
|
|
//void serial::layout()
|
|
//{
|
|
// label1=new QLabel("Receive:");
|
|
// label2=new QLabel("Send:");
|
|
// open_button=new QPushButton("Open");
|
|
// close_button=new QPushButton("Close");
|
|
// send_button=new QPushButton("Send");
|
|
// textbrowser=new QTextBrowser(this);
|
|
// lineedit=new QLineEdit;
|
|
// lineedit->setText("123489");
|
|
// QVBoxLayout *vlayout1=new QVBoxLayout;
|
|
// vlayout1->addWidget(label1);
|
|
// vlayout1->addWidget(textbrowser);
|
|
// vlayout1->addWidget(label2);
|
|
// QVBoxLayout *vlayout3=new QVBoxLayout;
|
|
// vlayout3->addWidget(open_button);
|
|
// vlayout3->addWidget(close_button);
|
|
// QHBoxLayout *hlayout2=new QHBoxLayout;
|
|
// hlayout2->addLayout(vlayout1);
|
|
// hlayout2->addLayout(vlayout3);
|
|
// QHBoxLayout *hlayout1=new QHBoxLayout;
|
|
// hlayout1->addWidget(lineedit);
|
|
// hlayout1->addWidget(send_button);
|
|
// QVBoxLayout *vlayout2=new QVBoxLayout;
|
|
// vlayout2->addLayout(hlayout2);
|
|
// vlayout2->addLayout(hlayout1);
|
|
// setLayout(vlayout2);
|
|
//}
|
|
|
|
//总结:总算完成了,前后花了两天的时候,很高兴的是自己没有放弃。至于这个串口的QT有什么用,那作用老大了,以前我用VB写过环境
|
|
//监控系统,在PC端实现的,低下用的是ADUC824,那代码,叫宏伟啊。Qt就简单多了(站在巨人的肩膀上)。有了UART的通信,一些
|
|
//控制类的东西都可以实现了,而且界面还可以做得要多漂亮就有多漂亮(看个人的了)。
|