├── .gitignore ├── LICENSE ├── README.md ├── inputdialog.cpp ├── inputdialog.h ├── inputdialog.ui ├── main.cpp ├── mainwindow.cpp ├── mainwindow.hpp ├── mainwindow.ui ├── modifydialog.cpp ├── modifydialog.h ├── modifydialog.ui ├── qcustomplot.cpp ├── qcustomplot.h ├── qextserialbase.cpp ├── qextserialbase.h ├── qextserialport.cpp ├── qextserialport.h ├── qt_usb_video.pro ├── scenewindow.cpp ├── scenewindow.hpp ├── updatedialog.cpp ├── updatedialog.h ├── updatedialog.ui ├── win_qextserialport.cpp └── win_qextserialport.h /.gitignore: -------------------------------------------------------------------------------- 1 | bulkloop/ 2 | freeglut/ 3 | opencv2/ 4 | shader/ 5 | *.dll 6 | *.[l|L]ib 7 | *.pro.user -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Chenghe Hu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 激光血液图像处理系统上位机软件设计 2 | 3 | 本项目为南京航空航天大学大学生创新实践工程创新训练计划项目。 4 | 5 | 6 | 7 | ### 功能说明 8 | 9 | `inputdialog`:输入患者信息 10 | 11 | `mainwindow`:程序主界面 12 | 13 | `modifydialog`:编辑患者信息 14 | 15 | `qcustomplot`:用于RGB均值的实时动态曲线绘制 16 | 17 | `qextserialbase`, `qextserialport`, `win_qextserialport`:用于串口通信 18 | 19 | `scenewindow`:openGL相关图形显示 20 | 21 | `updatedialog`:修改患者信息 22 | 23 | 24 | 25 | 26 | ### Dependence 27 | 28 | Qt 5.12.* 29 | 30 | OpenCV 4.0.1 31 | 32 | Sqlite 3 33 | 34 | 35 | 36 | ### LICENSE 37 | 38 | MIT 39 | -------------------------------------------------------------------------------- /inputdialog.cpp: -------------------------------------------------------------------------------- 1 | #include "inputdialog.h" 2 | #include "ui_inputdialog.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | QString addName,addAge,addID; 9 | int addSex; 10 | 11 | inputDialog::inputDialog(QWidget *parent) : 12 | QDialog(parent), 13 | ui(new Ui::inputDialog) 14 | { 15 | ui->setupUi(this); 16 | setWindowTitle("input patient info"); 17 | ui->buttonGroup->setId(ui->radioButton, 0); 18 | ui->buttonGroup->setId(ui->radioButton_2, 1); 19 | ui->radioButton->setChecked(true); 20 | } 21 | 22 | inputDialog::~inputDialog() 23 | { 24 | delete ui; 25 | } 26 | 27 | void inputDialog::on_pushButton_2_clicked() 28 | { 29 | // cancel button 30 | emit sendsignal(); 31 | this->close(); 32 | } 33 | 34 | void inputDialog::on_pushButton_clicked() 35 | { 36 | QSqlQuery sql_query; 37 | if(!sql_query.exec("create table qtusb(name text, sex numeric, age int, id text primary key)")) 38 | { 39 | qDebug() << "Error: Fail to create table."<< sql_query.lastError(); 40 | } 41 | else 42 | { 43 | qDebug() << "Table created!"; 44 | } 45 | 46 | addSex = ui->buttonGroup->checkedId(); 47 | 48 | // insert data 49 | QString insert_sql = "insert into qtusb values (?, ?, ?, ?)"; 50 | sql_query.prepare(insert_sql); 51 | addName = ui->textEdit->toPlainText(); 52 | sql_query.addBindValue(addName); // insert name 53 | sql_query.addBindValue(addSex); // insert sex, male: 0, female: 1 54 | addAge = ui->spinBox->text(); 55 | sql_query.addBindValue(addAge.toInt()); // insert age 56 | addID = ui->textEdit_3->toPlainText(); 57 | sql_query.addBindValue(addID); // insert id 58 | 59 | if(!sql_query.exec()) 60 | { 61 | qDebug() << sql_query.lastError(); 62 | } 63 | else 64 | { 65 | qDebug() << "inserted!"; 66 | } 67 | 68 | // ok button 69 | emit sendsignal(); 70 | this->close(); 71 | } 72 | -------------------------------------------------------------------------------- /inputdialog.h: -------------------------------------------------------------------------------- 1 | #ifndef INPUTDIALOG_H 2 | #define INPUTDIALOG_H 3 | 4 | #include 5 | 6 | namespace Ui { 7 | class inputDialog; 8 | } 9 | 10 | class inputDialog : public QDialog 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | explicit inputDialog(QWidget *parent = nullptr); 16 | ~inputDialog(); 17 | 18 | private slots: 19 | void on_pushButton_2_clicked(); 20 | 21 | void on_pushButton_clicked(); 22 | 23 | signals: 24 | void sendsignal(); 25 | 26 | private: 27 | Ui::inputDialog *ui; 28 | }; 29 | 30 | #endif // INPUTDIALOG_H 31 | -------------------------------------------------------------------------------- /inputdialog.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | inputDialog 4 | 5 | 6 | 7 | 0 8 | 0 9 | 400 10 | 300 11 | 12 | 13 | 14 | Dialog 15 | 16 | 17 | 18 | 19 | 70 20 | 30 21 | 71 22 | 31 23 | 24 | 25 | 26 | 姓名 27 | 28 | 29 | Qt::AlignCenter 30 | 31 | 32 | 33 | 34 | 35 | 70 36 | 80 37 | 71 38 | 31 39 | 40 | 41 | 42 | 性别 43 | 44 | 45 | Qt::AlignCenter 46 | 47 | 48 | 49 | 50 | 51 | 70 52 | 190 53 | 71 54 | 31 55 | 56 | 57 | 58 | 身份证号 59 | 60 | 61 | Qt::AlignCenter 62 | 63 | 64 | 65 | 66 | 67 | 70 68 | 140 69 | 71 70 | 31 71 | 72 | 73 | 74 | 年龄 75 | 76 | 77 | Qt::AlignCenter 78 | 79 | 80 | 81 | 82 | 83 | 210 84 | 30 85 | 104 86 | 31 87 | 88 | 89 | 90 | 91 | 等线 92 | 12 93 | 94 | 95 | 96 | IBeamCursor 97 | 98 | 99 | 100 | 101 | 102 | 193 103 | 190 104 | 141 105 | 31 106 | 107 | 108 | 109 | 110 | Adobe Arabic 111 | 14 112 | 113 | 114 | 115 | 116 | 117 | 118 | 200 119 | 90 120 | 41 121 | 16 122 | 123 | 124 | 125 | 126 | 127 | 128 | buttonGroup 129 | 130 | 131 | 132 | 133 | 134 | 290 135 | 90 136 | 41 137 | 16 138 | 139 | 140 | 141 | 142 | 143 | 144 | buttonGroup 145 | 146 | 147 | 148 | 149 | 150 | 260 151 | 250 152 | 75 153 | 23 154 | 155 | 156 | 157 | 确定 158 | 159 | 160 | 161 | 162 | 163 | 100 164 | 250 165 | 75 166 | 23 167 | 168 | 169 | 170 | 返回 171 | 172 | 173 | 174 | 175 | 176 | 240 177 | 150 178 | 42 179 | 22 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.hpp" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE"); 8 | 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | QApplication a(argc, argv); 13 | QApplication::addLibraryPath("./plugins"); 14 | 15 | // build and access to database 16 | 17 | database.setDatabaseName("./qtusb.db"); 18 | if (!database.open()) 19 | { 20 | qDebug() << "Error: Failed to connect database." << database.lastError(); 21 | } 22 | else 23 | { 24 | qDebug() << "Succeed to connect database." ; 25 | } 26 | 27 | MainWindow w; 28 | w.setWindowTitle("LPI诊断系统"); 29 | w.show(); 30 | qDebug()<<"debug"; 31 | return a.exec(); 32 | } 33 | -------------------------------------------------------------------------------- /mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.hpp" 2 | #include "ui_mainwindow.h" 3 | #include "inputdialog.h" 4 | 5 | #include 6 | 7 | #include 8 | // #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "bulkloop/inc/CyAPI.h" 14 | #include "bulkloop/inc/cyioctl.h" 15 | #include "bulkloop/inc/CyUSB30_def.h" 16 | #include "bulkloop/inc/UsbdStatus.h" 17 | #include "bulkloop/inc/usb200.h" 18 | #include "bulkloop/inc/VersionNo.h" 19 | 20 | using namespace cv; 21 | 22 | static const int g_nKwidthTrackBarMaxValue = 9; 23 | static const int g_nKheightTrackBarMaxValue = 9; 24 | static const int g_nsigmaXTrackBarMaxValue = 5; 25 | static const int g_nsigmaYTrackBarMaxvalue = 5; 26 | 27 | //定义每个轨迹条的初始值 28 | static int g_nKwidthTrackBarValue = 1; 29 | static int g_nKheightTrackBarValue = 1; 30 | static int g_nsigmaXTrackBarValue = 1; 31 | static int g_nsigmaYTrackBarValue = 1; 32 | 33 | //--高斯核的宽度和高度 34 | static int g_kernelWidthValue; 35 | static int g_kernelHeightValue; 36 | 37 | //--高斯模糊的输入和输出图像 38 | static Mat srcImage; 39 | static Mat dstImage; 40 | 41 | //播放视频时的帧数 42 | static int framenumber; 43 | 44 | //绘制点的最大个数 45 | static int MAX_NUM = 50; 46 | static int plotSize = 0; 47 | 48 | //外部函数声明 49 | void on_GaussianBlurTrackbar(int, void*); 50 | QImage Mat2QImage(const Mat &inputMat); 51 | 52 | /*MainWindow::MainWindow(QWidget *parent) : 53 | QMainWindow(parent), 54 | ui(new Ui::MainWindow) 55 | { 56 | ui->setupUi(this); 57 | 58 | VideoCapture cam(1); 59 | if (!cam.isOpened()) exit(0); 60 | namedWindow("摄像头"); 61 | Mat rgb; 62 | QImage img; 63 | 64 | 65 | while (true) 66 | { 67 | cam >> input_image; 68 | 69 | namedWindow("原图像", WINDOW_NORMAL); //定义窗口显示属性 70 | imshow("原图像", input_image); 71 | 72 | namedWindow("高斯滤波图像", WINDOW_NORMAL); 73 | 74 | //定义每个轨迹条名字 75 | char widthTrackBarName[20]; 76 | sprintf(widthTrackBarName, "核函数width %d", g_nKwidthTrackBarMaxValue); 77 | 78 | char heightTrackBarName[20]; 79 | sprintf(heightTrackBarName, "核函数height %d", g_nKheightTrackBarMaxValue); 80 | 81 | char sigmaXTrackBarName[20]; 82 | sprintf(sigmaXTrackBarName, "核函数sigmaX %d", g_nsigmaXTrackBarMaxValue); 83 | 84 | char sigmaYTrackBarName[20]; 85 | sprintf(sigmaYTrackBarName, "核函数sigmaY %d", g_nsigmaYTrackBarMaxvalue); 86 | 87 | //创建轨迹条 88 | createTrackbar(widthTrackBarName, "高斯滤波图像", &g_nKwidthTrackBarValue, 89 | g_nKwidthTrackBarMaxValue, on_GaussianBlurTrackbar); 90 | on_GaussianBlurTrackbar(g_nKwidthTrackBarValue, 0); 91 | 92 | createTrackbar(heightTrackBarName, "高斯滤波图像", &g_nKheightTrackBarValue, 93 | g_nKheightTrackBarMaxValue, on_GaussianBlurTrackbar); 94 | on_GaussianBlurTrackbar(g_nKheightTrackBarValue, 0); 95 | 96 | createTrackbar(sigmaXTrackBarName, "高斯滤波图像", &g_nsigmaXTrackBarValue, 97 | g_nsigmaXTrackBarMaxValue, on_GaussianBlurTrackbar); 98 | on_GaussianBlurTrackbar(g_nsigmaXTrackBarValue, 0); 99 | 100 | createTrackbar(sigmaYTrackBarName, "高斯滤波图像", &g_nsigmaYTrackBarValue, 101 | g_nsigmaYTrackBarMaxvalue, on_GaussianBlurTrackbar); 102 | on_GaussianBlurTrackbar(g_nsigmaYTrackBarValue, 0); 103 | 104 | //显示效果图 105 | imshow("高斯滤波[效果图]", dstImage); 106 | 107 | //imshow("input image", input_image); 108 | if (27 == waitKey(30)) 109 | break; 110 | } 111 | } 112 | 113 | MainWindow::~MainWindow() 114 | { 115 | delete ui; 116 | } 117 | */ 118 | 119 | // 添加患者信息 120 | extern QString addName,addAge,addID; 121 | extern int addSex; 122 | 123 | MainWindow::MainWindow(QWidget *parent) : 124 | QMainWindow(parent), 125 | ui(new Ui::MainWindow) 126 | { 127 | ui->setupUi(this); 128 | 129 | pointNumber = 5; //设置能够选择的像素点数目 130 | ui->label->installEventFilter(this);//为label安装事件过滤器 131 | 132 | initRealtimePlot(); //QCustomPlot画图初始化 133 | 134 | /////读取串口数据 135 | struct PortSettings myComSetting = {BAUD9600,DATA_8,PAR_NONE,STOP_1,FLOW_OFF,500}; 136 | //定义一个结构体,用来存放串口各个参数 137 | myCom = new Win_QextSerialPort("com1",myComSetting,QextSerialBase::Polling); 138 | //定义串口对象,并传递参数,在构造函数里对其进行初始化 139 | //定义串口对象,指定串口名和查询模式,这里使用Polling 140 | myCom ->open(QIODevice::ReadWrite); 141 | //以可读写方式打开串口 142 | timer=new QTimer(this); 143 | myCom->setTimeout(10); 144 | timer->start(200); 145 | //设置延时为200ms 146 | connect(timer, SIGNAL(timeout()), this, SLOT(readMyCom()));//Polling模式定时器触发timeout()信号 147 | 148 | ///// USB3 149 | CCyFX3Device *m_usbDevice = new CCyFX3Device(); 150 | FX3_FWDWNLOAD_ERROR_CODE dwld_status = FAILED; 151 | // CCyUSBEndPoint类主要负责对USB端点进行操作,包含获取和设置USB端点的各种描述符和进行数据传输等; 152 | // CCyBulkEndPoint类主要负责完成USB块传输操作,包含对USB块传输端点的读写操作函数; 153 | CCyBulkEndPoint *BulkInEpt = nullptr; 154 | CCyBulkEndPoint *BulkOutEpt = nullptr; 155 | // Open first USB device 156 | // Open 函数,用于打开USB设备 157 | // Close 函数,关闭主机设备和USB驱动的接口 158 | if(m_usbDevice->Open(0)) 159 | { 160 | //Check if boot loader is running. 161 | int status = m_usbDevice->IsBootLoaderRunning(); 162 | if(status) 163 | { 164 | // Download the file C:\Bulkloop.img file to FX3 device RAM 165 | char tm[20]="C:\\Bulkloop.img"; 166 | int dwld_status = m_usbDevice->DownloadFw(tm,RAM); 167 | } 168 | } 169 | 170 | //从EndPoints数组中找出块传输端点(由官方例子改编而来) 171 | int eptCount = m_usbDevice->EndPointCount();//当前设备的接口数+1 172 | for (int i=1; iEndPoints[i]->Address & 0x80) == 0x80); 175 | //成员变量Address的高位是0x8_,表示USB IN端点,IN表示主机接受USB功能设备发来的数据 176 | //高位是0x0_,表示USB OUT端点 177 | bool bIn = m_usbDevice->EndPoints[i]->bIn; 178 | bool bBulk = (m_usbDevice->EndPoints[i]->Attributes == 2); 179 | // Attributes表示端点的类型 180 | // 0 控制端点 181 | // 1 同步端点 182 | // 2 块端点 183 | // 3 中断端点 184 | // 对应USB的四种传输方式 185 | // 这里的例子分别指定为USB IN端点和USB OUT端点,均为块端点 186 | // 赋值 187 | if (bBulk && bIn) 188 | BulkInEpt = (CCyBulkEndPoint *) m_usbDevice->EndPoints[i]; 189 | if (bBulk && !bIn) 190 | BulkOutEpt = (CCyBulkEndPoint *) m_usbDevice->EndPoints[i]; 191 | } 192 | 193 | 194 | /*if (USBDevice->bHighSpeed) 195 | { 196 | // int i++;// Do something 197 | }*/ 198 | 199 | /* 200 | /// Official Example: 块传输(异步传输)实例 201 | // This example assumes that the device automatically sends back, 202 | // over its bulk-IN endpoint, any bytes that were received over its 203 | // bulk-OUT endpoint (commonly referred to as a loopback function) 204 | CCyUSBDevice *USBDevice = new CCyUSBDevice(nullptr); 205 | OVERLAPPED outOvLap, inOvLap; 206 | outOvLap.hEvent = CreateEvent(nullptr, false, false, L"CYUSB_OUT"); 207 | inOvLap.hEvent = CreateEvent(nullptr, false, false, L"CYUSB_IN"); 208 | 209 | unsigned char inBuf[128];//数据缓存区 210 | ZeroMemory(inBuf, 128); //申请内存,下同 211 | unsigned char buffer[128]; 212 | LONG length = 128; 213 | 214 | // 初始化输入输出 215 | UCHAR *outContext = USBDevice->BulkOutEndPt->BeginDataXfer(buffer, length, &outOvLap); 216 | UCHAR *inContext = USBDevice->BulkInEndPt->BeginDataXfer(inBuf, length, &inOvLap); 217 | //BulkInEndPt 指向第一个块输入端点数据的指针 218 | //BulkOutEndPt 指向第一个块输出端点数据的指针 219 | 220 | // 等待数据介绍 221 | USBDevice->BulkOutEndPt->WaitForXfer(&outOvLap,100); //等待时长:100ms 222 | USBDevice->BulkInEndPt->WaitForXfer(&inOvLap,100); 223 | 224 | // 完成传输 225 | USBDevice->BulkOutEndPt->FinishDataXfer(buffer, length, &outOvLap, outContext); 226 | USBDevice->BulkInEndPt->FinishDataXfer(inBuf, length, &inOvLap, inContext); 227 | 228 | // 关闭输入输出 229 | CloseHandle(outOvLap.hEvent); 230 | CloseHandle(inOvLap.hEvent); 231 | */ 232 | } 233 | 234 | 235 | 236 | MainWindow::~MainWindow() 237 | { 238 | delete ui; 239 | } 240 | 241 | void MainWindow::initRealtimePlot() 242 | { 243 | ui->customplot->xAxis->setLabel("framenumber"); 244 | ui->customplot->yAxis->setLabel("RGB"); 245 | //设置y轴范围 246 | ui->customplot->yAxis->setRange(0, 255); 247 | 248 | ui->customplot->addGraph(); // red line 249 | ui->customplot->graph(0)->setPen(QPen(Qt::red)); 250 | ui->customplot->addGraph(); // green line 251 | ui->customplot->graph(1)->setPen(QPen(Qt::green)); 252 | ui->customplot->addGraph(); // blue line 253 | ui->customplot->graph(2)->setPen(QPen(Qt::blue)); 254 | } 255 | 256 | void MainWindow::addData(double r, double g, double b, double x) 257 | { 258 | plotSize++; 259 | if(plotSize == MAX_NUM) 260 | { 261 | xVector.pop_front(); 262 | rValue.pop_front(); 263 | gValue.pop_front(); 264 | bValue.pop_front(); 265 | plotSize--; 266 | } 267 | xVector.push_back(x); 268 | rValue.push_back(r); 269 | gValue.push_back(g); 270 | bValue.push_back(b); 271 | } 272 | 273 | void on_GaussianBlurTrackbar(int, void*) 274 | { 275 | //根据输入的width和height重新计算ksize.width和ksize.height 276 | g_kernelWidthValue = g_nKwidthTrackBarValue * 2 + 1; 277 | g_kernelHeightValue = g_nKheightTrackBarValue * 2 + 1; 278 | 279 | GaussianBlur(srcImage, dstImage, Size(g_kernelWidthValue, g_kernelHeightValue), 280 | g_nsigmaXTrackBarValue, g_nsigmaYTrackBarValue); 281 | 282 | //imshow("GaussianBlur Image", dstImage); 283 | } 284 | 285 | QImage Mat2QImage(const Mat &inputMat) 286 | { 287 | Mat tmpMat; 288 | QImage qImg; 289 | if(inputMat.channels() == 1) //灰度图像 290 | { 291 | tmpMat = inputMat; 292 | qImg = QImage((const uchar*)tmpMat.data, //指向所用图像数据块的指针 293 | tmpMat.cols, tmpMat.rows, 294 | tmpMat.cols*tmpMat.channels(), //bytesPerLine,每行的字节数 295 | QImage::Format_Indexed8); //利用8bits存储灰度值 296 | } 297 | else 298 | { //Mat是BGR排列, QImage是RGB排列,利用cvtColor变换颜色空间 299 | cvtColor(inputMat, tmpMat, CV_BGR2RGB); 300 | qImg = QImage((const uchar*)tmpMat.data, //指向所用图像数据块的指针 301 | tmpMat.cols, tmpMat.rows, 302 | tmpMat.cols*tmpMat.channels(), //bytesPerLine,每行的字节数 303 | QImage::Format_RGB888); //利用24bits真彩色存储RGB值 304 | } 305 | return qImg.copy();//deep copy 306 | } 307 | 308 | void MainWindow::nextFrame() 309 | { 310 | Mat dst; 311 | int size; 312 | int posx, posy; 313 | QColor color; 314 | QString text = ""; 315 | double r,g,b; //摄取RGB值 316 | double s1=0, s2=0, s3=0;//对RGB值分别求和 317 | 318 | capture >> frame; 319 | if (!frame.empty()) 320 | { 321 | //---framenumber更新 322 | framenumber++; 323 | ui->label_3->setText(tr("System is running!\nThe number of current frame is\n%1.").arg(framenumber, 5)); 324 | 325 | //---label_2显示高斯模糊处理后的图像 326 | GaussianBlur(frame, dst, Size(5,5), 0, 0); 327 | image = Mat2QImage(dst); 328 | ui->label_2->setPixmap(QPixmap::fromImage(image)); 329 | 330 | //---如果含有标记像素点,则更改frame,显示标记点及其RGB值 331 | //---否则,frame不变,显示原图像 332 | size = Points.size(); 333 | if(size > 0) 334 | { 335 | image = Mat2QImage(frame); 336 | for(int i=0 ; i < size; i++) 337 | { 338 | //获取像素点坐标 339 | posx = Points[i].x(); 340 | posy = Points[i].y(); 341 | 342 | //提取像素RGB值 343 | color = image.pixel(Points[i]); 344 | r = color.red(); 345 | g = color.green(); 346 | b = color.blue(); 347 | s1 += r; 348 | s2 += g; 349 | s3 += b; 350 | 351 | text += QString("\nX: %1 Y: %2 RGB: %3, %4, %5").arg(posx, 3).arg(posy, 3).arg(r, 3).arg(g, 3).arg(b, 3); 352 | //, 3表示占据的宽度 353 | 354 | //标记坐标点(红色) 355 | circle(frame, Point(posx, posy), 3, Scalar(0,0,255), -1); 356 | //3表示半径,Scalar(B,G,R),-1表填充 357 | } 358 | //设置显示文本 359 | ui->label_16->setText(text); 360 | 361 | //添加图像数据点 362 | addData(s1/size, s2/size, s3/size, framenumber); 363 | ui->customplot->graph(0)->setData(xVector, rValue); 364 | ui->customplot->graph(1)->setData(xVector, gValue); 365 | ui->customplot->graph(2)->setData(xVector, bValue); 366 | //设置x轴范围 367 | ui->customplot->xAxis->setRange(framenumber-MAX_NUM*(1.05), framenumber+MAX_NUM*(0.05)); 368 | ui->customplot->replot(); 369 | } 370 | 371 | //---label显示frame 372 | image = Mat2QImage(frame); 373 | ui->label->setPixmap(QPixmap::fromImage(image)); 374 | } 375 | } 376 | 377 | void MainWindow::readMyCom()//读串口函数 378 | { 379 | QByteArray temp = myCom->readAll(); //读取串口缓冲区的所有数据给临时变量temp 380 | ui->textBrowser->insertPlainText(temp);//将串口的数据显示在窗口的文本浏览器中 381 | } 382 | 383 | bool MainWindow::eventFilter(QObject *watched, QEvent *event) 384 | { 385 | if(watched == ui->label) 386 | { 387 | if(event->type() == QEvent::MouseButtonPress) 388 | { 389 | if(Points.size() < pointNumber && capture.isOpened()) 390 | { 391 | //转化为鼠标事件 392 | QMouseEvent *mouseEvent = static_cast(event); 393 | //将事件的全局坐标转化成相对与label部件的坐标 394 | QPoint temp = ui->label->mapFromGlobal(mouseEvent->globalPos()); 395 | //qDebug()<<"x:"<> frame;//frame:Mat类型 421 | if (!frame.empty()) 422 | { 423 | framenumber = 1; 424 | image = Mat2QImage(frame); //函数定义见上方 425 | ui->label->setPixmap(QPixmap::fromImage(image));//实际的彩色图像 426 | timer = new QTimer(this); //设置定时器 427 | timer->setInterval(int(1000/rate)); //设置与FPS相匹配的时间间隔(毫秒) 428 | connect(timer, SIGNAL(timeout()), this, SLOT(nextFrame()));//nextFrame()定义见上部 429 | timer->start(); 430 | } 431 | } 432 | } 433 | 434 | void MainWindow::on_pushButton_2_clicked() 435 | { 436 | if (capture.isOpened()) //解读同on_pushButton_1_clicked() 437 | capture.release(); 438 | 439 | //设置相机参数 440 | //capture.set(CAP_PROP_FRAME_WIDTH, 1920); //宽度 441 | //capture.set(CAP_PROP_FRAME_HEIGHT, 1200);//高度 442 | //capture.set(CAP_PROP_FRAME_COUNT, 160); //帧数 443 | //打开默认相机 444 | capture.open(0); 445 | //图像参数设置 446 | //capture.set(CAP_PROP_BRIGHTNESS, 1); //亮度 447 | //capture.set(CAP_PROP_CONTRAST,40); //对比度 40 448 | //capture.set(CAP_PROP_SATURATION, 50);//饱和度 50 449 | //capture.set(CAP_PROP_HUE, 50); //色调 50 450 | //capture.set(CAP_PROP_EXPOSURE, 50); //曝光 50 451 | 452 | if (capture.isOpened()) 453 | { 454 | rate = capture.get(CAP_PROP_FPS); 455 | //rate = capture.get(5); 456 | capture >> frame; 457 | if (!frame.empty()) 458 | { 459 | framenumber = 1; 460 | image = Mat2QImage(frame); 461 | ui->label->setPixmap(QPixmap::fromImage(image)); 462 | timer = new QTimer(this); 463 | timer->setInterval(int(1000/rate)); 464 | connect(timer, SIGNAL(timeout()), this, SLOT(nextFrame())); 465 | timer->start(); 466 | } 467 | } 468 | else 469 | { 470 | QMessageBox::warning(nullptr, "warning", "camera is not open!", QMessageBox::Yes, QMessageBox::Yes); 471 | //Mat cannyImg ; 472 | //Canny(frame, cannyImg, 0, 30, 3); 473 | //namedWindow("Canny"); 474 | //imshow("camera is not open!", cannyImg); 475 | } 476 | } 477 | void MainWindow::on_pushButton_3_clicked() 478 | { 479 | Mat cannyImg; 480 | Canny(frame, cannyImg, 0, 30, 3);//(src, dst, threshold1, threshold2, aperture size for the Sobel operator) 481 | namedWindow("Canny"); //创建窗口并显示图像 482 | imshow("Canny", cannyImg); 483 | } 484 | 485 | void MainWindow::on_pushButton_4_clicked() 486 | { 487 | Mat src; 488 | if (!capture.isOpened()) 489 | qDebug()<<"ERROR! Unable to open camera.\n"; 490 | capture >> src; 491 | if (src.empty()) 492 | qDebug()<<"ERROR! Blank frame grabbed.\n"; 493 | 494 | //---初始化VideoWriter 495 | int codec = VideoWriter::fourcc('P','I','M','1'); 496 | //视频播放软件通过查询FourCC代码并且寻找与FourCC代码相关联的视频解码器来播放特定的视频流 497 | double fps = capture.get(CAP_PROP_FPS); 498 | bool isColor = (src.type() == CV_8UC3);//检查是否为彩色图像 499 | writer.open("./myRecord.avi", codec, fps, src.size(), isColor); 500 | if (!writer.isOpened()) 501 | qDebug()<<"Could not open the output video file for write.\n"; 502 | 503 | //---录制一个视频 504 | int t=20; //时长(seconds) 505 | int num=int(t*fps); //帧数,帧数越多,视频也就越长 506 | while(num--) 507 | { 508 | if (!capture.read(src))//读取新的图像 509 | { 510 | qDebug()<<"ERROR! Blank frame grabbed.\n"; 511 | break; 512 | } 513 | writer.write(src); //并写入文件 514 | } 515 | 516 | //---保存视频之后,取消该键,按钮变为灰色 517 | ui->pushButton_4->setDisabled(true); 518 | } 519 | 520 | void MainWindow::on_pushButton_5_clicked() 521 | { 522 | writer.release(); 523 | } 524 | /////////////////////////com 读串口函数 525 | 526 | void MainWindow::on_openButton_clicked() 527 | {} 528 | 529 | void MainWindow::on_closeButton_clicked(){} 530 | 531 | void MainWindow::on_sendMsgButton_clicked(){} 532 | 533 | void MainWindow::on_action_3_triggered() 534 | { 535 | dialog = new inputDialog; 536 | 537 | dialog->setModal(true); 538 | dialog->show(); 539 | } 540 | 541 | void MainWindow::on_action_4_triggered() 542 | { 543 | mddialog = new modifyDialog; 544 | 545 | mddialog->setModal(true); 546 | mddialog->show(); 547 | 548 | } 549 | -------------------------------------------------------------------------------- /mainwindow.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_HPP 2 | #define MAINWINDOW_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "opencv2/core/core.hpp" 16 | #include "opencv2/imgproc/imgproc.hpp" 17 | #include "opencv2/highgui/highgui.hpp" 18 | #include "opencv2/video/tracking.hpp" 19 | #include "opencv2/features2d/features2d.hpp" 20 | #include "opencv2/flann/flann.hpp" 21 | #include "opencv2/calib3d/calib3d.hpp" 22 | #include "opencv2/core/core_c.h" 23 | #include "opencv2/imgproc/imgproc_c.h" 24 | #include "opencv2/highgui/highgui_c.h" 25 | 26 | #include "win_qextserialport.h" 27 | 28 | #include 29 | #include 30 | 31 | //#include 32 | //#include 33 | //#include 34 | 35 | #include "inputdialog.h" 36 | #include "modifydialog.h" 37 | #include "qcustomplot.h" 38 | 39 | namespace Ui { 40 | class MainWindow; 41 | } 42 | 43 | class MainWindow : public QMainWindow 44 | { 45 | Q_OBJECT 46 | 47 | 48 | public: 49 | explicit MainWindow(QWidget *parent = nullptr); 50 | ~MainWindow(); 51 | 52 | //QCustomPlot画图初始化 53 | void initRealtimePlot(); 54 | //添加数据点 55 | void addData(double r, double g, double b, double x); 56 | 57 | protected: 58 | bool eventFilter(QObject *watched, QEvent *event); 59 | 60 | private: 61 | 62 | Ui::MainWindow *ui; 63 | 64 | inputDialog *dialog; 65 | modifyDialog *mddialog; 66 | 67 | cv::Mat frame; 68 | cv::VideoCapture capture; 69 | cv::VideoWriter writer; //make a video re 70 | 71 | QImage image; 72 | QTimer *timer; 73 | double rate; //FPS 74 | 75 | Win_QextSerialPort *myCom; 76 | 77 | QVector Points; //存放鼠标点击位置相对于QLabel的坐标 78 | int pointNumber; //能够选择的像素点数目(<=10) 79 | 80 | QVector xVector; //framenubmer 81 | QVector rValue; //RGB色彩的均值 82 | QVector gValue; 83 | QVector bValue; 84 | 85 | private slots: 86 | void nextFrame(); 87 | void readMyCom(); 88 | 89 | void on_pushButton_1_clicked(); 90 | void on_pushButton_2_clicked(); 91 | void on_pushButton_3_clicked(); 92 | void on_pushButton_4_clicked(); 93 | void on_pushButton_5_clicked(); 94 | 95 | void on_action_3_triggered(); 96 | void on_action_4_triggered(); 97 | 98 | void on_openButton_clicked(); 99 | void on_closeButton_clicked(); 100 | void on_sendMsgButton_clicked(); 101 | }; 102 | 103 | #endif // MAINWINDOW_HPP 104 | -------------------------------------------------------------------------------- /mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 1575 10 | 1032 11 | 12 | 13 | 14 | LPI诊断系统 15 | 16 | 17 | 18 | 19 | 20 | 30 21 | 40 22 | 131 23 | 21 24 | 25 | 26 | 27 | 普通彩色图像 28 | 29 | 30 | 31 | 32 | 33 | 30 34 | 70 35 | 851 36 | 541 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 1250 66 | 30 67 | 72 68 | 21 69 | 70 | 71 | 72 | 设备操作 73 | 74 | 75 | 76 | 77 | 78 | 1250 79 | 780 80 | 171 81 | 21 82 | 83 | 84 | 85 | LPI设备运行状态 86 | 87 | 88 | 89 | 90 | 91 | 470 92 | 10 93 | 111 94 | 21 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 410 105 | 10 106 | 81 107 | 21 108 | 109 | 110 | 111 | 年龄: 112 | 113 | 114 | 115 | 116 | 117 | 130 118 | 10 119 | 101 120 | 21 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 1400 131 | 30 132 | 161 133 | 21 134 | 135 | 136 | 137 | LPI设备操作 138 | 139 | 140 | 141 | 142 | 143 | 30 144 | 640 145 | 72 146 | 21 147 | 148 | 149 | 150 | 图像操作 151 | 152 | 153 | 154 | 155 | 156 | 30 157 | 10 158 | 131 159 | 21 160 | 161 | 162 | 163 | 患者姓名: 164 | 165 | 166 | 167 | 168 | 169 | 290 170 | 10 171 | 101 172 | 21 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 1250 183 | 40 184 | 146 185 | 721 186 | 187 | 188 | 189 | 190 | 191 | 192 | 打开视频文件 193 | 194 | 195 | 196 | 197 | 198 | 199 | 开启摄像头 200 | 201 | 202 | 203 | 204 | 205 | 206 | 边缘检测 207 | 208 | 209 | 210 | 211 | 212 | 213 | 保存图像 214 | 215 | 216 | 217 | 218 | 219 | 220 | 结束保存 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 460 230 | 40 231 | 201 232 | 21 233 | 234 | 235 | 236 | 高斯模糊处理后的图像 237 | 238 | 239 | 240 | 241 | 242 | 30 243 | 670 244 | 851 245 | 51 246 | 247 | 248 | 249 | 250 | 251 | 252 | 清空已选像素点 253 | 254 | 255 | 256 | 257 | 258 | 259 | 计算均值 260 | 261 | 262 | 263 | 264 | 265 | 266 | 计算多普勒 267 | 268 | 269 | 270 | 271 | 272 | 273 | 数据统计 274 | 275 | 276 | 277 | 278 | 279 | 280 | 保存并打印报告 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 30 290 | 810 291 | 851 292 | 141 293 | 294 | 295 | 296 | 297 | 298 | 299 | 30 300 | 780 301 | 111 302 | 21 303 | 304 | 305 | 306 | 数据统计 307 | 308 | 309 | 310 | 311 | 312 | 680 313 | 10 314 | 251 315 | 21 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 230 326 | 10 327 | 71 328 | 21 329 | 330 | 331 | 332 | 性别: 333 | 334 | 335 | 336 | 337 | 338 | 580 339 | 10 340 | 121 341 | 21 342 | 343 | 344 | 345 | 身份证号: 346 | 347 | 348 | 349 | 350 | 351 | 1250 352 | 770 353 | 291 354 | 151 355 | 356 | 357 | 358 | TextLabel 359 | 360 | 361 | true 362 | 363 | 364 | 365 | 366 | 367 | 1400 368 | 40 369 | 161 370 | 721 371 | 372 | 373 | 374 | 375 | 376 | 377 | 聚焦 378 | 379 | 380 | 381 | 382 | 383 | 384 | 红色激光的开启 385 | 386 | 387 | 388 | 389 | 390 | 391 | 红色激光关闭 392 | 393 | 394 | 395 | 396 | 397 | 398 | 红外激光的开启 399 | 400 | 401 | 402 | 403 | 404 | 405 | 红外激光关闭 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 910 415 | 70 416 | 321 417 | 851 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop 428 | 429 | 430 | true 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 910 456 | 40 457 | 171 458 | 21 459 | 460 | 461 | 462 | 实时数据 463 | 464 | 465 | false 466 | 467 | 468 | 469 | 470 | 471 | 710 472 | 950 473 | 301 474 | 16 475 | 476 | 477 | 478 | ——优谱激光科技(南京)有限公司—— 479 | 480 | 481 | 482 | 483 | 484 | 190 485 | 730 486 | 371 487 | 41 488 | 489 | 490 | 491 | 492 | 493 | 494 | 30 495 | 730 496 | 171 497 | 41 498 | 499 | 500 | 501 | 读取的串口数据: 502 | 503 | 504 | 505 | 506 | 507 | 508 | 0 509 | 0 510 | 1575 511 | 23 512 | 513 | 514 | 515 | 516 | 患者管理 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | LPI设备配置 526 | 527 | 528 | 529 | 530 | LPI设备校准 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | TopToolBarArea 540 | 541 | 542 | false 543 | 544 | 545 | 546 | 547 | 548 | 输入信息 549 | 550 | 551 | 552 | 553 | 查看信息 554 | 555 | 556 | 557 | 558 | 删除信息 559 | 560 | 561 | 562 | 563 | 564 | 565 | scenewindow 566 | QOpenGLWidget 567 |
scenewindow.hpp
568 |
569 | 570 | QCustomPlot 571 | QWidget 572 |
qcustomplot.h
573 | 1 574 |
575 |
576 | 577 | 578 |
579 | -------------------------------------------------------------------------------- /modifydialog.cpp: -------------------------------------------------------------------------------- 1 | #include "modifydialog.h" 2 | #include "ui_modifydialog.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | QStandardItemModel *model = new QStandardItemModel(); 12 | int curRow; 13 | QString curId; 14 | QString id[30]; // 用于记录符合条件患者id 15 | 16 | modifyDialog::modifyDialog(QWidget *parent) : 17 | QDialog(parent), 18 | ui(new Ui::modifyDialog) 19 | { 20 | ui->setupUi(this); 21 | setWindowTitle("edit patient info"); 22 | 23 | ui->tableView->setModel(model); 24 | model->setColumnCount(4); 25 | model->setHeaderData(0,Qt::Horizontal, "姓名"); 26 | model->setHeaderData(1,Qt::Horizontal, "性别"); 27 | model->setHeaderData(2,Qt::Horizontal, "年龄"); 28 | model->setHeaderData(3,Qt::Horizontal, "身份证号"); 29 | ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter); 30 | ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); 31 | } 32 | 33 | modifyDialog::~modifyDialog() 34 | { 35 | delete ui; 36 | } 37 | 38 | void modifyDialog::on_pushButton_clicked() 39 | { 40 | // query button 41 | QSqlQuery sql_query; 42 | QString Queryname = ui->textEdit->toPlainText(); 43 | QString select_sql = "select * from qtusb where name = '" + Queryname + "'"; 44 | 45 | int counts = 0; 46 | 47 | sql_query.prepare(select_sql); 48 | if(!sql_query.exec()) 49 | { 50 | qDebug()<setItem(counts,0,new QStandardItem(name)); 62 | if(sex) 63 | { 64 | model->setItem(counts,1,new QStandardItem("female")); 65 | } 66 | else { 67 | model->setItem(counts,1,new QStandardItem("male")); 68 | } 69 | model->setItem(counts,2,new QStandardItem(age)); 70 | model->setItem(counts,3,new QStandardItem(id[counts])); 71 | 72 | ++counts; 73 | } 74 | if(!counts) 75 | { 76 | QMessageBox::about(NULL, "查询结果", "未找到该条记录!"); 77 | } 78 | } 79 | } 80 | 81 | void modifyDialog::on_pushButton_2_clicked() 82 | { 83 | //delete button 84 | QSqlQuery sql_query; 85 | QString delete_sql = "delete from qtusb where id = '" + curId + "'"; 86 | sql_query.prepare(delete_sql); 87 | if(!sql_query.exec()) 88 | { 89 | qDebug()<removeRows(0,model->rowCount()); 96 | } 97 | 98 | void modifyDialog::on_tableView_clicked(const QModelIndex &index) 99 | { 100 | curRow = ui->tableView->currentIndex().row(); 101 | curId = id[curRow]; 102 | } 103 | 104 | 105 | void modifyDialog::on_pushButton_3_clicked() 106 | { 107 | // update button 108 | upddialog = new updateDialog; 109 | 110 | upddialog->setModal(true); 111 | upddialog->show(); 112 | model->removeRows(0,model->rowCount()); 113 | } 114 | -------------------------------------------------------------------------------- /modifydialog.h: -------------------------------------------------------------------------------- 1 | #ifndef MODIFYDIALOG_H 2 | #define MODIFYDIALOG_H 3 | 4 | #include 5 | #include "updatedialog.h" 6 | 7 | namespace Ui { 8 | class modifyDialog; 9 | } 10 | 11 | class modifyDialog : public QDialog 12 | { 13 | Q_OBJECT 14 | 15 | public: 16 | explicit modifyDialog(QWidget *parent = nullptr); 17 | ~modifyDialog(); 18 | 19 | private slots: 20 | void on_pushButton_clicked(); 21 | 22 | void on_pushButton_2_clicked(); 23 | 24 | void on_tableView_clicked(const QModelIndex &index); 25 | 26 | void on_pushButton_3_clicked(); 27 | 28 | private: 29 | Ui::modifyDialog *ui; 30 | updateDialog *upddialog; 31 | }; 32 | 33 | #endif // MODIFYDIALOG_H 34 | -------------------------------------------------------------------------------- /modifydialog.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | modifyDialog 4 | 5 | 6 | 7 | 0 8 | 0 9 | 613 10 | 333 11 | 12 | 13 | 14 | Dialog 15 | 16 | 17 | 18 | 19 | 70 20 | 20 21 | 91 22 | 41 23 | 24 | 25 | 26 | 27 | 等线 28 | 12 29 | 30 | 31 | 32 | 患者姓名: 33 | 34 | 35 | 36 | 37 | 38 | 160 39 | 30 40 | 131 41 | 31 42 | 43 | 44 | 45 | 46 | 等线 47 | 14 48 | 49 | 50 | 51 | 52 | 53 | 54 | 310 55 | 30 56 | 75 57 | 23 58 | 59 | 60 | 61 | 查询 62 | 63 | 64 | 65 | 66 | 67 | 120 68 | 80 69 | 411 70 | 192 71 | 72 | 73 | 74 | 75 | 76 | 77 | 400 78 | 30 79 | 75 80 | 23 81 | 82 | 83 | 84 | 删除 85 | 86 | 87 | 88 | 89 | 90 | 490 91 | 30 92 | 75 93 | 23 94 | 95 | 96 | 97 | 编辑 98 | 99 | 100 | 101 | 102 | 103 | 20 104 | 290 105 | 591 106 | 31 107 | 108 | 109 | 110 | 111 | 等线 112 | 11 113 | 114 | 115 | 116 | 提示:查询到结果后,点击任何一个单元格,然后对所在行的记录进行删除和编辑操作 117 | 118 | 119 | 120 | 121 | 122 | 123 | -------------------------------------------------------------------------------- /qextserialbase.cpp: -------------------------------------------------------------------------------- 1 | #include "qextserialbase.h" 2 | 3 | /*! 4 | \fn QextSerialBase::QextSerialBase() 5 | Default constructor. 6 | */ 7 | QextSerialBase::QextSerialBase() 8 | : QIODevice() 9 | { 10 | 11 | #ifdef _TTY_WIN_ 12 | setPortName("COM1"); 13 | 14 | #elif defined(_TTY_IRIX_) 15 | setPortName("/dev/ttyf1"); 16 | 17 | #elif defined(_TTY_HPUX_) 18 | setPortName("/dev/tty1p0"); 19 | 20 | #elif defined(_TTY_SUN_) 21 | setPortName("/dev/ttya"); 22 | 23 | #elif defined(_TTY_DIGITAL_) 24 | setPortName("/dev/tty01"); 25 | 26 | #elif defined(_TTY_FREEBSD_) 27 | setPortName("/dev/ttyd1"); 28 | 29 | #else 30 | setPortName("/dev/ttyS0"); 31 | #endif 32 | 33 | construct(); 34 | } 35 | 36 | /*! 37 | \fn QextSerialBase::QextSerialBase(const QString & name) 38 | Construct a port and assign it to the device specified by the name parameter. 39 | */ 40 | QextSerialBase::QextSerialBase(const QString & name) 41 | : QIODevice() 42 | { 43 | setPortName(name); 44 | construct(); 45 | } 46 | 47 | /*! 48 | \fn QextSerialBase::~QextSerialBase() 49 | Standard destructor. 50 | */ 51 | QextSerialBase::~QextSerialBase() 52 | { 53 | delete mutex; 54 | } 55 | 56 | /*! 57 | \fn void QextSerialBase::construct() 58 | Common constructor function for setting up default port settings. 59 | (115200 Baud, 8N1, Hardware flow control where supported, otherwise no flow control, and 0 ms timeout). 60 | */ 61 | void QextSerialBase::construct() 62 | { 63 | Settings.BaudRate=BAUD115200; 64 | Settings.DataBits=DATA_8; 65 | Settings.Parity=PAR_NONE; 66 | Settings.StopBits=STOP_1; 67 | Settings.FlowControl=FLOW_HARDWARE; 68 | Settings.Timeout_Millisec=500; 69 | mutex = new QMutex( QMutex::Recursive ); 70 | setOpenMode(QIODevice::NotOpen); 71 | } 72 | 73 | void QextSerialBase::setQueryMode(QueryMode mechanism) 74 | { 75 | _queryMode = mechanism; 76 | } 77 | 78 | /*! 79 | \fn void QextSerialBase::setPortName(const QString & name) 80 | Sets the name of the device associated with the object, e.g. "COM1", or "/dev/ttyS0". 81 | */ 82 | void QextSerialBase::setPortName(const QString & name) 83 | { 84 | port = name; 85 | } 86 | 87 | /*! 88 | \fn QString QextSerialBase::portName() const 89 | Returns the name set by setPortName(). 90 | */ 91 | QString QextSerialBase::portName() const 92 | { 93 | return port; 94 | } 95 | 96 | /*! 97 | \fn BaudRateType QextSerialBase::baudRate(void) const 98 | Returns the baud rate of the serial port. For a list of possible return values see 99 | the definition of the enum BaudRateType. 100 | */ 101 | BaudRateType QextSerialBase::baudRate(void) const 102 | { 103 | return Settings.BaudRate; 104 | } 105 | 106 | /*! 107 | \fn DataBitsType QextSerialBase::dataBits() const 108 | Returns the number of data bits used by the port. For a list of possible values returned by 109 | this function, see the definition of the enum DataBitsType. 110 | */ 111 | DataBitsType QextSerialBase::dataBits() const 112 | { 113 | return Settings.DataBits; 114 | } 115 | 116 | /*! 117 | \fn ParityType QextSerialBase::parity() const 118 | Returns the type of parity used by the port. For a list of possible values returned by 119 | this function, see the definition of the enum ParityType. 120 | */ 121 | ParityType QextSerialBase::parity() const 122 | { 123 | return Settings.Parity; 124 | } 125 | 126 | /*! 127 | \fn StopBitsType QextSerialBase::stopBits() const 128 | Returns the number of stop bits used by the port. For a list of possible return values, see 129 | the definition of the enum StopBitsType. 130 | */ 131 | StopBitsType QextSerialBase::stopBits() const 132 | { 133 | return Settings.StopBits; 134 | } 135 | 136 | /*! 137 | \fn FlowType QextSerialBase::flowControl() const 138 | Returns the type of flow control used by the port. For a list of possible values returned 139 | by this function, see the definition of the enum FlowType. 140 | */ 141 | FlowType QextSerialBase::flowControl() const 142 | { 143 | return Settings.FlowControl; 144 | } 145 | 146 | /*! 147 | \fn bool QextSerialBase::isSequential() const 148 | Returns true if device is sequential, otherwise returns false. Serial port is sequential device 149 | so this function always returns true. Check QIODevice::isSequential() documentation for more 150 | information. 151 | */ 152 | bool QextSerialBase::isSequential() const 153 | { 154 | return true; 155 | } 156 | 157 | /*! 158 | \fn bool QextSerialBase::atEnd() const 159 | This function will return true if the input buffer is empty (or on error), and false otherwise. 160 | Call QextSerialBase::lastError() for error information. 161 | */ 162 | bool QextSerialBase::atEnd() const 163 | { 164 | if (size()) { 165 | return true; 166 | } 167 | return false; 168 | } 169 | 170 | /*! 171 | \fn qint64 QextSerialBase::readLine(char * data, qint64 maxSize) 172 | This function will read a line of buffered input from the port, stopping when either maxSize bytes 173 | have been read, the port has no more data available, or a newline is encountered. 174 | The value returned is the length of the string that was read. 175 | */ 176 | qint64 QextSerialBase::readLine(char * data, qint64 maxSize) 177 | { 178 | qint64 numBytes = bytesAvailable(); 179 | char* pData = data; 180 | 181 | if (maxSize < 2) //maxSize must be larger than 1 182 | return -1; 183 | 184 | /*read a byte at a time for MIN(bytesAvail, maxSize - 1) iterations, or until a newline*/ 185 | while (pData<(data+numBytes) && --maxSize) { 186 | readData(pData, 1); 187 | if (*pData++ == '\n') { 188 | break; 189 | } 190 | } 191 | *pData='\0'; 192 | 193 | /*return size of data read*/ 194 | return (pData-data); 195 | } 196 | 197 | /*! 198 | \fn ulong QextSerialBase::lastError() const 199 | Returns the code for the last error encountered by the port, or E_NO_ERROR if the last port 200 | operation was successful. Possible error codes are: 201 | 202 | \verbatim 203 | Error Explanation 204 | --------------------------- ------------------------------------------------------------- 205 | E_NO_ERROR No Error has occured 206 | E_INVALID_FD Invalid file descriptor (port was not opened correctly) 207 | E_NO_MEMORY Unable to allocate memory tables (POSIX) 208 | E_CAUGHT_NON_BLOCKED_SIGNAL Caught a non-blocked signal (POSIX) 209 | E_PORT_TIMEOUT Operation timed out (POSIX) 210 | E_INVALID_DEVICE The file opened by the port is not a character device (POSIX) 211 | E_BREAK_CONDITION The port detected a break condition 212 | E_FRAMING_ERROR The port detected a framing error 213 | (usually caused by incorrect baud rate settings) 214 | E_IO_ERROR There was an I/O error while communicating with the port 215 | E_BUFFER_OVERRUN Character buffer overrun 216 | E_RECEIVE_OVERFLOW Receive buffer overflow 217 | E_RECEIVE_PARITY_ERROR The port detected a parity error in the received data 218 | E_TRANSMIT_OVERFLOW Transmit buffer overflow 219 | E_READ_FAILED General read operation failure 220 | E_WRITE_FAILED General write operation failure 221 | \endverbatim 222 | */ 223 | ulong QextSerialBase::lastError() const 224 | { 225 | return lastErr; 226 | } 227 | -------------------------------------------------------------------------------- /qextserialbase.h: -------------------------------------------------------------------------------- 1 | #ifndef _QEXTSERIALBASE_H_ 2 | #define _QEXTSERIALBASE_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | /*if all warning messages are turned off, flag portability warnings to be turned off as well*/ 10 | #ifdef _TTY_NOWARN_ 11 | #define _TTY_NOWARN_PORT_ 12 | #endif 13 | 14 | /*macros for thread support*/ 15 | #define LOCK_MUTEX() mutex->lock() 16 | #define UNLOCK_MUTEX() mutex->unlock() 17 | 18 | /*macros for warning and debug messages*/ 19 | #ifdef _TTY_NOWARN_PORT_ 20 | #define TTY_PORTABILITY_WARNING(s) 21 | #else 22 | #define TTY_PORTABILITY_WARNING(s) qWarning(s) 23 | #endif /*_TTY_NOWARN_PORT_*/ 24 | #ifdef _TTY_NOWARN_ 25 | #define TTY_WARNING(s) 26 | #else 27 | #define TTY_WARNING(s) qWarning(s) 28 | #endif /*_TTY_NOWARN_*/ 29 | 30 | 31 | /*line status constants*/ 32 | #define LS_CTS 0x01 33 | #define LS_DSR 0x02 34 | #define LS_DCD 0x04 35 | #define LS_RI 0x08 36 | #define LS_RTS 0x10 37 | #define LS_DTR 0x20 38 | #define LS_ST 0x40 39 | #define LS_SR 0x80 40 | 41 | /*error constants*/ 42 | #define E_NO_ERROR 0 43 | #define E_INVALID_FD 1 44 | #define E_NO_MEMORY 2 45 | #define E_CAUGHT_NON_BLOCKED_SIGNAL 3 46 | #define E_PORT_TIMEOUT 4 47 | #define E_INVALID_DEVICE 5 48 | #define E_BREAK_CONDITION 6 49 | #define E_FRAMING_ERROR 7 50 | #define E_IO_ERROR 8 51 | #define E_BUFFER_OVERRUN 9 52 | #define E_RECEIVE_OVERFLOW 10 53 | #define E_RECEIVE_PARITY_ERROR 11 54 | #define E_TRANSMIT_OVERFLOW 12 55 | #define E_READ_FAILED 13 56 | #define E_WRITE_FAILED 14 57 | 58 | /*! 59 | * Enums for port settings. 60 | */ 61 | enum NamingConvention 62 | { 63 | WIN_NAMES, 64 | IRIX_NAMES, 65 | HPUX_NAMES, 66 | SUN_NAMES, 67 | DIGITAL_NAMES, 68 | FREEBSD_NAMES, 69 | LINUX_NAMES 70 | }; 71 | 72 | enum BaudRateType 73 | { 74 | BAUD50, //POSIX ONLY 75 | BAUD75, //POSIX ONLY 76 | BAUD110, 77 | BAUD134, //POSIX ONLY 78 | BAUD150, //POSIX ONLY 79 | BAUD200, //POSIX ONLY 80 | BAUD300, 81 | BAUD600, 82 | BAUD1200, 83 | BAUD1800, //POSIX ONLY 84 | BAUD2400, 85 | BAUD4800, 86 | BAUD9600, 87 | BAUD14400, //WINDOWS ONLY 88 | BAUD19200, 89 | BAUD38400, 90 | BAUD56000, //WINDOWS ONLY 91 | BAUD57600, 92 | BAUD76800, //POSIX ONLY 93 | BAUD115200, 94 | BAUD128000, //WINDOWS ONLY 95 | BAUD256000 //WINDOWS ONLY 96 | }; 97 | 98 | enum DataBitsType 99 | { 100 | DATA_5, 101 | DATA_6, 102 | DATA_7, 103 | DATA_8 104 | }; 105 | 106 | enum ParityType 107 | { 108 | PAR_NONE, 109 | PAR_ODD, 110 | PAR_EVEN, 111 | PAR_MARK, //WINDOWS ONLY 112 | PAR_SPACE 113 | }; 114 | 115 | enum StopBitsType 116 | { 117 | STOP_1, 118 | STOP_1_5, //WINDOWS ONLY 119 | STOP_2 120 | }; 121 | 122 | enum FlowType 123 | { 124 | FLOW_OFF, 125 | FLOW_HARDWARE, 126 | FLOW_XONXOFF 127 | }; 128 | 129 | /** 130 | * structure to contain port settings 131 | */ 132 | struct PortSettings 133 | { 134 | BaudRateType BaudRate; 135 | DataBitsType DataBits; 136 | ParityType Parity; 137 | StopBitsType StopBits; 138 | FlowType FlowControl; 139 | long Timeout_Millisec; 140 | }; 141 | 142 | /*! 143 | * \author Stefan Sander 144 | * \author Michal Policht 145 | * 146 | * A common base class for Win_QextSerialBase, Posix_QextSerialBase and QextSerialPort. 147 | */ 148 | class QextSerialBase : public QIODevice 149 | { 150 | Q_OBJECT 151 | 152 | public: 153 | enum QueryMode { 154 | Polling, 155 | EventDriven 156 | }; 157 | 158 | protected: 159 | QMutex* mutex; 160 | QString port; 161 | PortSettings Settings; 162 | ulong lastErr; 163 | QextSerialBase::QueryMode _queryMode; 164 | 165 | virtual qint64 readData(char * data, qint64 maxSize)=0; 166 | virtual qint64 writeData(const char * data, qint64 maxSize)=0; 167 | 168 | public: 169 | QextSerialBase(); 170 | QextSerialBase(const QString & name); 171 | virtual ~QextSerialBase(); 172 | virtual void construct(); 173 | virtual void setPortName(const QString & name); 174 | virtual QString portName() const; 175 | 176 | /**! 177 | * Get query mode. 178 | * \return query mode. 179 | */ 180 | inline QextSerialBase::QueryMode queryMode() const { return _queryMode; }; 181 | 182 | /*! 183 | * Set desired serial communication handling style. You may choose from polling 184 | * or event driven approach. This function does nothing when port is open; to 185 | * apply changes port must be reopened. 186 | * 187 | * In event driven approach read() and write() functions are acting 188 | * asynchronously. They return immediately and the operation is performed in 189 | * the background, so they doesn't freeze the calling thread. 190 | * To determine when operation is finished, QextSerialPort runs separate thread 191 | * and monitors serial port events. Whenever the event occurs, adequate signal 192 | * is emitted. 193 | * 194 | * When polling is set, read() and write() are acting synchronously. Signals are 195 | * not working in this mode and some functions may not be available. The advantage 196 | * of polling is that it generates less overhead due to lack of signals emissions 197 | * and it doesn't start separate thread to monitor events. 198 | * 199 | * Generally event driven approach is more capable and friendly, although some 200 | * applications may need as low overhead as possible and then polling comes. 201 | * 202 | * \param mode query mode. 203 | */ 204 | virtual void setQueryMode(QueryMode mode); 205 | 206 | // virtual void setBlockingRead(bool block) = 0; ///< @todo implement. 207 | 208 | virtual void setBaudRate(BaudRateType)=0; 209 | virtual BaudRateType baudRate() const; 210 | virtual void setDataBits(DataBitsType)=0; 211 | virtual DataBitsType dataBits() const; 212 | virtual void setParity(ParityType)=0; 213 | virtual ParityType parity() const; 214 | virtual void setStopBits(StopBitsType)=0; 215 | virtual StopBitsType stopBits() const; 216 | virtual void setFlowControl(FlowType)=0; 217 | virtual FlowType flowControl() const; 218 | virtual void setTimeout(long)=0; 219 | 220 | virtual bool open(OpenMode mode)=0; 221 | virtual bool isSequential() const; 222 | virtual void close()=0; 223 | virtual void flush()=0; 224 | 225 | virtual qint64 size() const=0; 226 | virtual qint64 bytesAvailable()=0; 227 | virtual bool atEnd() const; 228 | 229 | virtual void ungetChar(char c)=0; 230 | virtual qint64 readLine(char * data, qint64 maxSize); 231 | 232 | virtual ulong lastError() const; 233 | virtual void translateError(ulong error)=0; 234 | 235 | virtual void setDtr(bool set=true)=0; 236 | virtual void setRts(bool set=true)=0; 237 | virtual ulong lineStatus()=0; 238 | 239 | signals: 240 | /** 241 | * This signal is emitted whenever port settings are updated. 242 | * \param valid \p true if settings are valid, \p false otherwise. 243 | * 244 | * @todo implement. 245 | */ 246 | // void validSettings(bool valid); 247 | 248 | /*! 249 | * This signal is emitted whenever dsr line has changed its state. You may 250 | * use this signal to check if device is connected. 251 | * \param status \p true when DSR signal is on, \p false otherwise. 252 | * 253 | * \see lineStatus(). 254 | */ 255 | void dsrChanged(bool status); 256 | }; 257 | 258 | #endif 259 | -------------------------------------------------------------------------------- /qextserialport.cpp: -------------------------------------------------------------------------------- 1 | 2 | /*! 3 | \class QextSerialPort 4 | \author Stefan Sander 5 | \author Michal Policht 6 | 7 | A cross-platform serial port class. 8 | This class encapsulates a serial port on both POSIX and Windows systems. The user will be 9 | notified of errors and possible portability conflicts at run-time by default - this behavior can 10 | be turned off by defining _TTY_NOWARN_ (to turn off all warnings) or _TTY_NOWARN_PORT_ (to turn 11 | off portability warnings) in the project. 12 | 13 | You may choose from polling or event driven API. For details check setQueryMode() documentation. 14 | 15 | \note 16 | On Windows NT/2000/XP this class uses Win32 serial port functions by default. The user may 17 | select POSIX behavior under NT, 2000, or XP ONLY by defining _TTY_POSIX_ in the project. I can 18 | make no guarantees as to the quality of POSIX support under NT/2000 however. 19 | 20 | */ 21 | 22 | #include 23 | #include "qextserialport.h" 24 | 25 | /*! 26 | Default constructor. Note that the naming convention used by a QextSerialPort constructed with 27 | this constructor will be determined by #defined constants, or lack thereof - the default behavior 28 | is the same as _TTY_LINUX_. Possible naming conventions and their associated constants are: 29 | 30 | \verbatim 31 | 32 | Constant Used By Naming Convention 33 | ---------- ------------- ------------------------ 34 | _TTY_WIN_ Windows COM1, COM2 35 | _TTY_IRIX_ SGI/IRIX /dev/ttyf1, /dev/ttyf2 36 | _TTY_HPUX_ HP-UX /dev/tty1p0, /dev/tty2p0 37 | _TTY_SUN_ SunOS/Solaris /dev/ttya, /dev/ttyb 38 | _TTY_DIGITAL_ Digital UNIX /dev/tty01, /dev/tty02 39 | _TTY_FREEBSD_ FreeBSD /dev/ttyd0, /dev/ttyd1 40 | _TTY_LINUX_ Linux /dev/ttyS0, /dev/ttyS1 41 | Linux /dev/ttyS0, /dev/ttyS1 42 | \endverbatim 43 | 44 | The object will be associated with the first port in the system, e.g. COM1 on Windows systems. 45 | See the other constructors if you need to use a port other than the first. 46 | */ 47 | QextSerialPort::QextSerialPort() 48 | : QextBaseType() 49 | {} 50 | 51 | /*! 52 | Constructs a serial port attached to the port specified by name. 53 | name is the name of the device, which is windowsystem-specific, 54 | e.g."COM1" or "/dev/ttyS0". 55 | 56 | \see setQueryMode(). 57 | */ 58 | QextSerialPort::QextSerialPort(const QString & name, QueryMode mode) 59 | : QextBaseType(name, mode) 60 | { 61 | } 62 | 63 | /*! 64 | Constructs a port with default name and settings specified by the settings parameter. 65 | 66 | \see setQueryMode(). 67 | */ 68 | QextSerialPort::QextSerialPort(PortSettings const& settings, QueryMode mode) 69 | : QextBaseType(settings, mode) 70 | {} 71 | 72 | /*! 73 | Constructs a port with the name and settings specified. 74 | 75 | \see setQueryMode(). 76 | */ 77 | QextSerialPort::QextSerialPort(const QString & name, PortSettings const& settings, QueryMode mode) 78 | : QextBaseType(name, settings, mode) 79 | {} 80 | 81 | /*! 82 | Copy constructor. 83 | 84 | \deprecated 85 | */ 86 | QextSerialPort::QextSerialPort(const QextSerialPort& s) 87 | : QextBaseType(s) 88 | {} 89 | 90 | /*! 91 | \fn QextSerialPort& QextSerialPort::operator=(const QextSerialPort& s) 92 | Overrides the = operator. 93 | 94 | \deprecated 95 | */ 96 | QextSerialPort& QextSerialPort::operator=(const QextSerialPort& s) 97 | { 98 | return (QextSerialPort&)QextBaseType::operator=(s); 99 | } 100 | 101 | /*! 102 | \fn QextSerialPort::~QextSerialPort() 103 | Standard destructor. 104 | */ 105 | QextSerialPort::~QextSerialPort() 106 | {} 107 | -------------------------------------------------------------------------------- /qextserialport.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _QEXTSERIALPORT_H_ 3 | #define _QEXTSERIALPORT_H_ 4 | 5 | /*POSIX CODE*/ 6 | #ifdef _TTY_POSIX_ 7 | #include "posix_qextserialport.h" 8 | #define QextBaseType Posix_QextSerialPort 9 | 10 | /*MS WINDOWS CODE*/ 11 | #else 12 | #include "win_qextserialport.h" 13 | #define QextBaseType Win_QextSerialPort 14 | #endif 15 | 16 | class QextSerialPort: public QextBaseType 17 | { 18 | Q_OBJECT 19 | 20 | public: 21 | typedef QextSerialBase::QueryMode QueryMode; 22 | 23 | QextSerialPort(); 24 | QextSerialPort(const QString & name, QueryMode mode = QextSerialPort::Polling); 25 | QextSerialPort(PortSettings const& s, QueryMode mode = QextSerialPort::Polling); 26 | QextSerialPort(const QString & name, PortSettings const& s, QueryMode mode = QextSerialPort::Polling); 27 | QextSerialPort(const QextSerialPort& s); 28 | QextSerialPort& operator=(const QextSerialPort&); 29 | virtual ~QextSerialPort(); 30 | 31 | }; 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /qt_usb_video.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2019-01-31T14:51:55 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | QT += core gui printsupport 9 | QT += core gui opengl 10 | QT += widgets 11 | QT += sql 12 | 13 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 14 | 15 | TARGET = qt_usb_video 16 | TEMPLATE = app 17 | 18 | 19 | SOURCES += main.cpp\ 20 | mainwindow.cpp \ 21 | qextserialbase.cpp \ 22 | qextserialport.cpp \ 23 | win_qextserialport.cpp \ 24 | scenewindow.cpp \ 25 | inputdialog.cpp \ 26 | modifydialog.cpp \ 27 | updatedialog.cpp \ 28 | qcustomplot.cpp 29 | 30 | HEADERS += mainwindow.hpp \ 31 | qextserialbase.h \ 32 | qextserialport.h \ 33 | win_qextserialport.h \ 34 | scenewindow.hpp \ 35 | inputdialog.h \ 36 | modifydialog.h \ 37 | updatedialog.h \ 38 | qcustomplot.h 39 | 40 | FORMS += mainwindow.ui \ 41 | inputdialog.ui \ 42 | modifydialog.ui \ 43 | updatedialog.ui 44 | 45 | LIBS += -lopengl32 -lglu32 46 | 47 | win32:CONFIG(release, debug|release): LIBS += -L$$PWD/bulkloop/lib/x64/ -lCyAPI 48 | else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/bulkloop/lib/x64/ -lCyAPI 49 | else:unix: LIBS += -L$$PWD/bulkloop/lib/x64/ -lCyAPI 50 | 51 | INCLUDEPATH += $$PWD/bulkloop/inc 52 | DEPENDPATH += $$PWD/bulkloop/inc 53 | 54 | win32:CONFIG(release, debug|release): LIBS += -L$$PWD/opencv2/x64/vc15/lib/ -lopencv_world401 55 | else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/opencv2/x64/vc15/lib/ -lopencv_world401d 56 | else:unix: LIBS += -L$$PWD/opencv2/x64/vc15/lib/ -lopencv_world401 57 | 58 | INCLUDEPATH += $$PWD/opencv2 59 | DEPENDPATH += $$PWD/opencv2 60 | 61 | 62 | win32:CONFIG(release, debug|release): LIBS += -L$$PWD/ -lSetupapi 63 | else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/ -lSetupapi 64 | else:unix: LIBS += -L$$PWD/ -lSetupapi 65 | 66 | win32:CONFIG(release, debug|release): LIBS += -L$$PWD/ -llegacy_stdio_definitions 67 | else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/ -llegacy_stdio_definitions 68 | else:unix: LIBS += -L$$PWD/ -llegacy_stdio_definitions 69 | 70 | win32:CONFIG(release, debug|release): LIBS += -L$$PWD/ -lUser32 71 | else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/ -lUser32 72 | else:unix: LIBS += -L$$PWD/ -lUser32 73 | 74 | 75 | 76 | 77 | INCLUDEPATH += $$PWD/bulkloop/lib/x64 78 | DEPENDPATH += $$PWD/bulkloop/lib/x64 79 | 80 | DISTFILES += \ 81 | shader/simple.frag \ 82 | shader/simple.vert \ 83 | shader/notebook 84 | 85 | 86 | -------------------------------------------------------------------------------- /scenewindow.cpp: -------------------------------------------------------------------------------- 1 | #include "scenewindow.hpp" 2 | #include 3 | #include 4 | #include 5 | #include "scenewindow.hpp" 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | 14 | #include 15 | 16 | #include 17 | 18 | 19 | scenewindow::scenewindow( QWidget* parent, const char* name, bool fs ) 20 | 21 | :QGLWidget( parent ) 22 | 23 | { 24 | fullScreen = fs; //设置窗口的位置,即左上角为(0,0)点,大小为640*480。 25 | setGeometry( 0, 0, 640, 480 ); // 设置窗口的标题为“My's OpenGL Framework”。 26 | //setCaption( "My's OpenGL Framework" ); // 如果fullscreen为真,那么就全屏显示这个窗口。 27 | if ( fullScreen ) 28 | { 29 | showFullScreen(); 30 | } 31 | 32 | } 33 | 34 | 35 | 36 | scenewindow::~scenewindow() 37 | { 38 | 39 | } 40 | 41 | 42 | 43 | void scenewindow::initializeGL() 44 | 45 | { 46 | glShadeModel(GL_SMOOTH); 47 | glClearColor( 0.0, 0.0, 0.0, 0.0 ); 48 | glClearDepth( 1.0 ); 49 | glEnable( GL_DEPTH_TEST ); 50 | glDepthFunc( GL_LEQUAL ); 51 | glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); 52 | return; 53 | 54 | } 55 | 56 | 57 | 58 | void scenewindow::paintGL() 59 | 60 | { 61 | 62 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 63 | glLoadIdentity(); 64 | glTranslatef(-1.5,0.0,-6.0); 65 | glBegin(GL_TRIANGLES); 66 | glVertex3f(0.0,1.0,0.0); 67 | glVertex3f(-1.0,-1.0,0.0); 68 | glVertex3f(1.0,-1.0,0.0); 69 | glEnd(); 70 | return; 71 | 72 | 73 | } 74 | 75 | 76 | 77 | void scenewindow::resizeGL(int width, int height) 78 | 79 | { 80 | 81 | if( height == 0 ) 82 | { height =1; } //重置当前的视口(Viewport) 83 | glViewport( 0, 0, (GLint)width, (GLint)height ); //选择投影矩阵。 84 | glMatrixMode( GL_PROJECTION ); //重置投影矩阵 85 | glLoadIdentity(); //建立透视投影矩阵 86 | gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0 ); // 87 | //gluPerspective(45.0,(GLfloat)width()/(GLfloat)height(),0.1,100.0); // 选择模型观察矩阵 88 | glMatrixMode( GL_MODELVIEW ); // 重置模型观察矩阵。 89 | glLoadIdentity(); 90 | return; 91 | 92 | } 93 | 94 | void scenewindow::keyPressEvent(QKeyEvent * e) 95 | { switch ( e->key() ) 96 | { //如果按下了F2键,那么屏幕是否全屏的状态就切换一次。然后再根据需要,显示所要的全屏窗口或者 普通窗口。 97 | case Qt::Key_F2: fullScreen = !fullScreen; 98 | 99 | if ( fullScreen ) 100 | { 101 | showFullScreen(); 102 | } 103 | else 104 | { 105 | showNormal(); 106 | setGeometry( 0, 0, 640, 480 ); 107 | } 108 | updateGL(); 109 | break; //如果按下了Escape键,程序退出。 110 | case Qt::Key_Escape: 111 | close(); 112 | } 113 | return; 114 | } 115 | 116 | 117 | -------------------------------------------------------------------------------- /scenewindow.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SCENEWINDOW_HPP 2 | #define SCENEWINDOW_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include 18 | 19 | #include 20 | 21 | #include 22 | 23 | class scenewindow: public QGLWidget 24 | { 25 | Q_OBJECT 26 | public: 27 | explicit scenewindow( QWidget* parent = 0, const char* name = 0, bool fs = false );\ 28 | 29 | ~scenewindow(); 30 | 31 | private: 32 | 33 | protected: 34 | void initializeGL(); 35 | 36 | void paintGL(); 37 | 38 | void resizeGL( int width, int height ); 39 | 40 | void keyPressEvent(QKeyEvent *); 41 | bool fullScreen; 42 | }; 43 | 44 | #endif // SCENEWINDOW_HPP 45 | -------------------------------------------------------------------------------- /updatedialog.cpp: -------------------------------------------------------------------------------- 1 | #include "updatedialog.h" 2 | #include "ui_updatedialog.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include"modifydialog.h" 10 | 11 | extern QString curId; 12 | 13 | updateDialog::updateDialog(QWidget *parent) : 14 | QDialog(parent), 15 | ui(new Ui::updateDialog) 16 | { 17 | ui->setupUi(this); 18 | setWindowTitle("update patient info"); 19 | ui->buttonGroup->setId(ui->radioButton, 0); 20 | ui->buttonGroup->setId(ui->radioButton_2, 1); 21 | ui->radioButton->setChecked(true); 22 | } 23 | 24 | updateDialog::~updateDialog() 25 | { 26 | delete ui; 27 | } 28 | 29 | void updateDialog::on_pushButton_2_clicked() 30 | { 31 | // cancel button 32 | emit sendsignal(); 33 | this->close(); 34 | } 35 | 36 | void updateDialog::on_pushButton_clicked() 37 | { 38 | // Apply button 39 | QSqlQuery sql_query; 40 | int btnId = ui->buttonGroup->checkedId(); 41 | 42 | QString update_sql = "update qtusb set name = :name, sex = :sex, age = :age, id = :newid where id = :id"; 43 | sql_query.prepare(update_sql); 44 | sql_query.bindValue(":name", ui->textEdit->toPlainText()); 45 | sql_query.bindValue(":sex", btnId); 46 | sql_query.bindValue(":age", ui->textEdit_2->toPlainText().toInt()); 47 | sql_query.bindValue(":newid", ui->textEdit_3->toPlainText()); 48 | sql_query.bindValue(":id",curId); 49 | 50 | if(!sql_query.exec()) 51 | { 52 | qDebug() << sql_query.lastError(); 53 | } 54 | else 55 | { 56 | QMessageBox::about(NULL,"提示","更新成功"); 57 | } 58 | 59 | emit sendsignal(); 60 | this->close(); 61 | } 62 | -------------------------------------------------------------------------------- /updatedialog.h: -------------------------------------------------------------------------------- 1 | #ifndef UPDATEDIALOG_H 2 | #define UPDATEDIALOG_H 3 | 4 | #include 5 | 6 | namespace Ui { 7 | class updateDialog; 8 | } 9 | 10 | class updateDialog : public QDialog 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | explicit updateDialog(QWidget *parent = nullptr); 16 | ~updateDialog(); 17 | 18 | private slots: 19 | void on_pushButton_2_clicked(); 20 | 21 | void on_pushButton_clicked(); 22 | 23 | signals: 24 | void sendsignal(); 25 | 26 | private: 27 | Ui::updateDialog *ui; 28 | }; 29 | 30 | #endif // UPDATEDIALOG_H 31 | -------------------------------------------------------------------------------- /updatedialog.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | updateDialog 4 | 5 | 6 | 7 | 0 8 | 0 9 | 460 10 | 332 11 | 12 | 13 | 14 | Dialog 15 | 16 | 17 | 18 | 19 | 120 20 | 60 21 | 61 22 | 31 23 | 24 | 25 | 26 | 27 | 等线 28 | 12 29 | 30 | 31 | 32 | 姓名: 33 | 34 | 35 | 36 | 37 | 38 | 120 39 | 110 40 | 71 41 | 31 42 | 43 | 44 | 45 | 46 | 等线 47 | 12 48 | 49 | 50 | 51 | 性别: 52 | 53 | 54 | 55 | 56 | 57 | 120 58 | 160 59 | 91 60 | 51 61 | 62 | 63 | 64 | 65 | 等线 66 | 12 67 | 68 | 69 | 70 | 年龄: 71 | 72 | 73 | 74 | 75 | 76 | 90 77 | 220 78 | 81 79 | 51 80 | 81 | 82 | 83 | 84 | 等线 85 | 12 86 | 87 | 88 | 89 | 身份证号: 90 | 91 | 92 | 93 | 94 | 95 | 200 96 | 60 97 | 141 98 | 31 99 | 100 | 101 | 102 | 103 | 等线 104 | 12 105 | 106 | 107 | 108 | 109 | 110 | 111 | 360 112 | 290 113 | 75 114 | 23 115 | 116 | 117 | 118 | 提交 119 | 120 | 121 | 122 | 123 | 124 | 40 125 | 290 126 | 75 127 | 23 128 | 129 | 130 | 131 | 返回 132 | 133 | 134 | 135 | 136 | 137 | 210 138 | 120 139 | 89 140 | 16 141 | 142 | 143 | 144 | 145 | 146 | 147 | buttonGroup 148 | 149 | 150 | 151 | 152 | 153 | 280 154 | 120 155 | 89 156 | 16 157 | 158 | 159 | 160 | 161 | 162 | 163 | buttonGroup 164 | 165 | 166 | 167 | 168 | 169 | 240 170 | 170 171 | 51 172 | 31 173 | 174 | 175 | 176 | 177 | Adobe Arabic 178 | 14 179 | 180 | 181 | 182 | 183 | 184 | 185 | 170 186 | 230 187 | 191 188 | 31 189 | 190 | 191 | 192 | 193 | Adobe Arabic 194 | 14 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | -------------------------------------------------------------------------------- /win_qextserialport.cpp: -------------------------------------------------------------------------------- 1 | //#include 2 | //#include 3 | //#include 4 | //#include 5 | #include 6 | #include "win_qextserialport.h" 7 | 8 | 9 | /*! 10 | \fn Win_QextSerialPort::Win_QextSerialPort() 11 | Default constructor. Note that the name of the device used by a Win_QextSerialPort constructed 12 | with this constructor will be determined by #defined constants, or lack thereof - the default 13 | behavior is the same as _TTY_LINUX_. Possible naming conventions and their associated constants 14 | are: 15 | 16 | \verbatim 17 | 18 | Constant Used By Naming Convention 19 | ---------- ------------- ------------------------ 20 | _TTY_WIN_ Windows COM1, COM2 21 | _TTY_IRIX_ SGI/IRIX /dev/ttyf1, /dev/ttyf2 22 | _TTY_HPUX_ HP-UX /dev/tty1p0, /dev/tty2p0 23 | _TTY_SUN_ SunOS/Solaris /dev/ttya, /dev/ttyb 24 | _TTY_DIGITAL_ Digital UNIX /dev/tty01, /dev/tty02 25 | _TTY_FREEBSD_ FreeBSD /dev/ttyd0, /dev/ttyd1 26 | _TTY_LINUX_ Linux /dev/ttyS0, /dev/ttyS1 27 | Linux /dev/ttyS0, /dev/ttyS1 28 | \endverbatim 29 | 30 | This constructor associates the object with the first port on the system, e.g. COM1 for Windows 31 | platforms. See the other constructor if you need a port other than the first. 32 | */ 33 | Win_QextSerialPort::Win_QextSerialPort(): 34 | QextSerialBase() 35 | { 36 | Win_Handle=INVALID_HANDLE_VALUE; 37 | init(); 38 | } 39 | 40 | /*!Win_QextSerialPort::Win_QextSerialPort(const Win_QextSerialPort&) 41 | Copy constructor. 42 | */ 43 | Win_QextSerialPort::Win_QextSerialPort(const Win_QextSerialPort& s): 44 | QextSerialBase(s.port) 45 | { 46 | Win_Handle=INVALID_HANDLE_VALUE; 47 | _queryMode = s._queryMode; 48 | _bytesToWrite = s._bytesToWrite; 49 | bytesToWriteLock = new QReadWriteLock; 50 | overlapThread = new Win_QextSerialThread(this); 51 | memcpy(& overlap, & s.overlap, sizeof(OVERLAPPED)); 52 | memcpy(& overlapWrite, & s.overlapWrite, sizeof(OVERLAPPED)); 53 | setOpenMode(s.openMode()); 54 | lastErr=s.lastErr; 55 | port = s.port; 56 | Settings.FlowControl=s.Settings.FlowControl; 57 | Settings.Parity=s.Settings.Parity; 58 | Settings.DataBits=s.Settings.DataBits; 59 | Settings.StopBits=s.Settings.StopBits; 60 | Settings.BaudRate=s.Settings.BaudRate; 61 | Win_Handle=s.Win_Handle; 62 | memcpy(&Win_CommConfig, &s.Win_CommConfig, sizeof(COMMCONFIG)); 63 | memcpy(&Win_CommTimeouts, &s.Win_CommTimeouts, sizeof(COMMTIMEOUTS)); 64 | if (s.overlapThread->isRunning()) 65 | overlapThread->start(); 66 | } 67 | 68 | /*! 69 | \fn Win_QextSerialPort::Win_QextSerialPort(const QString & name) 70 | Constructs a serial port attached to the port specified by devName. 71 | devName is the name of the device, which is windowsystem-specific, 72 | e.g."COM2" or "/dev/ttyS0". 73 | */ 74 | Win_QextSerialPort::Win_QextSerialPort(const QString & name, QextSerialBase::QueryMode mode): 75 | QextSerialBase(name) 76 | { 77 | Win_Handle=INVALID_HANDLE_VALUE; 78 | setQueryMode(mode); 79 | init(); 80 | } 81 | 82 | /*! 83 | \fn Win_QextSerialPort::Win_QextSerialPort(const PortSettings& settings) 84 | Constructs a port with default name and specified settings. 85 | */ 86 | Win_QextSerialPort::Win_QextSerialPort(const PortSettings& settings, QextSerialBase::QueryMode mode) { 87 | Win_Handle=INVALID_HANDLE_VALUE; 88 | setBaudRate(settings.BaudRate); 89 | setDataBits(settings.DataBits); 90 | setStopBits(settings.StopBits); 91 | setParity(settings.Parity); 92 | setFlowControl(settings.FlowControl); 93 | setTimeout(settings.Timeout_Millisec); 94 | setQueryMode(mode); 95 | init(); 96 | } 97 | 98 | /*! 99 | \fn Win_QextSerialPort::Win_QextSerialPort(const QString & name, const PortSettings& settings) 100 | Constructs a port with specified name and settings. 101 | */ 102 | Win_QextSerialPort::Win_QextSerialPort(const QString & name, const PortSettings& settings, QextSerialBase::QueryMode mode) { 103 | Win_Handle=INVALID_HANDLE_VALUE; 104 | setPortName(name); 105 | setBaudRate(settings.BaudRate); 106 | setDataBits(settings.DataBits); 107 | setStopBits(settings.StopBits); 108 | setParity(settings.Parity); 109 | setFlowControl(settings.FlowControl); 110 | setTimeout(settings.Timeout_Millisec); 111 | setQueryMode(mode); 112 | init(); 113 | } 114 | 115 | void Win_QextSerialPort::init() 116 | { 117 | _bytesToWrite = 0; 118 | overlap.Internal = 0; 119 | overlap.InternalHigh = 0; 120 | overlap.Offset = 0; 121 | overlap.OffsetHigh = 0; 122 | overlap.hEvent = CreateEvent(NULL, true, false, NULL); 123 | overlapThread = new Win_QextSerialThread(this); 124 | bytesToWriteLock = new QReadWriteLock; 125 | } 126 | 127 | /*! 128 | \fn Win_QextSerialPort::~Win_QextSerialPort() 129 | Standard destructor. 130 | */ 131 | Win_QextSerialPort::~Win_QextSerialPort() { 132 | if (isOpen()) { 133 | close(); 134 | } 135 | CloseHandle(overlap.hEvent); 136 | delete overlapThread; 137 | delete bytesToWriteLock; 138 | } 139 | 140 | /*! 141 | \fn Win_QextSerialPort& Win_QextSerialPort::operator=(const Win_QextSerialPort& s) 142 | overrides the = operator 143 | */ 144 | Win_QextSerialPort& Win_QextSerialPort::operator=(const Win_QextSerialPort& s) { 145 | setOpenMode(s.openMode()); 146 | _queryMode = s._queryMode; 147 | _bytesToWrite = s._bytesToWrite; 148 | bytesToWriteLock = new QReadWriteLock; 149 | overlapThread = new Win_QextSerialThread(this); 150 | memcpy(& overlap, & s.overlap, sizeof(OVERLAPPED)); 151 | memcpy(& overlapWrite, & s.overlapWrite, sizeof(OVERLAPPED)); 152 | lastErr=s.lastErr; 153 | port = s.port; 154 | Settings.FlowControl=s.Settings.FlowControl; 155 | Settings.Parity=s.Settings.Parity; 156 | Settings.DataBits=s.Settings.DataBits; 157 | Settings.StopBits=s.Settings.StopBits; 158 | Settings.BaudRate=s.Settings.BaudRate; 159 | Win_Handle=s.Win_Handle; 160 | memcpy(&Win_CommConfig, &s.Win_CommConfig, sizeof(COMMCONFIG)); 161 | memcpy(&Win_CommTimeouts, &s.Win_CommTimeouts, sizeof(COMMTIMEOUTS)); 162 | if (s.overlapThread->isRunning()) 163 | overlapThread->start(); 164 | return *this; 165 | } 166 | 167 | 168 | /*! 169 | \fn bool Win_QextSerialPort::open(OpenMode mode) 170 | Opens a serial port. Note that this function does not specify which device to open. If you need 171 | to open a device by name, see Win_QextSerialPort::open(const char*). This function has no effect 172 | if the port associated with the class is already open. The port is also configured to the current 173 | settings, as stored in the Settings structure. 174 | */ 175 | bool Win_QextSerialPort::open(OpenMode mode) { 176 | unsigned long confSize = sizeof(COMMCONFIG); 177 | Win_CommConfig.dwSize = confSize; 178 | DWORD dwFlagsAndAttributes = 0; 179 | if (queryMode() == QextSerialBase::EventDriven) 180 | dwFlagsAndAttributes += FILE_FLAG_OVERLAPPED; 181 | 182 | LOCK_MUTEX(); 183 | if (mode == QIODevice::NotOpen) 184 | return isOpen(); 185 | if (!isOpen()) { 186 | /*open the port*/ 187 | Win_Handle=CreateFileA(port.toLatin1(), GENERIC_READ|GENERIC_WRITE, 188 | FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, dwFlagsAndAttributes, NULL); 189 | if (Win_Handle!=INVALID_HANDLE_VALUE) { 190 | /*configure port settings*/ 191 | GetCommConfig(Win_Handle, &Win_CommConfig, &confSize); 192 | GetCommState(Win_Handle, &(Win_CommConfig.dcb)); 193 | 194 | /*set up parameters*/ 195 | Win_CommConfig.dcb.fBinary=TRUE; 196 | Win_CommConfig.dcb.fInX=FALSE; 197 | Win_CommConfig.dcb.fOutX=FALSE; 198 | Win_CommConfig.dcb.fAbortOnError=FALSE; 199 | Win_CommConfig.dcb.fNull=FALSE; 200 | setBaudRate(Settings.BaudRate); 201 | setDataBits(Settings.DataBits); 202 | setStopBits(Settings.StopBits); 203 | setParity(Settings.Parity); 204 | setFlowControl(Settings.FlowControl); 205 | setTimeout(Settings.Timeout_Millisec); 206 | SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG)); 207 | 208 | //init event driven approach 209 | if (queryMode() == QextSerialBase::EventDriven) { 210 | Win_CommTimeouts.ReadIntervalTimeout = MAXDWORD; 211 | Win_CommTimeouts.ReadTotalTimeoutMultiplier = 0; 212 | Win_CommTimeouts.ReadTotalTimeoutConstant = 0; 213 | Win_CommTimeouts.WriteTotalTimeoutMultiplier = 0; 214 | Win_CommTimeouts.WriteTotalTimeoutConstant = 0; 215 | SetCommTimeouts(Win_Handle, &Win_CommTimeouts); 216 | if (!SetCommMask( Win_Handle, EV_TXEMPTY | EV_RXCHAR | EV_DSR)) { 217 | qWarning("Failed to set Comm Mask. Error code: %ld", GetLastError()); 218 | UNLOCK_MUTEX(); 219 | return false; 220 | } 221 | overlapThread->start(); 222 | } 223 | QIODevice::open(mode); 224 | } 225 | } else { 226 | UNLOCK_MUTEX(); 227 | return false; 228 | } 229 | UNLOCK_MUTEX(); 230 | return isOpen(); 231 | } 232 | 233 | /*! 234 | \fn void Win_QextSerialPort::close() 235 | Closes a serial port. This function has no effect if the serial port associated with the class 236 | is not currently open. 237 | */ 238 | void Win_QextSerialPort::close() 239 | { 240 | LOCK_MUTEX(); 241 | 242 | if (isOpen()) { 243 | flush(); 244 | if (overlapThread->isRunning()) { 245 | overlapThread->stop(); 246 | if (QThread::currentThread() != overlapThread) 247 | overlapThread->wait(); 248 | } 249 | if (CloseHandle(Win_Handle)) 250 | Win_Handle = INVALID_HANDLE_VALUE; 251 | _bytesToWrite = 0; 252 | QIODevice::close(); 253 | } 254 | 255 | UNLOCK_MUTEX(); 256 | } 257 | 258 | /*! 259 | \fn void Win_QextSerialPort::flush() 260 | Flushes all pending I/O to the serial port. This function has no effect if the serial port 261 | associated with the class is not currently open. 262 | */ 263 | void Win_QextSerialPort::flush() { 264 | LOCK_MUTEX(); 265 | if (isOpen()) { 266 | FlushFileBuffers(Win_Handle); 267 | } 268 | UNLOCK_MUTEX(); 269 | } 270 | 271 | /*! 272 | \fn qint64 Win_QextSerialPort::size() const 273 | This function will return the number of bytes waiting in the receive queue of the serial port. 274 | It is included primarily to provide a complete QIODevice interface, and will not record errors 275 | in the lastErr member (because it is const). This function is also not thread-safe - in 276 | multithreading situations, use Win_QextSerialPort::bytesAvailable() instead. 277 | */ 278 | qint64 Win_QextSerialPort::size() const { 279 | int availBytes; 280 | COMSTAT Win_ComStat; 281 | DWORD Win_ErrorMask=0; 282 | ClearCommError(Win_Handle, &Win_ErrorMask, &Win_ComStat); 283 | availBytes = Win_ComStat.cbInQue; 284 | return (qint64)availBytes; 285 | } 286 | 287 | /*! 288 | \fn qint64 Win_QextSerialPort::bytesAvailable() 289 | Returns the number of bytes waiting in the port's receive queue. This function will return 0 if 290 | the port is not currently open, or -1 on error. Error information can be retrieved by calling 291 | Win_QextSerialPort::getLastError(). 292 | */ 293 | qint64 Win_QextSerialPort::bytesAvailable() { 294 | LOCK_MUTEX(); 295 | if (isOpen()) { 296 | DWORD Errors; 297 | COMSTAT Status; 298 | bool success=ClearCommError(Win_Handle, &Errors, &Status); 299 | translateError(Errors); 300 | if (success) { 301 | lastErr=E_NO_ERROR; 302 | UNLOCK_MUTEX(); 303 | return Status.cbInQue + QIODevice::bytesAvailable(); 304 | } 305 | UNLOCK_MUTEX(); 306 | return (unsigned int)-1; 307 | } 308 | UNLOCK_MUTEX(); 309 | return 0; 310 | } 311 | 312 | /*! 313 | \fn void Win_QextSerialPort::translateError(ulong error) 314 | Translates a system-specific error code to a QextSerialPort error code. Used internally. 315 | */ 316 | void Win_QextSerialPort::translateError(ulong error) { 317 | if (error&CE_BREAK) { 318 | lastErr=E_BREAK_CONDITION; 319 | } 320 | else if (error&CE_FRAME) { 321 | lastErr=E_FRAMING_ERROR; 322 | } 323 | else if (error&CE_IOE) { 324 | lastErr=E_IO_ERROR; 325 | } 326 | else if (error&CE_MODE) { 327 | lastErr=E_INVALID_FD; 328 | } 329 | else if (error&CE_OVERRUN) { 330 | lastErr=E_BUFFER_OVERRUN; 331 | } 332 | else if (error&CE_RXPARITY) { 333 | lastErr=E_RECEIVE_PARITY_ERROR; 334 | } 335 | else if (error&CE_RXOVER) { 336 | lastErr=E_RECEIVE_OVERFLOW; 337 | } 338 | else if (error&CE_TXFULL) { 339 | lastErr=E_TRANSMIT_OVERFLOW; 340 | } 341 | } 342 | 343 | /*! 344 | \fn qint64 Win_QextSerialPort::readData(char *data, qint64 maxSize) 345 | Reads a block of data from the serial port. This function will read at most maxlen bytes from 346 | the serial port and place them in the buffer pointed to by data. Return value is the number of 347 | bytes actually read, or -1 on error. 348 | 349 | \warning before calling this function ensure that serial port associated with this class 350 | is currently open (use isOpen() function to check if port is open). 351 | */ 352 | qint64 Win_QextSerialPort::readData(char *data, qint64 maxSize) 353 | { 354 | DWORD retVal; 355 | 356 | LOCK_MUTEX(); 357 | 358 | retVal = 0; 359 | if (queryMode() == QextSerialBase::EventDriven) { 360 | OVERLAPPED overlapRead; 361 | overlapRead.Internal = 0; 362 | overlapRead.InternalHigh = 0; 363 | overlapRead.Offset = 0; 364 | overlapRead.OffsetHigh = 0; 365 | overlapRead.hEvent = CreateEvent(NULL, true, false, NULL); 366 | if (!ReadFile(Win_Handle, (void*)data, (DWORD)maxSize, & retVal, & overlapRead)) { 367 | if (GetLastError() == ERROR_IO_PENDING) 368 | GetOverlappedResult(Win_Handle, & overlapRead, & retVal, true); 369 | else { 370 | lastErr = E_READ_FAILED; 371 | retVal = (DWORD)-1; 372 | } 373 | } 374 | CloseHandle(overlapRead.hEvent); 375 | } else if (!ReadFile(Win_Handle, (void*)data, (DWORD)maxSize, & retVal, NULL)) { 376 | lastErr = E_READ_FAILED; 377 | retVal = (DWORD)-1; 378 | } 379 | 380 | UNLOCK_MUTEX(); 381 | 382 | return (qint64)retVal; 383 | } 384 | 385 | /*! 386 | \fn qint64 Win_QextSerialPort::writeData(const char *data, qint64 maxSize) 387 | Writes a block of data to the serial port. This function will write len bytes 388 | from the buffer pointed to by data to the serial port. Return value is the number 389 | of bytes actually written, or -1 on error. 390 | 391 | \warning before calling this function ensure that serial port associated with this class 392 | is currently open (use isOpen() function to check if port is open). 393 | */ 394 | qint64 Win_QextSerialPort::writeData(const char *data, qint64 maxSize) 395 | { 396 | DWORD retVal; 397 | 398 | LOCK_MUTEX(); 399 | 400 | retVal = 0; 401 | if (queryMode() == QextSerialBase::EventDriven) { 402 | bytesToWriteLock->lockForWrite(); 403 | _bytesToWrite += maxSize; 404 | bytesToWriteLock->unlock(); 405 | overlapWrite.Internal = 0; 406 | overlapWrite.InternalHigh = 0; 407 | overlapWrite.Offset = 0; 408 | overlapWrite.OffsetHigh = 0; 409 | overlapWrite.hEvent = CreateEvent(NULL, true, false, NULL); 410 | if (!WriteFile(Win_Handle, (void*)data, (DWORD)maxSize, & retVal, & overlapWrite)) { 411 | lastErr = E_WRITE_FAILED; 412 | retVal = (DWORD)-1; 413 | } else 414 | retVal = maxSize; 415 | } else if (!WriteFile(Win_Handle, (void*)data, (DWORD)maxSize, & retVal, NULL)) { 416 | lastErr = E_WRITE_FAILED; 417 | retVal = (DWORD)-1; 418 | } 419 | 420 | UNLOCK_MUTEX(); 421 | 422 | return (qint64)retVal; 423 | } 424 | 425 | /*! 426 | \fn void Win_QextSerialPort::ungetChar(char c) 427 | This function is included to implement the full QIODevice interface, and currently has no 428 | purpose within this class. This function is meaningless on an unbuffered device and currently 429 | only prints a warning message to that effect. 430 | */ 431 | void Win_QextSerialPort::ungetChar(char c) { 432 | 433 | /*meaningless on unbuffered sequential device - return error and print a warning*/ 434 | TTY_WARNING("Win_QextSerialPort: ungetChar() called on an unbuffered sequential device - operation is meaningless"); 435 | } 436 | 437 | /*! 438 | \fn void Win_QextSerialPort::setFlowControl(FlowType flow) 439 | Sets the flow control used by the port. Possible values of flow are: 440 | \verbatim 441 | FLOW_OFF No flow control 442 | FLOW_HARDWARE Hardware (RTS/CTS) flow control 443 | FLOW_XONXOFF Software (XON/XOFF) flow control 444 | \endverbatim 445 | */ 446 | void Win_QextSerialPort::setFlowControl(FlowType flow) { 447 | LOCK_MUTEX(); 448 | if (Settings.FlowControl!=flow) { 449 | Settings.FlowControl=flow; 450 | } 451 | if (isOpen()) { 452 | switch(flow) { 453 | 454 | /*no flow control*/ 455 | case FLOW_OFF: 456 | Win_CommConfig.dcb.fOutxCtsFlow=FALSE; 457 | Win_CommConfig.dcb.fRtsControl=RTS_CONTROL_DISABLE; 458 | Win_CommConfig.dcb.fInX=FALSE; 459 | Win_CommConfig.dcb.fOutX=FALSE; 460 | SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG)); 461 | break; 462 | 463 | /*software (XON/XOFF) flow control*/ 464 | case FLOW_XONXOFF: 465 | Win_CommConfig.dcb.fOutxCtsFlow=FALSE; 466 | Win_CommConfig.dcb.fRtsControl=RTS_CONTROL_DISABLE; 467 | Win_CommConfig.dcb.fInX=TRUE; 468 | Win_CommConfig.dcb.fOutX=TRUE; 469 | SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG)); 470 | break; 471 | 472 | case FLOW_HARDWARE: 473 | Win_CommConfig.dcb.fOutxCtsFlow=TRUE; 474 | Win_CommConfig.dcb.fRtsControl=RTS_CONTROL_HANDSHAKE; 475 | Win_CommConfig.dcb.fInX=FALSE; 476 | Win_CommConfig.dcb.fOutX=FALSE; 477 | SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG)); 478 | break; 479 | } 480 | } 481 | UNLOCK_MUTEX(); 482 | } 483 | 484 | /*! 485 | \fn void Win_QextSerialPort::setParity(ParityType parity) 486 | Sets the parity associated with the serial port. The possible values of parity are: 487 | \verbatim 488 | PAR_SPACE Space Parity 489 | PAR_MARK Mark Parity 490 | PAR_NONE No Parity 491 | PAR_EVEN Even Parity 492 | PAR_ODD Odd Parity 493 | \endverbatim 494 | */ 495 | void Win_QextSerialPort::setParity(ParityType parity) { 496 | LOCK_MUTEX(); 497 | if (Settings.Parity!=parity) { 498 | Settings.Parity=parity; 499 | } 500 | if (isOpen()) { 501 | Win_CommConfig.dcb.Parity=(unsigned char)parity; 502 | switch (parity) { 503 | 504 | /*space parity*/ 505 | case PAR_SPACE: 506 | if (Settings.DataBits==DATA_8) { 507 | TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: Space parity with 8 data bits is not supported by POSIX systems."); 508 | } 509 | Win_CommConfig.dcb.fParity=TRUE; 510 | break; 511 | 512 | /*mark parity - WINDOWS ONLY*/ 513 | case PAR_MARK: 514 | TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: Mark parity is not supported by POSIX systems"); 515 | Win_CommConfig.dcb.fParity=TRUE; 516 | break; 517 | 518 | /*no parity*/ 519 | case PAR_NONE: 520 | Win_CommConfig.dcb.fParity=FALSE; 521 | break; 522 | 523 | /*even parity*/ 524 | case PAR_EVEN: 525 | Win_CommConfig.dcb.fParity=TRUE; 526 | break; 527 | 528 | /*odd parity*/ 529 | case PAR_ODD: 530 | Win_CommConfig.dcb.fParity=TRUE; 531 | break; 532 | } 533 | SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG)); 534 | } 535 | UNLOCK_MUTEX(); 536 | } 537 | 538 | /*! 539 | \fn void Win_QextSerialPort::setDataBits(DataBitsType dataBits) 540 | Sets the number of data bits used by the serial port. Possible values of dataBits are: 541 | \verbatim 542 | DATA_5 5 data bits 543 | DATA_6 6 data bits 544 | DATA_7 7 data bits 545 | DATA_8 8 data bits 546 | \endverbatim 547 | 548 | \note 549 | This function is subject to the following restrictions: 550 | \par 551 | 5 data bits cannot be used with 2 stop bits. 552 | \par 553 | 1.5 stop bits can only be used with 5 data bits. 554 | \par 555 | 8 data bits cannot be used with space parity on POSIX systems. 556 | 557 | */ 558 | void Win_QextSerialPort::setDataBits(DataBitsType dataBits) { 559 | LOCK_MUTEX(); 560 | if (Settings.DataBits!=dataBits) { 561 | if ((Settings.StopBits==STOP_2 && dataBits==DATA_5) || 562 | (Settings.StopBits==STOP_1_5 && dataBits!=DATA_5)) { 563 | } 564 | else { 565 | Settings.DataBits=dataBits; 566 | } 567 | } 568 | if (isOpen()) { 569 | switch(dataBits) { 570 | 571 | /*5 data bits*/ 572 | case DATA_5: 573 | if (Settings.StopBits==STOP_2) { 574 | TTY_WARNING("Win_QextSerialPort: 5 Data bits cannot be used with 2 stop bits."); 575 | } 576 | else { 577 | Win_CommConfig.dcb.ByteSize=5; 578 | SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG)); 579 | } 580 | break; 581 | 582 | /*6 data bits*/ 583 | case DATA_6: 584 | if (Settings.StopBits==STOP_1_5) { 585 | TTY_WARNING("Win_QextSerialPort: 6 Data bits cannot be used with 1.5 stop bits."); 586 | } 587 | else { 588 | Win_CommConfig.dcb.ByteSize=6; 589 | SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG)); 590 | } 591 | break; 592 | 593 | /*7 data bits*/ 594 | case DATA_7: 595 | if (Settings.StopBits==STOP_1_5) { 596 | TTY_WARNING("Win_QextSerialPort: 7 Data bits cannot be used with 1.5 stop bits."); 597 | } 598 | else { 599 | Win_CommConfig.dcb.ByteSize=7; 600 | SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG)); 601 | } 602 | break; 603 | 604 | /*8 data bits*/ 605 | case DATA_8: 606 | if (Settings.StopBits==STOP_1_5) { 607 | TTY_WARNING("Win_QextSerialPort: 8 Data bits cannot be used with 1.5 stop bits."); 608 | } 609 | else { 610 | Win_CommConfig.dcb.ByteSize=8; 611 | SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG)); 612 | } 613 | break; 614 | } 615 | } 616 | UNLOCK_MUTEX(); 617 | } 618 | 619 | /*! 620 | \fn void Win_QextSerialPort::setStopBits(StopBitsType stopBits) 621 | Sets the number of stop bits used by the serial port. Possible values of stopBits are: 622 | \verbatim 623 | STOP_1 1 stop bit 624 | STOP_1_5 1.5 stop bits 625 | STOP_2 2 stop bits 626 | \endverbatim 627 | 628 | \note 629 | This function is subject to the following restrictions: 630 | \par 631 | 2 stop bits cannot be used with 5 data bits. 632 | \par 633 | 1.5 stop bits cannot be used with 6 or more data bits. 634 | \par 635 | POSIX does not support 1.5 stop bits. 636 | */ 637 | void Win_QextSerialPort::setStopBits(StopBitsType stopBits) { 638 | LOCK_MUTEX(); 639 | if (Settings.StopBits!=stopBits) { 640 | if ((Settings.DataBits==DATA_5 && stopBits==STOP_2) || 641 | (stopBits==STOP_1_5 && Settings.DataBits!=DATA_5)) { 642 | } 643 | else { 644 | Settings.StopBits=stopBits; 645 | } 646 | } 647 | if (isOpen()) { 648 | switch (stopBits) { 649 | 650 | /*one stop bit*/ 651 | case STOP_1: 652 | Win_CommConfig.dcb.StopBits=ONESTOPBIT; 653 | SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG)); 654 | break; 655 | 656 | /*1.5 stop bits*/ 657 | case STOP_1_5: 658 | TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: 1.5 stop bit operation is not supported by POSIX."); 659 | if (Settings.DataBits!=DATA_5) { 660 | TTY_WARNING("Win_QextSerialPort: 1.5 stop bits can only be used with 5 data bits"); 661 | } 662 | else { 663 | Win_CommConfig.dcb.StopBits=ONE5STOPBITS; 664 | SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG)); 665 | } 666 | break; 667 | 668 | /*two stop bits*/ 669 | case STOP_2: 670 | if (Settings.DataBits==DATA_5) { 671 | TTY_WARNING("Win_QextSerialPort: 2 stop bits cannot be used with 5 data bits"); 672 | } 673 | else { 674 | Win_CommConfig.dcb.StopBits=TWOSTOPBITS; 675 | SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG)); 676 | } 677 | break; 678 | } 679 | } 680 | UNLOCK_MUTEX(); 681 | } 682 | 683 | /*! 684 | \fn void Win_QextSerialPort::setBaudRate(BaudRateType baudRate) 685 | Sets the baud rate of the serial port. Note that not all rates are applicable on 686 | all platforms. The following table shows translations of the various baud rate 687 | constants on Windows(including NT/2000) and POSIX platforms. Speeds marked with an * 688 | are speeds that are usable on both Windows and POSIX. 689 | \verbatim 690 | 691 | RATE Windows Speed POSIX Speed 692 | ----------- ------------- ----------- 693 | BAUD50 110 50 694 | BAUD75 110 75 695 | *BAUD110 110 110 696 | BAUD134 110 134.5 697 | BAUD150 110 150 698 | BAUD200 110 200 699 | *BAUD300 300 300 700 | *BAUD600 600 600 701 | *BAUD1200 1200 1200 702 | BAUD1800 1200 1800 703 | *BAUD2400 2400 2400 704 | *BAUD4800 4800 4800 705 | *BAUD9600 9600 9600 706 | BAUD14400 14400 9600 707 | *BAUD19200 19200 19200 708 | *BAUD38400 38400 38400 709 | BAUD56000 56000 38400 710 | *BAUD57600 57600 57600 711 | BAUD76800 57600 76800 712 | *BAUD115200 115200 115200 713 | BAUD128000 128000 115200 714 | BAUD256000 256000 115200 715 | \endverbatim 716 | */ 717 | void Win_QextSerialPort::setBaudRate(BaudRateType baudRate) { 718 | LOCK_MUTEX(); 719 | if (Settings.BaudRate!=baudRate) { 720 | switch (baudRate) { 721 | case BAUD50: 722 | case BAUD75: 723 | case BAUD134: 724 | case BAUD150: 725 | case BAUD200: 726 | Settings.BaudRate=BAUD110; 727 | break; 728 | 729 | case BAUD1800: 730 | Settings.BaudRate=BAUD1200; 731 | break; 732 | 733 | case BAUD76800: 734 | Settings.BaudRate=BAUD57600; 735 | break; 736 | 737 | default: 738 | Settings.BaudRate=baudRate; 739 | break; 740 | } 741 | } 742 | if (isOpen()) { 743 | switch (baudRate) { 744 | 745 | /*50 baud*/ 746 | case BAUD50: 747 | TTY_WARNING("Win_QextSerialPort: Windows does not support 50 baud operation. Switching to 110 baud."); 748 | Win_CommConfig.dcb.BaudRate=CBR_110; 749 | break; 750 | 751 | /*75 baud*/ 752 | case BAUD75: 753 | TTY_WARNING("Win_QextSerialPort: Windows does not support 75 baud operation. Switching to 110 baud."); 754 | Win_CommConfig.dcb.BaudRate=CBR_110; 755 | break; 756 | 757 | /*110 baud*/ 758 | case BAUD110: 759 | Win_CommConfig.dcb.BaudRate=CBR_110; 760 | break; 761 | 762 | /*134.5 baud*/ 763 | case BAUD134: 764 | TTY_WARNING("Win_QextSerialPort: Windows does not support 134.5 baud operation. Switching to 110 baud."); 765 | Win_CommConfig.dcb.BaudRate=CBR_110; 766 | break; 767 | 768 | /*150 baud*/ 769 | case BAUD150: 770 | TTY_WARNING("Win_QextSerialPort: Windows does not support 150 baud operation. Switching to 110 baud."); 771 | Win_CommConfig.dcb.BaudRate=CBR_110; 772 | break; 773 | 774 | /*200 baud*/ 775 | case BAUD200: 776 | TTY_WARNING("Win_QextSerialPort: Windows does not support 200 baud operation. Switching to 110 baud."); 777 | Win_CommConfig.dcb.BaudRate=CBR_110; 778 | break; 779 | 780 | /*300 baud*/ 781 | case BAUD300: 782 | Win_CommConfig.dcb.BaudRate=CBR_300; 783 | break; 784 | 785 | /*600 baud*/ 786 | case BAUD600: 787 | Win_CommConfig.dcb.BaudRate=CBR_600; 788 | break; 789 | 790 | /*1200 baud*/ 791 | case BAUD1200: 792 | Win_CommConfig.dcb.BaudRate=CBR_1200; 793 | break; 794 | 795 | /*1800 baud*/ 796 | case BAUD1800: 797 | TTY_WARNING("Win_QextSerialPort: Windows does not support 1800 baud operation. Switching to 1200 baud."); 798 | Win_CommConfig.dcb.BaudRate=CBR_1200; 799 | break; 800 | 801 | /*2400 baud*/ 802 | case BAUD2400: 803 | Win_CommConfig.dcb.BaudRate=CBR_2400; 804 | break; 805 | 806 | /*4800 baud*/ 807 | case BAUD4800: 808 | Win_CommConfig.dcb.BaudRate=CBR_4800; 809 | break; 810 | 811 | /*9600 baud*/ 812 | case BAUD9600: 813 | Win_CommConfig.dcb.BaudRate=CBR_9600; 814 | break; 815 | 816 | /*14400 baud*/ 817 | case BAUD14400: 818 | TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: POSIX does not support 14400 baud operation."); 819 | Win_CommConfig.dcb.BaudRate=CBR_14400; 820 | break; 821 | 822 | /*19200 baud*/ 823 | case BAUD19200: 824 | Win_CommConfig.dcb.BaudRate=CBR_19200; 825 | break; 826 | 827 | /*38400 baud*/ 828 | case BAUD38400: 829 | Win_CommConfig.dcb.BaudRate=CBR_38400; 830 | break; 831 | 832 | /*56000 baud*/ 833 | case BAUD56000: 834 | TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: POSIX does not support 56000 baud operation."); 835 | Win_CommConfig.dcb.BaudRate=CBR_56000; 836 | break; 837 | 838 | /*57600 baud*/ 839 | case BAUD57600: 840 | Win_CommConfig.dcb.BaudRate=CBR_57600; 841 | break; 842 | 843 | /*76800 baud*/ 844 | case BAUD76800: 845 | TTY_WARNING("Win_QextSerialPort: Windows does not support 76800 baud operation. Switching to 57600 baud."); 846 | Win_CommConfig.dcb.BaudRate=CBR_57600; 847 | break; 848 | 849 | /*115200 baud*/ 850 | case BAUD115200: 851 | Win_CommConfig.dcb.BaudRate=CBR_115200; 852 | break; 853 | 854 | /*128000 baud*/ 855 | case BAUD128000: 856 | TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: POSIX does not support 128000 baud operation."); 857 | Win_CommConfig.dcb.BaudRate=CBR_128000; 858 | break; 859 | 860 | /*256000 baud*/ 861 | case BAUD256000: 862 | TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: POSIX does not support 256000 baud operation."); 863 | Win_CommConfig.dcb.BaudRate=CBR_256000; 864 | break; 865 | } 866 | SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG)); 867 | } 868 | UNLOCK_MUTEX(); 869 | } 870 | 871 | /*! 872 | \fn void Win_QextSerialPort::setDtr(bool set) 873 | Sets DTR line to the requested state (high by default). This function will have no effect if 874 | the port associated with the class is not currently open. 875 | */ 876 | void Win_QextSerialPort::setDtr(bool set) { 877 | LOCK_MUTEX(); 878 | if (isOpen()) { 879 | if (set) { 880 | EscapeCommFunction(Win_Handle, SETDTR); 881 | } 882 | else { 883 | EscapeCommFunction(Win_Handle, CLRDTR); 884 | } 885 | } 886 | UNLOCK_MUTEX(); 887 | } 888 | 889 | /*! 890 | \fn void Win_QextSerialPort::setRts(bool set) 891 | Sets RTS line to the requested state (high by default). This function will have no effect if 892 | the port associated with the class is not currently open. 893 | */ 894 | void Win_QextSerialPort::setRts(bool set) { 895 | LOCK_MUTEX(); 896 | if (isOpen()) { 897 | if (set) { 898 | EscapeCommFunction(Win_Handle, SETRTS); 899 | } 900 | else { 901 | EscapeCommFunction(Win_Handle, CLRRTS); 902 | } 903 | } 904 | UNLOCK_MUTEX(); 905 | } 906 | 907 | /*! 908 | \fn ulong Win_QextSerialPort::lineStatus(void) 909 | returns the line status as stored by the port function. This function will retrieve the states 910 | of the following lines: DCD, CTS, DSR, and RI. On POSIX systems, the following additional lines 911 | can be monitored: DTR, RTS, Secondary TXD, and Secondary RXD. The value returned is an unsigned 912 | long with specific bits indicating which lines are high. The following constants should be used 913 | to examine the states of individual lines: 914 | 915 | \verbatim 916 | Mask Line 917 | ------ ---- 918 | LS_CTS CTS 919 | LS_DSR DSR 920 | LS_DCD DCD 921 | LS_RI RI 922 | \endverbatim 923 | 924 | This function will return 0 if the port associated with the class is not currently open. 925 | */ 926 | ulong Win_QextSerialPort::lineStatus(void) { 927 | unsigned long Status=0, Temp=0; 928 | LOCK_MUTEX(); 929 | if (isOpen()) { 930 | GetCommModemStatus(Win_Handle, &Temp); 931 | if (Temp&MS_CTS_ON) { 932 | Status|=LS_CTS; 933 | } 934 | if (Temp&MS_DSR_ON) { 935 | Status|=LS_DSR; 936 | } 937 | if (Temp&MS_RING_ON) { 938 | Status|=LS_RI; 939 | } 940 | if (Temp&MS_RLSD_ON) { 941 | Status|=LS_DCD; 942 | } 943 | } 944 | UNLOCK_MUTEX(); 945 | return Status; 946 | } 947 | 948 | bool Win_QextSerialPort::waitForReadyRead(int msecs) 949 | { 950 | //@todo implement 951 | return false; 952 | } 953 | 954 | qint64 Win_QextSerialPort::bytesToWrite() const 955 | { 956 | return _bytesToWrite; 957 | } 958 | 959 | void Win_QextSerialPort::monitorCommEvent() 960 | { 961 | DWORD eventMask = 0; 962 | 963 | ResetEvent(overlap.hEvent); 964 | if (!WaitCommEvent(Win_Handle, & eventMask, & overlap)) 965 | if (GetLastError() != ERROR_IO_PENDING) 966 | qCritical("WaitCommEvent error %ld\n", GetLastError()); 967 | 968 | if (WaitForSingleObject(overlap.hEvent, INFINITE) == WAIT_OBJECT_0) { 969 | //overlap event occured 970 | DWORD undefined; 971 | if (!GetOverlappedResult(Win_Handle, & overlap, & undefined, false)) { 972 | qWarning("Comm event overlapped error %ld", GetLastError()); 973 | return; 974 | } 975 | if (eventMask & EV_RXCHAR) { 976 | if (sender() != this) 977 | emit readyRead(); 978 | } 979 | if (eventMask & EV_TXEMPTY) { 980 | DWORD numBytes; 981 | GetOverlappedResult(Win_Handle, & overlapWrite, & numBytes, true); 982 | bytesToWriteLock->lockForWrite(); 983 | if (sender() != this) 984 | emit bytesWritten(bytesToWrite()); 985 | _bytesToWrite = 0; 986 | bytesToWriteLock->unlock(); 987 | } 988 | if (eventMask & EV_DSR) 989 | if (lineStatus() & LS_DSR) 990 | emit dsrChanged(true); 991 | else 992 | emit dsrChanged(false); 993 | } 994 | } 995 | 996 | void Win_QextSerialPort::terminateCommWait() 997 | { 998 | SetCommMask(Win_Handle, 0); 999 | } 1000 | 1001 | 1002 | /*! 1003 | \fn void Win_QextSerialPort::setTimeout(ulong millisec); 1004 | Sets the read and write timeouts for the port to millisec milliseconds. 1005 | Setting 0 for both sec and millisec indicates that timeouts are not used for read nor 1006 | write operations. Setting -1 indicates that read and write should return immediately. 1007 | 1008 | \note this function does nothing in event driven mode. 1009 | */ 1010 | void Win_QextSerialPort::setTimeout(long millisec) { 1011 | LOCK_MUTEX(); 1012 | Settings.Timeout_Millisec = millisec; 1013 | 1014 | if (millisec == -1) { 1015 | Win_CommTimeouts.ReadIntervalTimeout = MAXDWORD; 1016 | Win_CommTimeouts.ReadTotalTimeoutConstant = 0; 1017 | } else { 1018 | Win_CommTimeouts.ReadIntervalTimeout = millisec; 1019 | Win_CommTimeouts.ReadTotalTimeoutConstant = millisec; 1020 | } 1021 | Win_CommTimeouts.ReadTotalTimeoutMultiplier = 0; 1022 | Win_CommTimeouts.WriteTotalTimeoutMultiplier = millisec; 1023 | Win_CommTimeouts.WriteTotalTimeoutConstant = 0; 1024 | if (queryMode() != QextSerialBase::EventDriven) 1025 | SetCommTimeouts(Win_Handle, &Win_CommTimeouts); 1026 | 1027 | UNLOCK_MUTEX(); 1028 | } 1029 | 1030 | 1031 | Win_QextSerialThread::Win_QextSerialThread(Win_QextSerialPort * qesp): 1032 | QThread() 1033 | { 1034 | this->qesp = qesp; 1035 | terminate = false; 1036 | } 1037 | 1038 | void Win_QextSerialThread::stop() 1039 | { 1040 | terminate = true; 1041 | qesp->terminateCommWait(); 1042 | } 1043 | 1044 | void Win_QextSerialThread::run() 1045 | { 1046 | while (!terminate) 1047 | qesp->monitorCommEvent(); 1048 | terminate = false; 1049 | } 1050 | -------------------------------------------------------------------------------- /win_qextserialport.h: -------------------------------------------------------------------------------- 1 | #ifndef _WIN_QEXTSERIALPORT_H_ 2 | #define _WIN_QEXTSERIALPORT_H_ 3 | 4 | #include "qextserialbase.h" 5 | #include 6 | #include 7 | 8 | 9 | /*if all warning messages are turned off, flag portability warnings to be turned off as well*/ 10 | #ifdef _TTY_NOWARN_ 11 | #define _TTY_NOWARN_PORT_ 12 | #endif 13 | 14 | class QReadWriteLock; 15 | class Win_QextSerialThread; 16 | 17 | 18 | /*! 19 | \author Stefan Sander 20 | \author Michal Policht 21 | 22 | A cross-platform serial port class. 23 | This class encapsulates the Windows portion of QextSerialPort. The user will be notified of 24 | errors and possible portability conflicts at run-time by default - this behavior can be turned 25 | off by defining _TTY_NOWARN_ (to turn off all warnings) or _TTY_NOWARN_PORT_ (to turn off 26 | portability warnings) in the project. Note that defining _TTY_NOWARN_ also defines 27 | _TTY_NOWARN_PORT_. 28 | 29 | \note 30 | On Windows NT/2000/XP this class uses Win32 serial port functions by default. The user may 31 | select POSIX behavior under NT, 2000, or XP ONLY by defining _TTY_POSIX_ in the project. I can 32 | make no guarantees as to the quality of POSIX support under NT/2000 however. 33 | 34 | \todo remove copy constructor and assign operator. 35 | */ 36 | class Win_QextSerialPort: public QextSerialBase 37 | { 38 | Q_OBJECT 39 | friend class Win_QextSerialThread; 40 | 41 | private: 42 | /*! 43 | * This method is a part of constructor. 44 | */ 45 | void init(); 46 | 47 | protected: 48 | HANDLE Win_Handle; 49 | HANDLE threadStartEvent; 50 | HANDLE threadTerminateEvent; 51 | OVERLAPPED overlap; 52 | OVERLAPPED overlapWrite; 53 | COMMCONFIG Win_CommConfig; 54 | COMMTIMEOUTS Win_CommTimeouts; 55 | QReadWriteLock * bytesToWriteLock; ///< @todo maybe move to QextSerialBase. 56 | qint64 _bytesToWrite; ///< @todo maybe move to QextSerialBase (and implement in POSIX). 57 | Win_QextSerialThread * overlapThread; ///< @todo maybe move to QextSerialBase (and implement in POSIX). 58 | 59 | void monitorCommEvent(); 60 | void terminateCommWait(); 61 | virtual qint64 readData(char *data, qint64 maxSize); 62 | virtual qint64 writeData(const char *data, qint64 maxSize); 63 | 64 | public: 65 | Win_QextSerialPort(); 66 | Win_QextSerialPort(Win_QextSerialPort const& s); 67 | Win_QextSerialPort(const QString & name, QextSerialBase::QueryMode mode = QextSerialBase::Polling); 68 | Win_QextSerialPort(const PortSettings& settings, QextSerialBase::QueryMode mode = QextSerialBase::Polling); 69 | Win_QextSerialPort(const QString & name, const PortSettings& settings, QextSerialBase::QueryMode mode = QextSerialBase::Polling); 70 | Win_QextSerialPort& operator=(const Win_QextSerialPort& s); 71 | virtual ~Win_QextSerialPort(); 72 | virtual bool open(OpenMode mode); 73 | virtual void close(); 74 | virtual void flush(); 75 | virtual qint64 size() const; 76 | virtual void ungetChar(char c); 77 | virtual void setFlowControl(FlowType); 78 | virtual void setParity(ParityType); 79 | virtual void setDataBits(DataBitsType); 80 | virtual void setStopBits(StopBitsType); 81 | virtual void setBaudRate(BaudRateType); 82 | virtual void setDtr(bool set=true); 83 | virtual void setRts(bool set=true); 84 | virtual ulong lineStatus(void); 85 | virtual qint64 bytesAvailable(); 86 | virtual void translateError(ulong); 87 | virtual void setTimeout(long); 88 | 89 | /*! 90 | * Return number of bytes waiting in the buffer. Currently this shows number 91 | * of bytes queued within write() and before the TX_EMPTY event occured. TX_EMPTY 92 | * event is created whenever last character in the system buffer was sent. 93 | * 94 | * \return number of bytes queued within write(), before the first TX_EMPTY 95 | * event occur. 96 | * 97 | * \warning this function may not give you expected results since TX_EMPTY may occur 98 | * while writing data to the buffer. Eventually some TX_EMPTY events may not be 99 | * catched. 100 | * 101 | * \note this function always returns 0 in polling mode. 102 | * 103 | * \see flush(). 104 | */ 105 | virtual qint64 bytesToWrite() const; 106 | 107 | virtual bool waitForReadyRead(int msecs); ///< @todo implement. 108 | }; 109 | 110 | /*! 111 | * This thread monitors communication events. 112 | */ 113 | class Win_QextSerialThread: public QThread 114 | { 115 | Win_QextSerialPort * qesp; 116 | bool terminate; 117 | 118 | public: 119 | /*! 120 | * Constructor. 121 | * 122 | * \param qesp valid serial port object. 123 | */ 124 | Win_QextSerialThread(Win_QextSerialPort * qesp); 125 | 126 | /*! 127 | * Stop the thread. 128 | */ 129 | void stop(); 130 | 131 | protected: 132 | //overriden 133 | virtual void run(); 134 | 135 | }; 136 | 137 | #endif 138 | --------------------------------------------------------------------------------