views:

239

answers:

1

Hi! 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;

}
+1  A: 

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.

Hans Passant
How can I convert QChar to char to be able to receive it correct ??
javaAmator
You can't, no hammer is big enough. If you don't want to encode it yourself, use some kind of QT or operating system function to encode to, say, utf-8. Read this: http://www.joelonsoftware.com/articles/Unicode.html
Hans Passant