Test a __HTML5 based on QT serial port programming under Linux

Source: Internet
Author: User

This article blog link: http://blog.csdn.net/jdh99, author: jdh, reprint please specify.


Environment:

Host: Fedora12

Development software: QT

Target board: MINI6410


Implementation features:

The target board receives the information which the PC serial port passes over and in the terminal output, the target board serial port receives the information uses the select mechanism


Source:

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> &nbSp #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;   };     //port information definition    typedef struct _port_info   {        int baud_rate;       int port_fd; &nbsp     char parity;       char stop_bit;       char flow_ctrl;       char data_bits;   }*port_info;     //Open serial port    int open_port (Char *port);  // Close serial port    void close_port (INT&NBSP;FD);  //baud rate set parameters based on baud rates    Int get_baud_ Rate (Unsigned long baud_rate);  //Set port parameters    Int set_port (port_info p_ info);  //through the serial port to send data, can only write compress_byte length data, send the file header "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;      //Open serial    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;        }  }     //Turn off the specified serial port    void close_port (INT&NBSP;FD)    {         close (FD);  }     //baud rate of the response based on the baud rate setting parameters    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;        }  }     //Set port    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);   &NBSP;&NBSP;&NBSp; tcgetattr (p_info->port_fd,&old_opt);          //set serial port baud rate        baud_rate = get_baud_rate (p_info->baud_rate);       //modifies the serial port input/output baud rate slot parameters in new_opt structure        cfsetispeed (&new_ opt,baud_rate);       cfsetospeed (&new_opt,baud_rate);           //Modify the control mode to ensure that the program will not occupy the serial port        new_opt.c_cflag |=  CLOCAL;       //modifies the control mode to enable reading of input data from the serial port        new _opt.c_cflag |= cread;          //Set Data flow control         switch  (P_info->flow_ctrl)        {        case  ' 0 ':           {               //does not use flow control                 new_opt.c_cflag &= ~CRTSCTS;                break;            }       case  ' 1 ':            {               / /Use hardware for flow control                new_opt.c_cflag  |= CRTSCTS;                break;           }       case   ' 2 ':           {                new_opt.c_cflag |= ixon | ixoff | ixany;               break;            }       }           //Set data bits        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;            }       }          //set parity bit         switch  (p_info->parity)        {       case  ' 0 ':           {                //does not use parity                 new_opt.c_cflag &= ~PARENB;                break;            }       case  ' 1 ':            {                //with parity                new_opt.c_ cflag |= parenb;                new_opt.c_cflag &= ~parodd;                break;           }        case  ' 2 ':           {                //using odd check                 new_opt.c_cflag |= parenb;                new_opt.c_cflag |= PARODD;                break;            }       }          //set Stop bit        if  (p_info->stop_bit&

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.