O
O
OnlyFirster2015-07-31 15:35:43
Programming
OnlyFirster, 2015-07-31 15:35:43

Strange response from COM port, why?

Hello dear!
There is a code:

#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

int main ()
{

    HANDLE port = CreateFileA("\\\\.\\COM24", GENERIC_READ | GENERIC_WRITE, 0,
                              NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if(port == INVALID_HANDLE_VALUE)				{ MessageBoxA(NULL, "Порт не открыт", "", MB_OK); return 0; }
    if( ! SetCommMask(port, EV_RXCHAR | EV_ERR) )   { MessageBoxA(NULL, "маска не установлена", "", MB_OK); return 0; }

    DCB dcb;
    if( ! GetCommState(port, &dcb) )   { MessageBoxA(NULL, "GCS error", "", MB_OK);; return 0; }
    dcb.DCBlength = sizeof(DCB);
    dcb.BaudRate = CBR_9600;
    dcb.fBinary = TRUE;
    dcb.fParity = FALSE;
    dcb.fOutxCtsFlow = FALSE;				// hardware flow control //
    dcb.fOutxDsrFlow = FALSE;				// hardware flow control //
    dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;	// hardware flow control
    dcb.fDsrSensitivity = FALSE;				// software flow control
    //dcb.fTXContinueOnXoff = ;				// software flow control
    dcb.fOutX = FALSE;					// software flow control  //
    dcb.fInX = FALSE;					// software flow control
    //dcb.fErrorChar = ;
    dcb.fNull = TRUE;
    dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;	// hardware flow control
    dcb.fAbortOnError = FALSE;  //
    //dcb.fDummy2 = ;
    dcb.wReserved = 0;
    //dcb.XonLim = ;						// software flow control
    //dcb.XoffLim = ;						// software flow control
    dcb.ByteSize = 8;
    dcb.Parity = NOPARITY;
    dcb.StopBits = ONESTOPBIT;
    //dcb.XonChar = ;					// software flow control
    //dcb.XoffChar = ;					// software flow control
    //dcb.ErrorChar = ;
    //dcb.EofChar = ;
    //dcb.EvtChar = ;
    //dcb.wReserved1 = ;
    if( ! SetCommState(port, &dcb) )   { MessageBoxA(NULL, "SCS error", "", MB_OK); return 0; }

    COMMTIMEOUTS timeouts;
    timeouts.ReadIntervalTimeout = MAXDWORD;
    timeouts.ReadTotalTimeoutMultiplier = 100;
    timeouts.ReadTotalTimeoutConstant = 100;
    timeouts.WriteTotalTimeoutMultiplier = 100;
    timeouts.WriteTotalTimeoutConstant = 100;
    if (!SetCommTimeouts(port, &timeouts))	{ MessageBoxA(NULL, "Не удалось задать timout", "", MB_OK); return 0; }

    DWORD error;
    COMSTAT status;
    if( ! ClearCommError(port, &error, &status) )	{ MessageBoxA(NULL, "CME error", "", MB_OK); return 0; }

#define my_LEN(str)		strlen(str)
    DWORD written;
    char str[50] = "at\r";
    char str1[50];
    WriteFile(port, str, my_LEN(str), &written, NULL);
    if( ! WriteFile(port, str, my_LEN(str),		// Отсылаем AT команду модему
                    &written, NULL) )
    { MessageBoxA(NULL, "WriteFile error", "", MB_OK); return 0; }


    DWORD event;
    if( WaitCommEvent(port, &event, NULL) )   // Ждем события порта и не получаем его. Программа виснет тут.
    {
        ReadFile(port, &str1, my_LEN(str), &written, NULL);
        cout << str1  << endl;
        switch(event)
        {
            case EV_RXCHAR:
                break;
            case EV_ERR:
                break;
            default:
                MessageBoxA(NULL, "WCE error default", "", MB_OK); return 0;
        }
    }
    else   { MessageBoxA(NULL, "WCE error", "", MB_OK); return 0; }

    cout <<"End programm\n";	// До этой точки мы не доходим

    return 0;
}

An AT command is sent to the e352 megaphone modem, in response it returns messages like this:
Mв–ЂNв™ЈРґв– (
End programm

I don’t understand why, I tried to set up a Russian locale for the console, for example - all the same, such krakozyabry, do not tell me what could be the matter?
The settings are set according to the modem.
Thanks in advance for your replies!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Martyanov, 2015-07-31
@vilgeforce

Port parameters: speed, number of data bits, parity and stopbits check. There must be a problem somewhere.

J
jcmvbkbc, 2015-07-31
@jcmvbkbc

char str[50] = "at\r";

I would replace with "at\r\n".
There is something mixed up here. But if this is corrected, it will still not work properly because my_LEN will be surprised.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question