|
- #include "widget.h"
- #include "ui_widget.h"
- #include<QTimer>
- #include<QThread>
- #define FALSE 0
- #define TRUE 1
- static int serial_fd;
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- ui->setupUi(this);
- serial_init();
- }
- Widget::~Widget()
- {
- delete ui;
-
- }
- void Widget::changeEvent(QEvent *e)
- {
- QWidget::changeEvent(e);
- switch (e->type()) {
- case QEvent::LanguageChange:
- ui->retranslateUi(this);
- break;
- default:
- break;
- }
- }
- void Widget::set_speed(int fd)
- {
- int status;
- struct termios Opt;
- struct termios oldOpt;
- tcgetattr(fd, &oldOpt);
- tcflush(fd, TCIOFLUSH);
- cfsetispeed(&Opt, B38400);
- cfsetospeed(&Opt, B38400);
- status = tcsetattr(fd, TCSANOW, &Opt);
- tcflush(fd, TCIOFLUSH);
- }
- int Widget::set_Parity(int fd)
- {
- struct termios options;
- struct termios oldoptions;
- if(tcgetattr(fd, &oldoptions) != 0)
- {
- ui->textBrowser->insertPlainText("tcgetattr");
- return(FALSE);
- }
- options.c_cflag |= (CLOCAL|CREAD);
- options.c_cflag &=~CSIZE;
- options.c_cflag |= CS8;
- options.c_cflag &= ~PARENB;
- options.c_iflag &= ~INPCK;
- options.c_cflag &= ~CSTOPB;
- options.c_iflag |= INPCK;
- options.c_cc[VTIME] = 0; //150; //15 seconds
- options.c_cc[VMIN] = 0;
- options.c_iflag |= IGNPAR|ICRNL;
- options.c_oflag |= OPOST;
- options.c_iflag &= ~(IXON|IXOFF|IXANY);
- options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //有时候要屏蔽掉这句才行,有时候又不用
-
- tcflush(fd, TCIFLUSH);
- #if 1
- //这里更奇怪有时候要屏蔽掉这段代码发送才正确
- if(tcsetattr(fd, TCSANOW, &options) != 0)
- {
- ui->textBrowser->insertPlainText("parity 2");
- // return(FALSE);
- }
- return(TRUE);
- #endif
- return(TRUE);
- }
- int Widget::OpenDev(char *Dev)
- {
- int fd = open(Dev, O_RDWR, 0);
- if(-1 == fd)
- {
- ui->textBrowser->insertPlainText("OpenDev");
- return -1;
- }
- else
- return fd;
- }
- void Widget::serial_init(void)
- {
- serial_fd = OpenDev("/dev/ttyUSB0");
- if(serial_fd > 0)
- set_speed(serial_fd); //设置波特率
- else
- {
- ui->textBrowser->insertPlainText("init_speed");
- exit(0);
- }
- #if 1 //恢复串口未阻塞状态
- if (fcntl(serial_fd, F_SETFL, O_NONBLOCK) < 0)
- {
- ui->textBrowser->insertPlainText("init_speed");
- exit(0);
- }
- //检查是否是终端设备
- #endif
- #if 1
- // if(isatty(STDIN_FILENO)==0)
- if(isatty(serial_fd)==0)
- {
- ui->textBrowser->insertPlainText("standard input is not a terminal device\n");
- }
- #endif
- #if 1 //设置串口参数
- if(set_Parity(serial_fd) == FALSE)
- {
- ui->textBrowser->insertPlainText("set_Parity Error\n");
- // exit(1);
- }
- #endif
- }
- void Widget::readCom()
- {
- char buffrecv[512];
- int nread=0;
- while((nread=read(serial_fd,buffrecv,512))<=0)
- {
- ;
- }
- for(int i=0;i<nread;i++)
- {
- QString str = QString("%1").arg(buffrecv[i]&0xff,2, 16, QLatin1Char('0'));
- ui->textBrowser->insertPlainText(str+" ");
- }
- nread=0;
- }
- void Widget::on_pushButton_clicked()
- {
- char buff[512];
- char buff2[] ={0xef,0x01,0xff,0xff,0xff,0xff,0x01,0x00,0x03,0x01,0x00,0x05};
- int nread=0,nwrite=0,i;
- nwrite = write(serial_fd,buff2,sizeof(buff2));
- readCom();
- }
复制代码 |
|