
一、QtSerialPort简介
1、串口通信基础
目前使用最广泛的串口为DB9接口,适用于较近距离的通信。一般小于10米。DB9接口有9个针脚。
串口通信的主要参数如下:
A、波特率:衡量通信速度的参数,表示每秒钟传送的bit的个数。例如9600波特表示每秒钟发送9600个bit。
B、数据位:衡量通信中实际数据位的参数,当计算机发送一个信息包,实际包含的有效数据位个数。
C、停止位:用于表示单个包的最后一位。典型的值为1和2位。
D、奇偶校验位:串口通信中一种检错方式。常用的检错方式有:偶、奇校验。
2、QtSerialPort模块简介
QtSerialPort模块是QT5中附加模块的一个模块,为硬件和虚拟的串口提供统一的接口。
串口由于其简单和可靠,目前在像嵌入式系统、机器人等工业中依旧用得很多。使用QtSerialPort模块,开发者可以大大缩短开发串口相关的应用程的周期。
Qt SerialPort提供了基本的功能,包括配置、I/O *** 作、获取和设置RS-232引脚的信号。
Qt SerialPort模块暂不支持以下特性:
A、终端的特性,例如回显,控制CR/LF等等
B、文本模式
C、读或写 *** 作的超时和延时配置
D、当RS-232引脚信号变化通知
#include <QtSerialPort/QtSerialPort>
要链接QtSerialPort模块,需要在pro文件中添加如下内容:
QT += serialport
二、QSerialPort
1、QSerialPort简介
QSerialPort提供了访问串口的接口函数。使用辅助类QSerialPortInfo可以获取可用的串口信息。将QSerialPortInfo辅助类对象做为参数,使用setPort()或setPortName()函数可以设置要访问的串口设备。
设置好端口后,可以使用open()函数以只读、只写或读写的模式打开使用。
注意,串口使用独占方式打开。
使用close()函数关闭串口并且取消IO *** 作。
串口成功打开后,QSerialPort会尝试确定串口的当前配置并初始化。可以使用setBaudRate()、setDataBits()、setParity()、setStopBits()和setFlowControl()函数重新配置端口设置。
有一对名为QSerialPort::dataTerminalReady、QSerialPort::requestToSend的属性
QSerialPort提供了中止正在调用线程直到信号触发的一系列函数。这些函数用于阻塞串口。
waitForReadyRead():阻塞调用,直到有新的数据可读
waitForBytesWritten():阻塞调用,直到数据以及写入串口
阻塞串口编程与非阻塞串口编程完全不同。阻塞串口不会要求时间循环并且通常会简化代码。然而,在GUI程序中,为了避免冻结用户界面,阻塞串口编程只能用于非GUI线程。
QSerialPort也能使用QTextStream和QDataStream的流 *** 作符。在试图使用流 *** 作符>>读时,需要确保有足够可用的数据。
2、QSerialPort成员函数
QSerialPort::QSerialPort(QObject parent = Q_NULLPTR)
QSerialPort::QSerialPort(const QString &name, QObject parent = Q_NULLPTR)
QSerialPort::QSerialPort(const QSerialPortInfo &serialPortInfo, QObject parent = Q_NULLPTR)
[virtual] bool QSerialPort::atEnd() const
[signal] void QSerialPort::baudRateChanged(qint32 baudRate, QSerialPort::Directions directions)
[virtual] qint64 QSerialPort::bytesAvailable() const
[virtual] qint64 QSerialPort::bytesToWrite() const
[virtual] void QSerialPort::close()
void QSerialPort::setPort(const QSerialPortInfo &serialPortInfo)
void QSerialPort::setPortName(const QString &name)
三、QSerialPortInfo
1、QSerialPortInfo简介
QSerialPortInfo类提供已有串口设备的信息。使用QSerialPortInfo类的静态成员函数生成QSerialPortInfo对象的链表。链表中的每个QSerialPortInfo对象代表一个串口,每个串口可以使用端口名、系统定位、描述、制造商查询。QSerialPortInfo类对象也可以用做QSerialPort类的setPort()成员函数的参数。
2、QSerialPortInfo成员函数
QSerialPortInfo::QSerialPortInfo(const QSerialPort &port)
QSerialPortInfo::QSerialPortInfo(const QString &name)
QSerialPortInfo::QSerialPortInfo(const QSerialPortInfo &other)
[static] QList<QSerialPortInfo> QSerialPortInfo::availablePorts()
QString QSerialPortInfo::description() const
bool QSerialPortInfo::hasProductIdentifier() const
bool QSerialPortInfo::hasVendorIdentifier() const
bool QSerialPortInfo::isBusy() const
QString QSerialPortInfo::manufacturer() const
QString QSerialPortInfo::portName() const
quint16 QSerialPortInfo::productIdentifier() const
QString QSerialPortInfo::serialNumber() const
[static] QList<qint32> QSerialPortInfo::standardBaudRates()
void QSerialPortInfo::swap(QSerialPortInfo &other)
QString QSerialPortInfo::systemLocation() const
quint16 QSerialPortInfo::vendorIdentifier() const
3、QSerialPortInfo显示串口信息实例
建议用Model和View
用QSqlQuery直接 *** 作数据库,执行数据库语句,将你QTableWidget中的数据替换到语句中对应的字段
例如:
QString(“%1,%2”)arg("wang",“25”)
可以实现你需要的功能
Qt中定时器用于间隔一段时间执行一次 *** 作,循环执行,直至定时器停止或被杀死。有两种使用方法, QObject类 与 QTimer类 。个人比较习惯使用QTimer类,信号与槽方式比较简洁灵活。
方法一:QObject中的定时器的使用,需要用到三个函数
1、int QObject::startTimer ( int interval ) ; //开启定时器,执行周期为interval毫秒。
此函数开启成功后,返回这个定时器的ID,并每隔interval毫秒时间进入一次timeEvent函数,直至定时器停止或被杀死。
2、void QObject::timeEvent ( QTimerEvent event ) ; //需重写timeEvent函数,相关 *** 作在此函数中执行。
当定时器超时后会进入此函数,可以通过event->timerId()来确定当前执行的定时器是哪个。
3、void QObject::killTimer ( int id ); //杀死目的id号的定时器,停止循环。
示例:
方法二:使用QTimer类(信号与槽)
1、创建QTimer类对象,
QTimer m_timer= new QTimer ( this ) ;
2、连接信号与槽,定时器超时后会发出 timeout () 信号,超时时执行槽函数 mySlot()
connect ( timer , SIGNAL ( timeout()) , this ,SLOT( mySlot()) );
3、开启定时器,周期为msec毫秒
timer->start( msec );
QFile类是一个 *** 作文件的输入/输出设备。 详情请见……
#include <qfileh>
继承了 QIODevice。
所有成员函数的列表。
公有成员
QFile ()
QFile ( const QString & name
)
~QFile ()
QString name () const
void setName (
const QString & name )
typedef QCString ( EncoderFn ) ( const QString & fileName )
typedef QString ( DecoderFn ) ( const QCString & localfileName )
bool exists () const
bool remove ()
virtual bool open ( int m )
bool open ( int m, FILE f )
bool open ( int m, int f )
virtual void close ()
virtual void flush ()
virtual Offset size () const
virtual Offset at () const
virtual bool at ( Offset pos )
virtual bool atEnd () const
virtual Q_LONG readBlock (
char p, Q_ULONG len )
virtual Q_LONG readLine ( char p,
Q_ULONG maxlen )
Q_LONG readLine (
QString & s, Q_ULONG maxlen )
virtual int getch ()
virtual int putch ( int ch )
virtual int ungetch ( int ch )
int handle () const
静态公有成员
QCString encodeName (
const QString & fileName )
QString decodeName (
const QCString & localFileName )
void setEncodingFunction
( EncoderFn f )
void setDecodingFunction
( DecoderFn f )
bool exists (
const QString & fileName )
bool remove (
const QString & fileName )
重要的继承成员
virtual QByteArray readAll
()
详细描述
QFile类是一个 *** 作文件的输入/输出设备。
QFile是用来读写二进制文件和文本文件的输入/输出设备。QFile可以自己单独被使用,但是如果和QDataStream或QTextStream一起使用将更加方便。
文件名通常可以通过构造函数来传递,但也可以使用setName()来设置。你可以通过exists()来检查一个文件是否存在并且可以通过remove()来移去一个文件。
文件可以用open()来打开、用close()来关闭、用flush()来刷新。数据通常可以使用QDataStream或者QTextStream进行读写,但你也可以使用readBlock()和readLine()来读,使用writeBlock()来写。QFile也支持getch()、 ungetch()和putch()。
size()可以返回文件的大小。你可以通过使用at()函数得到当前文件位置或者移到一个新的文件位置。如果你到了文件的末尾,atEnd()返回真。handle()返回文件句柄。
这里是一个使用QTextStream来一行一行地读取一个文本文件的代码段。它会把每一行带上一个行号打印出来。
QStringList lines;
QFile file( "filetxt" );
if ( fileopen( IO_ReadOnly ) ) {
QTextStream stream( &file );
QString line;
int n = 1;
while ( !streameof() ) {
line = streamreadLine(); // 不包括“\n”的一行文本
printf( "%3d: %s\n", n++, linelatin1() );
lines += line;
}
fileclose();
}
写文本也很容易(假设我们有一个行的字符串列表要写):
QFile file( "filetxt" );
if ( fileopen( IO_WriteOnly ) ) {
QTextStream stream( &file );
for ( QStringList::Iterator it = linesbegin(); it != linesend(); ++it )
stream << it << "\n";
fileclose();
}
QFileInfo类控制文件的详细信息,比如访问权限、文件日期和文件类型。
QDir类管理目录和文件名列表。
Qt使用Unicode文件名。如果你想在Unix系统上使用你自己的输入/输出,你可以使用encodeName()(和decodeName())来把文件名转换为本地编码。
也可以参考QDataStream、QTextStream和输入/输出和网络。
《MySQL:MySQL日期数据类型、MySQL时间类型使用总结》适用于 MySQL 5X 及以上版本。
一、MySQL 获得当前日期时间 函数
11 获得当前日期+时间(date + time)函数:now()
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2008-08-08 22:20:46 |
+---------------------+
除了 now() 函数能获得当前的日期时间外,MySQL 中还有下面的函数:
current_timestamp()
,current_timestamp
,localtime()
,localtime
,localtimestamp -- (v406)
,localtimestamp() -- (v406)
这些日期时间函数,都等同于 now()。鉴于 now() 函数简短易记,建议总是使用 now() 来替代上面列出的函数。
12 获得当前日期+时间(date + time)函数:sysdate()
sysdate() 日期时间函数跟 now() 类似,不同之处在于:now() 在执行开始时值就得到了, sysdate() 在函数执行时动态得到值。看下面的例子就明白了:
mysql> select now(), sleep(3), now();
+---------------------+----------+---------------------+
| now() | sleep(3) | now() |
+---------------------+----------+---------------------+
| 2008-08-08 22:28:21 | 0 | 2008-08-08 22:28:21 |
+---------------------+----------+---------------------+
mysql> select sysdate(), sleep(3), sysdate();
+---------------------+----------+---------------------+
| sysdate() | sleep(3) | sysdate() |
+---------------------+----------+---------------------+
| 2008-08-08 22:28:41 | 0 | 2008-08-08 22:28:44 |
+---------------------+----------+---------------------+
可以看到,虽然中途 sleep 3 秒,但 now() 函数两次的时间值是相同的; sysdate() 函数两次得到的时间值相差 3 秒。MySQL Manual 中是这样描述 sysdate() 的:Return the time at which the function executes。
sysdate() 日期时间函数,一般情况下很少用到。
2 获得当前日期(date)函数:curdate()
mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2008-08-08 |
+------------+
其中,下面的两个日期函数等同于 curdate():
current_date()
,current_date
3 获得当前时间(time)函数:curtime()
mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 22:41:30 |
+-----------+
其中,下面的两个时间函数等同于 curtime():
current_time()
,current_time
4 获得当前 UTC 日期时间函数:utc_date(), utc_time(), utc_timestamp()
mysql> select utc_timestamp(), utc_date(), utc_time(), now()
因为我国位于东八时区,所以本地时间 = UTC 时间 + 8 小时。UTC 时间在业务涉及多个国家和地区的时候,非常有用。
五、MySQL 时间戳(Timestamp)函数
1 MySQL 获得当前时间戳函数:current_timestamp, current_timestamp()
mysql> select current_timestamp, current_timestamp();
+---------------------+---------------------+
| current_timestamp | current_timestamp() |
+---------------------+---------------------+
| 2008-08-09 23:22:24 | 2008-08-09 23:22:24 |
+---------------------+---------------------+
2 MySQL (Unix 时间戳、日期)转换函数:
unix_timestamp(),
unix_timestamp(date),
from_unixtime(unix_timestamp),
from_unixtime(unix_timestamp,format)
下面是示例:
select unix_timestamp(); -- 1218290027 ===得到当前时间的UNIX时间值
将具体时间来转换成timestamp
select unix_timestamp('2008-08-08'); -- 1218124800
select unix_timestamp('2008-08-08 12:30:00'); -- 1218169800
3 MySQL 时间戳(timestamp)转换、增、减函数:
timestamp(date) -- date to timestamp
timestamp(dt,time) -- dt + time
timestampadd(unit,interval,datetime_expr) --
timestampdiff(unit,datetime_expr1,datetime_expr2) --
MySQL timestampdiff() 函数就比 datediff() 功能强多了,datediff() 只能计算两个日期(date)之间相差的天数。
====================================================================================
二、MySQL 日期时间 Extract(选取) 函数。
1 选取日期时间的各个部分:日期、时间、年、季度、月、日、小时、分钟、秒、微秒
set @dt = '2008-09-10 07:15:30123456';
select date(@dt); -- 2008-09-10
2 MySQL Extract() 函数,可以上面实现类似的功能:
set @dt = '2008-09-10 07:15:30123456';
select extract(year from @dt); -- 2008
MySQL Extract() 函数除了没有date(),time() 的功能外,其他功能一应具全。并且还具有选取‘day_microsecond' 等功能。注意这里不是只选取 day 和 microsecond,而是从日期的 day 部分一直选取到 microsecond 部分。够强悍的吧!
MySQL Extract() 函数唯一不好的地方在于:你需要多敲几次键盘。
3 MySQL dayof 函数:dayofweek(), dayofmonth(), dayofyear()
分别返回日期参数,在一周、一月、一年中的位置。
set @dt = '2008-08-08';
日期 '2008-08-08' 是一周中的第 6 天(1 = Sunday, 2 = Monday, , 7 = Saturday);一月中的第 8 天;一年中的第 221 天。
4 MySQL week 函数:week(), weekofyear(), dayofweek(), weekday(), yearweek()
MySQL week() 函数,可以有两个参数,具体可看手册。 weekofyear() 和 week() 一样,都是计算“某天”是位于一年中的第几周。 weekofyear(@dt) 等价于 week(@dt,3)。
MySQL weekday() 函数和 dayofweek() 类似,都是返回“某天”在一周中的位置。不同点在于参考的标准, weekday:(0 = Monday, 1 = Tuesday, , 6 = Sunday); dayofweek:(1 = Sunday, 2 = Monday, , 7 = Saturday)
MySQL yearweek() 函数,返回 year(2008) + week 位置(31)。
5 MySQL 返回星期和月份名称函数:dayname(), monthname()
思考,如何返回中文的名称呢?
6 MySQL last_day() 函数:返回月份中的最后一天。
MySQL last_day() 函数非常有用,比如我想得到当前月份中有多少天,可以这样来计算:
mysql> select now(), day(last_day(now())) as days;
三、MySQL 日期时间计算函数
1 MySQL 为日期增加一个时间间隔:date_add()
set @dt = now();
select date_add(@dt, interval 1 day); -- add 1 day
MySQL adddate(), addtime()函数,可以用 date_add() 来替代。下面是 date_add() 实现 addtime() 功能示例:
mysql> set @dt = '2008-08-09 12:12:33';
mysql>
mysql> select date_add(@dt, interval '01:15:30' hour_second);
mysql> select date_add(@dt, interval '1 01:15:30' day_second);
date_add() 函数,分别为 @dt 增加了“1小时 15分 30秒” 和 “1天 1小时 15分 30秒”。建议:总是使用 date_add() 日期时间函数来替代 adddate(), addtime()。
2 MySQL 为日期减去一个时间间隔:date_sub()
mysql> select date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second);
MySQL date_sub() 日期时间函数 和 date_add() 用法一致,不再赘述。另外,MySQL 中还有两个函数 subdate(), subtime(),建议,用 date_sub() 来替代。
3 MySQL 另类日期函数:period_add(P,N), period_diff(P1,P2)
函数参数“P” 的格式为“YYYYMM” 或者 “YYMM”,第二个参数“N” 表示增加或减去 N month(月)。
MySQL period_add(P,N):日期加/减去N月。
mysql> select period_add(200808,2), period_add(20080808,-2)
MySQL period_diff(P1,P2):日期 P1-P2,返回 N 个月。
mysql> select period_diff(200808, 200801);
在 MySQL 中,这两个日期函数,一般情况下很少用到。
4 MySQL 日期、时间相减函数:datediff(date1,date2), timediff(time1,time2)
MySQL datediff(date1,date2):两个日期相减 date1 - date2,返回天数。
select datediff('2008-08-08', '2008-08-01'); -- 7
select datediff('2008-08-01', '2008-08-08'); -- -7
MySQL timediff(time1,time2):两个日期相减 time1 - time2,返回 time 差值。
select timediff('2008-08-08 08:08:08', '2008-08-08 00:00:00'); -- 08:08:08
select timediff('08:08:08', '00:00:00'); -- 08:08:08
注意:timediff(time1,time2) 函数的两个参数类型必须相同。
四、MySQL 日期转换函数、时间转换函数
1 MySQL (时间、秒)转换函数:time_to_sec(time), sec_to_time(seconds)
select time_to_sec('01:00:05'); -- 3605
select sec_to_time(3605); -- '01:00:05'
2 MySQL (日期、天数)转换函数:to_days(date), from_days(days)
select to_days('0000-00-00'); -- 0
3 MySQL Str to Date (字符串转换为日期)函数:str_to_date(str, format)
select str_to_date('08/09/2008', '%m/%d/%Y'); -- 2008-08-09
可以看到,str_to_date(str,format) 转换函数,可以把一些杂乱无章的字符串转换为日期格式。另外,它也可以转换为时间。“format” 可以参看 MySQL 手册。
4 MySQL Date/Time to Str(日期/时间转换为字符串)函数:date_format(date,format), time_format(time,format)
mysql> select date_format('2008-08-08 22:23:00', '%W %M %Y');
MySQL 日期、时间转换函数:date_format(date,format), time_format(time,format) 能够把一个日期/时间转换成各种各样的字符串格式。它是 str_to_date(str,format) 函数的 一个逆转换。
5 MySQL 获得国家地区时间格式函数:get_format()
MySQL get_format() 语法:
get_format(date|time|datetime, 'eur'|'usa'|'jis'|'iso'|'internal'
MySQL get_format() 函数在实际中用到机会的比较少。
6 MySQL 拼凑日期、时间函数:makdedate(year,dayofyear), maketime(hour,minute,second)
QTime time = QTime::currentTime(); //获取当前时间
qsrand(timemsec()+timesecond()100000);
而由于程序执行速度很快,可能会导致循环中两次获取的time是一样的,这样的话产生随机数的种子一样,导致生成的随机数一样。
可以使用:
qsrand(time(0));来生成不同的种子。
以上就是关于QT开发(五十)——QT串口编程基础全部的内容,包括:QT开发(五十)——QT串口编程基础、求帮忙解决,基于qt下,QListWidget里怎样显示来自数据库的信息,当前桌,人员,时间…、Qt中定时器的两种使用方法(QObject类与QTimer类)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)