www.pudn.com > myQTserial.rar > mywindow.cpp


#include 
#include "mywindow.h"

MyWindow::MyWindow(QWidget *parent): QDialog(parent)
{
    setupUi(this);
    //QApplication myApp(argc,argv);
   // QLabel myLabel("Hello",0);
    readTimer = new QTimer(this);
    RS232=new Win_QextSerialPort("COM1");
    RS232->setBaudRate(BAUD9600); //com1,19200,n,8,1
    RS232->setFlowControl(FLOW_OFF);
    RS232->setParity(PAR_NONE);
    RS232->setDataBits(DATA_8 );
    RS232->setStopBits(STOP_1);
        //QHostAddress MyAddress;
    //QString FakeAddress;

    //FakeAddress = "192.168.10.136";
    //MyAddress.setAddress(FakeAddress);
    //short int Port = 6000;
    //ServerSocket = new QSocketDevice(QSocketDevice::Stream);
    //ClientSocket = new QSocketDevice(QSocketDevice::Stream);
    tcp_server.listen(QHostAddress("192.168.10.136"),6000);
    //ServerSocket->lister(20);//20 stand for the max simulate connect

/*     ClientNotifier = new
 *         QSocketNotifier(ClientSocket->socket(),QSocketNotifier::Read,0,"ClientSocket");
 */
/*     ServerNotifier = new
 *         QSocketNotifier(ServerSocket->socket(),QSocketNotifier::Read,0,"ServerSocket");
 * 
 */
    connect(PushButtonInsert, SIGNAL(clicked()), this, SLOT(InsertItem()));
    connect(PushButtonSend, SIGNAL(clicked()), this, SLOT(SendNetdata()));
    connect(radioButton,SIGNAL(clicked()),this,SLOT(OpencloseCom()));
    connect(AddressPushButton,SIGNAL(clicked()),this,SLOT(SendAddress()));
    connect(&tcp_server,
            SIGNAL(newConnection()),this,SLOT(acceptConnection()));
    connect(readTimer,SIGNAL(timeout()),this,SLOT(Readcom()));
    connect(PushButtonSendfileNet,SIGNAL(clicked()),this,SLOT(SendfileNet()));
    connect(PushButtonSendfileCom,SIGNAL(clicked()),this,SLOT(SendfileCom()));

    
}

MyWindow::~MyWindow()
{
  
    RS232->close();
}

void MyWindow::acceptConnection()
{
    ServerSocket = tcp_server.nextPendingConnection();
    connect(ServerSocket,SIGNAL(readyRead()),this,SLOT(OnMReceive()));
}
        

void MyWindow::Readcom()
{
    char * incommingdata;
    qint64 ByteCount,ReadCount;
    readTimer->stop();
    ByteCount = RS232->bytesAvailable();
    if(ByteCount)
    {
        incommingdata = (char *)malloc(ByteCount + 1); 
        ReadCount = RS232->readData(incommingdata ,ByteCount);
       
        incommingdata[ByteCount] = '\0';
        ComdataEdit->append(QString::fromLocal8Bit(incommingdata,-1));
        free(incommingdata);

        readTimer->start(100);
    }
    else
        readTimer->start(100);
} 

void MyWindow::InsertItem()
{
	if (LineEditInsert->text().isEmpty())
	{
		QMessageBox::critical(this, tr("Error"), tr("the input text box has no character"), QMessageBox::Ok, 0);
	}
	else
	{
                strcpy(outbuf,(LineEditInsert->text()).toLocal8Bit().data());
                count = (LineEditInsert->text()).toLocal8Bit().size();
                for(int x = 0; x != count; x++)
                RS232->putChar(outbuf[x]);
                RS232->flush();
                
		//LineEditInsert->clear();
	}
	LineEditInsert->setFocus();
}
void MyWindow::SendNetdata()
{
	if (LineEditSend->text().isEmpty())
	{
		QMessageBox::critical(this, tr("Error"), tr("the socket has no character"), QMessageBox::Ok, 0);
	}
	else
	{
                strcpy(outbuf,(LineEditSend->text()).toLocal8Bit().data());
                count = (LineEditSend->text()).toLocal8Bit().size();
                ServerSocket->write(outbuf,count);
		//LineEditSend->clear();
	}
	LineEditSend->setFocus();
}
void MyWindow::SendfileNet()
{
    QString line;
    QString filename = QFileDialog::getOpenFileName(this,tr("Open txt file..."),"txt/","*.txt",0,0);
    if(filename.isEmpty())
    {
        return;
    }
    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;

    while (!file.atEnd())
    {
        line = file.readLine();
        //count = ;
        ServerSocket->write(line.toLocal8Bit().data(),line.toLocal8Bit().size());
    }

}
 void MyWindow::SendfileCom()
{
    QString filename = QFileDialog::getOpenFileName(this,tr("Open txt file..."),"txt/","*.txt",0,0);
    if(filename.isEmpty())
    {
        return;
    }
    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;

    //while (!file.atEnd())
    {
        QString line = file.readAll();
        count = line.toLocal8Bit().size();
        RS232->writeData(line.toLocal8Bit().data(),line.toLocal8Bit().size());
        RS232->flush();
    }
}   
void MyWindow::SendAddress()
{
        RS232->putChar('\xc0');
        RS232->putChar('\xa8');
        RS232->putChar('\x0a');
        RS232->putChar('\x88');
        RS232->putChar('\x17');
        RS232->putChar('\x70');
        RS232->flush();
}

void MyWindow::OnMReceive()
{
    int ByteCount,ReadCount;
    char *IncommingChar;
    //fprintf(stderr,"load a piece of Message!\n");

    ByteCount = ServerSocket->bytesAvailable();
    IncommingChar = (char *)malloc(ByteCount + 1);
    ReadCount = ServerSocket->readLine(IncommingChar,ByteCount + 1);
    //IncommingChar[ByteCount] = '\0';
    NetdataEdit->append(QString::fromLocal8Bit(IncommingChar,-1));
    free(IncommingChar);
    
    //fprintf(stderr,"%s",IncommingChar); //print received data

}
void MyWindow::OpencloseCom()
{
    if(radioButton->isChecked())
    {


        if(RS232->open()) 
        {
/*             RS232->putChar('o');
 *             RS232->putChar('r');
 *             RS232->putChar('n');
 * 
 */
            readTimer->start(100);
        }
        else

            return;
    }
    else
        RS232->close();
}