Hello,
I Could not Display data from Serial port. Please check my code and help me how to display data from serial port. The error is "error C3861: 'ProcessChar': identifier not found". I understood, it need to write some code for ProcessChar function to access data. could anybody help me in details.

#include <windows.h>
#include <iostream>
#include <string>
#include <fstream>

using namespace std;
DWORD dwError;
extern HWND hMainWnd;

int main(){

// Open the serial port.
  HANDLE hPort = CreateFile (TEXT("COM1"), // Pointer to the name of the port
                      GENERIC_READ | GENERIC_WRITE,
                                    // Access (read-write) mode
                      0,            // Share mode
                      NULL,         // Pointer to the security attribute
                      OPEN_EXISTING,// How to open the serial port
                      0,            // Port attributes
                      NULL);        // Handle to port with attribute
                                    // to copy

DCB PortDCB;

// Initialize the DCBlength member. 
PortDCB.DCBlength = sizeof (DCB); 

// Get the default port setting information.
GetCommState (hPort, &PortDCB);

// Change the DCB structure settings.
PortDCB.BaudRate = 9600;              // Current baud 
PortDCB.fBinary = TRUE;               // Binary mode; no EOF check 
PortDCB.fParity = TRUE;               // Enable parity checking 
PortDCB.fOutxCtsFlow = FALSE;         // No CTS output flow control 
PortDCB.fOutxDsrFlow = FALSE;         // No DSR output flow control 
PortDCB.fDtrControl = DTR_CONTROL_ENABLE; 
                                      // DTR flow control type 
PortDCB.fDsrSensitivity = FALSE;      // DSR sensitivity 
PortDCB.fTXContinueOnXoff = TRUE;     // XOFF continues Tx 
PortDCB.fOutX = FALSE;                // No XON/XOFF out flow control 
PortDCB.fInX = FALSE;                 // No XON/XOFF in flow control 
PortDCB.fErrorChar = FALSE;           // Disable error replacement 
PortDCB.fNull = FALSE;                // Disable null stripping 
PortDCB.fRtsControl = RTS_CONTROL_ENABLE; 
                                      // RTS flow control 
PortDCB.fAbortOnError = FALSE;        // Do not abort reads/writes on 
                                      // error
PortDCB.ByteSize = 8;                 // Number of bits/byte, 4-8 
PortDCB.Parity = NOPARITY;            // 0-4=no,odd,even,mark,space 
PortDCB.StopBits = ONESTOPBIT;        // 0,1,2 = 1, 1.5, 2 

// Configure the port according to the specifications of the DCB 
// structure.
if (!SetCommState (hPort, &PortDCB))
{
  // Could not configure the serial port.

/*
  dwError = GetLastError ();
  MessageBox (hMainWnd, TEXT("Unable to configure the serial port"), 
              TEXT("Error"), MB_OK);
*/
    cout<<"SetCommState failed" <<endl;
  return FALSE;
}


// Retrieve the timeout parameters for all read and write operations
// on the port. 
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts (hPort, &CommTimeouts);

// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = 100;  
CommTimeouts.ReadTotalTimeoutMultiplier = 100;  
CommTimeouts.ReadTotalTimeoutConstant = 100;    
CommTimeouts.WriteTotalTimeoutMultiplier = 100;  
CommTimeouts.WriteTotalTimeoutConstant = 100;    

// Set the timeout parameters for all read and write operations
// on the port. 
if (!SetCommTimeouts (hPort, &CommTimeouts))
{
  // Could not set the timeout parameters.
/*  MessageBox (hMainWnd, TEXT("Unable to set the timeout parameters"), 
              TEXT("Error"), MB_OK);
  dwError = GetLastError ();
*/
    cout<<"SetCommTimeouts failed" <<endl;
  return FALSE;
}

BYTE Byte;
DWORD dwBytesTransferred;
DWORD dwCommModemStatus;

// Specify a set of events to be monitored for the port.
SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD | EV_RING);

while (hPort != INVALID_HANDLE_VALUE) 
{
  // Wait for an event to occur for the port.
  WaitCommEvent (hPort, &dwCommModemStatus, 0);

  // Re-specify the set of events to be monitored for the port.
  SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RING);

  if (dwCommModemStatus & EV_RXCHAR) 
  {
    // Loop for waiting for the data.
    do 
    {
      // Read the data from the serial port.
      ReadFile (hPort, &Byte, 1, &dwBytesTransferred, 0);

      // Display the data read.
      if (dwBytesTransferred == 1)
      ProcessChar (Byte);

    } while (dwBytesTransferred == 1);
  }
}
CloseHandle(hPort);
//cout<<"Program Run Successfully!!!"<<endl;

}

Thanks in advance.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Comments
if (postcount > 10 && !code_tags) repution-=(niek_e.reppower/2);

First you have to know what kind of data is arriving at the port. Then just add the characters to a data buffer. I have no idea what data you are getting, so just assume you want to put it into a character array

unsigned char buffer[255] = {0};
int nextin = 0; // next spot to add a new charcter

void ProcessChar(unsigned char c)
{
    if( nextin < sizeof(buffer)) // make sure there's room for the char
       buffer[nextin++] = c;
}

I am sorry for reply late. The device produces ASCII data. I could not success to display the data yet. i tried to use the above code, but i could not able to display the data. This is my first program for embedded system. Please help me more descriptively.

Thanks for cooperation.

Thanks for your reply. Please see code for read and display data.

unsigned char buffer[255] = {0};
int i = 0;

void ProcessChar(unsigned char c)
{
    if( i < sizeof(buffer)) 
       buffer[i++] = c;
    cout<<"%s", buffer[i];
}


BYTE Byte;
DWORD Read;
DWORD dwCommModemStatus;


// Specify a set of events to be monitored for the port.
SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD | EV_RING);

while (hPort == INVALID_HANDLE_VALUE)
{

  // Wait for an event to occur for the port.
  WaitCommEvent (hPort, &dwCommModemStatus, 0);

  // Re-specify the set of events to be monitored for the port.
  SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RING);

  if (dwCommModemStatus) 
  {

    // Loop for waiting for the data.
    do 
    {
      // Read the data from the serial port.
      ReadFile (hPort, buffer, sizeof(buffer), &Read, NULL);

      // Display the data read.
      if (Read == 1)

      ProcessChar (Byte);

    } while (Read == 1);
  }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This is my first program for embedded system.

This program will never run on an embedded system unless you're running Windows (CE) on it. So are you?
Also, when you say: doesn't work; what doesn't work?

And last but certainly not least: code-tags. Learn how to use them.

The code for the visual basic program is based on the WinWedge DDE Example for Visual Basic - Sending DDE Commands To WinWedge From A VB Application. At its most basic, the Project consists of a only a single form and a module but, to make our example more useful, instead of 'hard coding' variables like the selected com port we added code to read these parameters from a setup.ini file. This method allows you to easily add more of your own settings and change variables without needing to recompile the program each time.

There is no need to display a screen shot of the form as it is never displayed on screen. Instead all we run is the Main Subroutine.

<snipped>

This program will never run on an embedded system unless you're running Windows (CE) on it. So are you?
Also, when you say: doesn't work; what doesn't work?

And last but certainly not least: code-tags. Learn how to use them.

Thanks for your post. I am using Visual studio 2008. as my know it supports Windows (CE).
Actually it is part of whole program, which i posted at first of my post. I could able to access the device, but i could not read and display data.
Thanks for your cooperation.

Hello mark456,
I am using visual c++ and my program is written in C++ code.
Thanks for your post.

And don't have anything running Windows with a serial port at the moment, so I can't test your code. I would recommend to put a lot of debug output between the lines, to see where the program fails.

This article has been dead for over six months. Start a new discussion instead.