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

发布于 2024-08-31 10:35:27 字数 3511 浏览 12 评论 0原文

我编写了与调制解调器通信的程序(它使用 Hayes 命令),并且这是有效的。 GUI是用QT编写的,但与COM端口的通信是用winapi库编写的。当我想将程序消息从一台计算机发送到另一台计算机时遇到问题,我无法发送波兰字符(它们被“?”替换),我该如何修复它?有人有想法吗?我还有一个问题,我无法从我的程序向 Microsoft 超级终端发送消息,超级终端收到一些内容,但不是我发送的内容。 感谢您的帮助:)

重要的代码片段: 连接端口:

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);

发送 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));

}

接收 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;

}

I write program to communicate with modem (it useing Hayes commands) and this is working.
GUI is programmed with QT, but communication with COM port is write with winapi library. 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.
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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热情消退 2024-09-07 10:35:27

您正在将 16 位 QChar 敲入 8 位孔中。您需要为每个字符发送和接收 2 个字节,或者将自己限制为 8 位字符编码。

You are hammering a 16-bit QChar into a 8-bit hole. You'll need to send and receive 2 bytes per character or restrict yourself to an 8-bit character encoding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文