您的位置:首页 > 编程语言 > Qt开发

QT serial

2015-12-22 14:17 295 查看

遍历当前chuankou

foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
qDebug() <<"Name        : "<<info.portName().toStdString().data();
qDebug() <<"Description : "<<info.description().toStdString().data();
qDebug() <<"Manufacturer: "<<info.manufacturer().toStdString().data();
// Example use QSerialPort
ui->serial_comboBox->addItem(info.portName());
printf(" \n");

}


foreach 为qt中预编译宏变量

#define Q_FOREACH(variable, container)                                \
for (QForeachContainer<__typeof__(container)> _container_(container); \
!_container_.brk && _container_.i != _container_.e;              \
__extension__  ({ ++_container_.brk; ++_container_.i; }))                       \
for (variable = *_container_.i;; __extension__ ({--_container_.brk; break;}))

#else


If you just want to iterate over all the items in a container in order, you can use Qt’s foreach keyword. The keyword is a Qt-specific addition to the C++ language, and is implemented using the preprocessor.

Its syntax is: foreach (variable, container) statement. For example, here’s how to use foreach to iterate over a QLinkedList:

QLinkedList<QString> list;

QString str;
foreach (str, list)
qDebug() << str;


qt 初始化函数

bool Port_Hw::Port_Hw_Setup(QString port_name,int baudrate)
{
QString str = "\\\\.\\";
int index=findindex(baudrate);
if (-1 == index)
{
index=baudrate;
}
bool ret = false;
str += port_name;
port->setPortName(str);
ret = port->open(QIODevice::ReadWrite|QIODevice::Unbuffered);
port->setBaudRate(baudrate);
port->setFlowControl(QSerialPort::NoFlowControl);
port->setParity(QSerialPort::NoParity);
port->setDataBits(QSerialPort::Data8);
port->setStopBits(QSerialPort::OneStop);
//set timeouts to 20 ms
port->waitForBytesWritten(20);
port->waitForReadyRead(20);
if(ret == true)
{
printf("%s,line %d,OPEN SUCCESS\n",__func__,__LINE__);
port->flush();
}
return ret;
}


send bytes

int Port_Hw::Port_Hw_Send(unsigned char *buf, int len)
{
int sent = 0;
int ret;
if(port->isOpen())
{
for(;sent < len;)
{
ret = port->write((char *)buf + sent,len - sent);
port->waitForBytesWritten(-1);
if(port->isOpen() == false)
{
break;
}
if(ret > 0)
{
sent += ret;
}
}
}
return sent;
}


flush

void Port_Hw::Port_Hw_Flush()
{
if(port->isOpen())
{
port->flush();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  qt