Linux下基於QT串口編程測試一__html5

來源:互聯網
上載者:User

本文部落格連結:http://blog.csdn.net/jdh99,作者:jdh,轉載請註明.


環境:

主機:Fedora12

開發軟體:QT

目標板:MINI6410


實現功能:

目標板接收PC串口傳過來的資訊並在終端輸出,目標板串口接收資訊用SELECT機制


原始碼:

widget.h:

[cpp] view plain copy #ifndef WIDGET_H   #define WIDGET_H      #include <QWidget>   #include <QDebug>   #include <QTimer>   #include <unistd.h>   #include <sys/types.h>   #include <fcntl.h>   #include <sys/stat.h>   #include <stdio.h>   #include <sys/param.h>   #include <QVector>   #include <QByteArray>   #include <QQueue>   #include <QSemaphore>   #include <iostream>   #include <QFile>   #include "QThread"   #include <QtGui>   #include <QMutex>   #include <QtNetwork>   #include <QUdpSocket>   #include <sys/ioctl.h>   #include <stdlib.h>   #include <stdio.h>   #include <linux/soundcard.h>   #include <alsa/asoundlib.h>   #include <QtGui/QMainWindow>   #include <QtGui/QDialog>   #include <QtGui/QPushButton>   #include <QtGui/QHBoxLayout>   #include <QtGui/QVBoxLayout>   #include <QtGui/QGridLayout>   #include <QTextCodec>   #include <QtGui/QToolButton>   #include <qsocketnotifier.h>   #include <QTimer>   #include <QtNetwork/QUdpSocket>   #include <iostream>   #include <qmessagebox.h>   #include <qstringlist.h>   #include <QtNetwork>   #include <QUdpSocket>   #include <QSound>   #include <QMap>   #include <sys/socket.h>   #include <arpa/inet.h>   #include <linux/soundcard.h>   #include "sys/select.h"   #include "termios.h"      namespace Ui {       class Widget;   }      class Widget : public QWidget   {       Q_OBJECT      public:       explicit Widget(QWidget *parent = 0);       ~Widget();      private:       Ui::Widget *ui;   };      //連接埠資訊定義   typedef struct _Port_Info   {       int baud_rate;       int port_fd;       char parity;       char stop_bit;       char flow_ctrl;       char data_bits;   }*Port_Info;      //開啟串口   int open_port(char *port);   //關閉串口   void close_port(int fd);   //根據傳輸速率獲得傳輸速率設定參數   int get_baud_rate(unsigned long baud_rate);   //設定連接埠參數   int set_port(Port_Info p_info);   //通過串口發送資料,只能寫COMPRESS_BYTE長度資料,發送時加檔案頭"JDH"   int send_data(int fd,char *data,int data_len);      #endif // WIDGET_H  
widget.c:

[cpp] view plain copy #include "widget.h"   #include "ui_widget.h"      int Fd_Com;   #define COM "/dev/ttySAC1"      char buffer_com[1024 + 10];   char buffer_read_com[1024];   int send_index;      //開啟串口   int open_port(char *port)   {       int fd;       if ((fd = open(port,O_RDWR | O_NOCTTY |O_NONBLOCK)) == -1)       {           perror("can not open com port!");           return -1;       }   }      //關閉指定串口   void close_port(int fd)   {       close(fd);   }      //根據傳輸速率獲得響應的傳輸速率設定參數   int get_baud_rate(unsigned long baud_rate)   {       switch (baud_rate)       {       case 2400:           return B2400;       case 4800:           return B4800;       case 9600:           return B9600;       case 19200:           return B19200;       case 38400:           return B38400;       case 57600:           return B57600;       case 115200:           return B115200;       case 230400:           return B230400;       default:           return -1;       }   }      //設定連接埠   int set_port(Port_Info p_info)   {       struct termios old_opt,new_opt;       int baud_rate,parity;          memset(&old_opt,0,sizeof(old_opt));       memset(&new_opt,0,sizeof(new_opt));          cfmakeraw(&new_opt);       tcgetattr(p_info->port_fd,&old_opt);          //設定串口傳輸速率       baud_rate = get_baud_rate(p_info->baud_rate);       //修改new_opt結構中的串口輸入/輸出傳輸速率槽參數       cfsetispeed(&new_opt,baud_rate);       cfsetospeed(&new_opt,baud_rate);          //修改控制模式,保證程式不會佔用串口       new_opt.c_cflag |= CLOCAL;       //修改控制模式,使得能夠從串口讀取輸入資料       new_opt.c_cflag |= CREAD;          //設定資料流控制       switch (p_info->flow_ctrl)       {       case '0':           {               //不使用流量控制               new_opt.c_cflag &= ~CRTSCTS;               break;           }       case '1':           {               //使用硬體進行流量控制               new_opt.c_cflag |= CRTSCTS;               break;           }       case '2':           {               new_opt.c_cflag |= IXON | IXOFF | IXANY;               break;           }       }          //設定資料位元       new_opt.c_cflag &= ~CSIZE;       switch (p_info->data_bits)       {       case '5':           {               new_opt.c_cflag |= CS5;               break;           }       case '6':           {               new_opt.c_cflag |= CS6;               break;           }       case '7':           {               new_opt.c_cflag |= CS7;               break;           }       case '8':           {               new_opt.c_cflag |= CS8;               break;           }       default:           {               new_opt.c_cflag |= CS8;               break;           }       }          //設定同位位元       switch (p_info->parity)       {       case '0':           {               //不使用同位               new_opt.c_cflag &= ~PARENB;               break;           }       case '1':           {               //使用偶校正               new_opt.c_cflag |= PARENB;               new_opt.c_cflag &= ~PARODD;               break;           }       case '2':           {               //使用奇數同位               new_opt.c_cflag |= PARENB;               new_opt.c_cflag |= PARODD;               break;           }       }          //設定停止位       if (p_info->stop_bit&

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.