Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Reading problems on COM2 port

AllvaterAllvater Posts: 14Member
[b][red]This message was edited by Allvater at 2003-7-15 1:51:21[/red][/b][hr]
Hi there,

I 'm writing a programm that sends an AT-command to a 56K-modem. If the modem has recieved this command, it should return an "OK" (You can try it with Hyperterminal: Open a port and send the command "atz"; The word "OK" will appear).
Now the problem: The "OK" isn't returned - instead the "atz"-command is returned.
Here's the complete code:
DWORD dwBytesRead; // BytesRead
BOOL bSuccess;
HANDLE hComm; // Handle on COM-Port
DCB dcb;
COMMTIMEOUTS timeout;
char *pPort = "COM2"; // Portname

ZeroMemory(&dcb, sizeof(dcb)); // Fill block of memory with zeros

// Open Port
hComm = CreateFile(pPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

// Aaarrgh, error opening port
if(hComm == INVALID_HANDLE_VALUE)
{
AfxMessageBox("Zugriff verweigert!
Entweder ist kein Gert an diesem Port angeschlossen
oder es existiert bereits eine Verbindung mit diesem Port!", MB_ICONSTOP);
return FALSE;
}

// Get current control settings
bSuccess = GetCommState(hComm, &dcb);

// Aaarrgh, error getting settings
if(!bSuccess)
{
AfxMessageBox("Kann aktuelle Konfiguration nicht ermitteln!", MB_ICONSTOP);
return FALSE;
}

// Fill dcb structure:
dcb.DCBlength = sizeof(DCB);
dcb.BaudRate = 1200;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fAbortOnError = TRUE;
dcb.fBinary = TRUE;
dcb.wReserved = 0;

// Set new configuration settigs
bSuccess = SetCommState(hComm, &dcb);

// Aaarrgh, error setting new values
if(!bSuccess)
{
AfxMessageBox("Kann Einstellungen nicht setzen!", MB_ICONSTOP);
return FALSE;
}

// Get timeout properties
bSuccess = GetCommTimeouts(hComm, &timeout);

// Aaarrgh, error getting properties
if(!bSuccess)
{
AfxMessageBox("Kann Timeouteinstellungen nicht holen!");
}

timeout.ReadIntervalTimeout = 5;
timeout.ReadTotalTimeoutMultiplier = 0;
timeout.ReadTotalTimeoutConstant = 0;
timeout.WriteTotalTimeoutConstant = 0;
timeout.WriteTotalTimeoutMultiplier = 0;

// Set timeout properties
bSuccess = SetCommTimeouts(hComm, &timeout);

// Aaarrgh, error setting new properties
if(!bSuccess)
{
AfxMessageBox("Kann Timeouteinstellungen nicht holen!");
}

// Write to file (com2 port)
char msg[] = "atz
";
DWORD dwLaenge = strlen(msg);
bSuccess = WriteFile(hComm, msg, dwLaenge, &dwBytesRead, NULL);

// Damn it, can't write!
if(!bSuccess)
{
AfxMessageBox("Konnte nicht schreiben!");
}

// Clear buffer
FlushFileBuffers(hComm);

// Get and set comm events
DWORD dwEventMask = EV_BREAK | EV_CTS | EV_DSR | EV_ERR | EV_RING | EV_RLSD | EV_RXCHAR | EV_RXFLAG | EV_TXEMPTY;
GetCommMask(hComm, &dwEventMask);
SetCommMask(hComm, dwEventMask);

// Wait on events
WaitCommEvent(hComm, &dwEventMask, NULL);

// Read from file (com2 port)
char cBuf[16];

memset(cBuf, '', sizeof(cBuf));
bSuccess = ReadFile(hComm, cBuf, sizeof(cBuf), &dwBytesRead, 0);
// Damn it, can't read!
if(!bSuccess)
{
AfxMessageBox("Konnte nicht lesen!");
}
else
{
// Output for testing (THE OUTPUT SHOULD BE "OK" NOT "atz" !!!!)
CString strResult = cBuf;
MessageBox(strResult, "Das schickt das Modem zurck:", MB_OK);
}

return TRUE;


Comments

  • weiccoweicco Posts: 950Member
    After you have opened the COM port and initialized it send string ATE0
    to modem so it won't echo your commands back to you.

    Btw. Baudrate 1200 is _really_ slow, you could try to raise it. And why do you first initialize dwEventMask and then read you read mask from COM port to it (loosing all the initialized stuff)?


    : [b][red]This message was edited by Allvater at 2003-7-15 1:51:21[/red][/b][hr]
    : Hi there,
    :
    : I 'm writing a programm that sends an AT-command to a 56K-modem. If the modem has recieved this command, it should return an "OK" (You can try it with Hyperterminal: Open a port and send the command "atz"; The word "OK" will appear).
    : Now the problem: The "OK" isn't returned - instead the "atz"-command is returned.
    : Here's the complete code:
    : DWORD dwBytesRead; // BytesRead
    : BOOL bSuccess;
    : HANDLE hComm; // Handle on COM-Port
    : DCB dcb;
    : COMMTIMEOUTS timeout;
    : char *pPort = "COM2"; // Portname
    :
    : ZeroMemory(&dcb, sizeof(dcb)); // Fill block of memory with zeros
    :
    : // Open Port
    : hComm = CreateFile(pPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
    :
    : // Aaarrgh, error opening port
    : if(hComm == INVALID_HANDLE_VALUE)
    : {
    : AfxMessageBox("Zugriff verweigert!
    Entweder ist kein Gert an diesem Port angeschlossen
    oder es existiert bereits eine Verbindung mit diesem Port!", MB_ICONSTOP);
    : return FALSE;
    : }
    :
    : // Get current control settings
    : bSuccess = GetCommState(hComm, &dcb);
    :
    : // Aaarrgh, error getting settings
    : if(!bSuccess)
    : {
    : AfxMessageBox("Kann aktuelle Konfiguration nicht ermitteln!", MB_ICONSTOP);
    : return FALSE;
    : }
    :
    : // Fill dcb structure:
    : dcb.DCBlength = sizeof(DCB);
    : dcb.BaudRate = 1200;
    : dcb.ByteSize = 8;
    : dcb.Parity = NOPARITY;
    : dcb.StopBits = ONESTOPBIT;
    : dcb.fAbortOnError = TRUE;
    : dcb.fBinary = TRUE;
    : dcb.wReserved = 0;
    :
    : // Set new configuration settigs
    : bSuccess = SetCommState(hComm, &dcb);
    :
    : // Aaarrgh, error setting new values
    : if(!bSuccess)
    : {
    : AfxMessageBox("Kann Einstellungen nicht setzen!", MB_ICONSTOP);
    : return FALSE;
    : }
    :
    : // Get timeout properties
    : bSuccess = GetCommTimeouts(hComm, &timeout);
    :
    : // Aaarrgh, error getting properties
    : if(!bSuccess)
    : {
    : AfxMessageBox("Kann Timeouteinstellungen nicht holen!");
    : }
    :
    : timeout.ReadIntervalTimeout = 5;
    : timeout.ReadTotalTimeoutMultiplier = 0;
    : timeout.ReadTotalTimeoutConstant = 0;
    : timeout.WriteTotalTimeoutConstant = 0;
    : timeout.WriteTotalTimeoutMultiplier = 0;
    :
    : // Set timeout properties
    : bSuccess = SetCommTimeouts(hComm, &timeout);
    :
    : // Aaarrgh, error setting new properties
    : if(!bSuccess)
    : {
    : AfxMessageBox("Kann Timeouteinstellungen nicht holen!");
    : }
    :
    : // Write to file (com2 port)
    : char msg[] = "atz
    ";
    : DWORD dwLaenge = strlen(msg);
    : bSuccess = WriteFile(hComm, msg, dwLaenge, &dwBytesRead, NULL);
    :
    : // Damn it, can't write!
    : if(!bSuccess)
    : {
    : AfxMessageBox("Konnte nicht schreiben!");
    : }
    :
    : // Clear buffer
    : FlushFileBuffers(hComm);
    :
    : // Get and set comm events
    : DWORD dwEventMask = EV_BREAK | EV_CTS | EV_DSR | EV_ERR | EV_RING | EV_RLSD | EV_RXCHAR | EV_RXFLAG | EV_TXEMPTY;
    : GetCommMask(hComm, &dwEventMask);
    : SetCommMask(hComm, dwEventMask);
    :
    : // Wait on events
    : WaitCommEvent(hComm, &dwEventMask, NULL);
    :
    : // Read from file (com2 port)
    : char cBuf[16];
    :
    : memset(cBuf, '', sizeof(cBuf));
    : bSuccess = ReadFile(hComm, cBuf, sizeof(cBuf), &dwBytesRead, 0);
    : // Damn it, can't read!
    : if(!bSuccess)
    : {
    : AfxMessageBox("Konnte nicht lesen!");
    : }
    : else
    : {
    : // Output for testing (THE OUTPUT SHOULD BE "OK" NOT "atz" !!!!)
    : CString strResult = cBuf;
    : MessageBox(strResult, "Das schickt das Modem zurck:", MB_OK);
    : }
    :
    : return TRUE;
    :
    :
    :
  • AllvaterAllvater Posts: 14Member
    Hi,

    now it works - I forgot the memset() and the Sleep() functions :-)
    The Baudrate must be 1200 because it's for a special screen which needs this rate.

    Thx
Sign In or Register to comment.