简体   繁体   English

与超级终端通讯[QT和WINApi]

[英]Communication with HyperTerminal [ QT and WINApi ]

I write program to communicate with modem (it useing Hayes commands) and this is working. 我写了程序与调制解调器通信(它使用Hayes命令),并且可以正常工作。 GUI is programmed with QT, but communication with COM port is write with winapi library. GUI是使用QT编程的,但是与COM端口的通信是使用winapi库编写的。 I have problem when I want to send with my program message from one computer to another, i can't send Polish chars (they are repleaced by '?'), how can I fix it ? 当我想将程序消息从一台计算机发送到另一台计算机时,我遇到问题,我无法发送波兰字符(它们由'?'补充),我该如何解决? Does anyone have idea ?? 有人知道吗? And I have one more problem, I can't send message from my program to Microsoft HyperTerminal, HyperTerminal receive something, but not that what I send. 我还有一个问题,我无法从程序向Microsoft HyperTerminal发送消息,HyperTerminal收到了一些东西,但不是我发送的。 Thx for any help :) 感谢任何帮助:)

Important pieces of code: Connect with port: 重要的代码段:与端口连接:

portHandle = CreateFile (portName, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);

GetCommState (portHandle, &dcb);
switch(ui->comboBox->currentIndex())
{
       case 0 : dcb.BaudRate=CBR_110; break;
       case 1 : dcb.BaudRate=CBR_300; break;
       case 2 : dcb.BaudRate=CBR_600; break;
       case 3 : dcb.BaudRate=CBR_1200; break;
       case 4 : dcb.BaudRate=CBR_2400; break;
       case 5 : dcb.BaudRate=CBR_4800; break;
       case 6 : dcb.BaudRate=CBR_9600; break;
       case 7 : dcb.BaudRate=CBR_14400; break;
       case 8 : dcb.BaudRate=CBR_19200; break;
       case 9 : dcb.BaudRate=CBR_38400; break;
       case 10 : dcb.BaudRate=CBR_56000; break;
       case 11 : dcb.BaudRate=CBR_57600; break;
       case 12 : dcb.BaudRate=CBR_115200; break;
       case 13 : dcb.BaudRate=CBR_128000; break;
       case 14 : dcb.BaudRate=CBR_256000; break;
}

dcb.fBinary = TRUE;
dcb.fParity = TRUE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fDsrSensitivity = FALSE;
dcb.fTXContinueOnXoff = TRUE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fErrorChar = FALSE;
dcb.fNull = FALSE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.fAbortOnError = FALSE;
//dcb.ByteSize = dataBits;
dcb.DCBlength = sizeof (DCB);

switch(ui->comboBox_3->currentIndex())
{
   case 1 : dcb.Parity = EVENPARITY; break;
   case 3 : dcb.Parity = MARKPARITY; break;
   case 2 : dcb.Parity = ODDPARITY; break;
   case 4 : dcb.Parity = SPACEPARITY; break;
   case 0 : dcb.Parity = NOPARITY; break;
}

switch (ui->comboBox_4->currentIndex())
{
    case 0 : dcb.StopBits = ONESTOPBIT; break;
    case 1 : dcb.StopBits = ONE5STOPBITS;break;
    case 2 : dcb.StopBits = TWOSTOPBITS; break;
}

switch (ui->comboBox_2->currentIndex())
{
    case 0 : dcb.ByteSize = 5; break;
    case 1 : dcb.ByteSize = 6;break;
    case 2 : dcb.ByteSize= 7; break;
     case 3 : dcb.ByteSize = 8; break;
}

SetCommState (portHandle, &dcb);

GetCommTimeouts (portHandle, &CommTimeouts);
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;
CommTimeouts.WriteTotalTimeoutConstant = 1000;
SetCommTimeouts (portHandle, &CommTimeouts);

Send MSG: 发送味精:

void MainWindow::Send(char c)
{
    do
        {WriteFile(portHandle, &c, 1, &cbWritten, NULL);
        }
    while (!(cbWritten));
}

void MainWindow::on_pushButton_clicked()
{
    QString str = ui->lineEdit->text();
    std::string str2;
    ui->lineEdit->clear();
    str2 = str.toStdString();
    for(int i=0; i < str2.size();i++)
    {
        Send(str2[i]);
        //qDebug()<< str2[i];
    }
    Send(char(13));

}

Receive MSG: 接收味精:

void ReaderThread::run()
{

    char c;
    while(1)
    {

        c = Receive();
        if(c==13)
        {
            emit insertPlainText("\n");
        }
        else
        {
            emit insertPlainText(QString(c));

        }

    }
}

char ReaderThread::Receive()
{
    char c;
    do{
        ReadFile(portHandle, &c, 1, &cbRead, NULL);
    }
    while (!(cbRead));
    return c;

}

You are hammering a 16-bit QChar into a 8-bit hole. 您正在将16位QChar锤入8位孔中。 You'll need to send and receive 2 bytes per character or restrict yourself to an 8-bit character encoding. 您需要每个字符发送和接收2个字节,或者将自己限制为8位字符编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM