Solved

RS232 communications

Posted on 1997-12-02
2
403 Views
Last Modified: 2013-11-20
I'm trying to implement serial communications from an app and to test it out I wanted to write & read from the same com port by tying the TX & RX pins together(this works in Hyper terminal). My problem is that when I get to ReadFile the program hangs & I'm not good enough w/ the assembly code to figure out why...?

hCom = CreateFile("COM1",GENERIC_READ | GENERIC_WRITE,
          0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
   
if (hCom == INVALID_HANDLE_VALUE) // deal w/ err

char strBuffer[80] = "ABAB";
DWORD dwWrite;
OVERLAPPED OLWrite;

PurgeComm(hCom, PURGE_RXCLEAR);

if(!WriteFile(hCom,strBuffer,4,&dwWrite,&OLWrite))
      // deal w/ err

// delay a second before reading

if(!ReadFile(hCom,(void*)strBuffer,5,&dwRead,&OLRead))

This is the assembly code from 'ReadFile' call:
lea       eax,dword ptr [OLRead]
push      eax
lea       eax,dword ptr [dwRead]
push      eax
push      00000005
lea       eax,dword ptr [strBuffer]
push      eax
mov       eax,dword ptr [hCom]
push      eax
call      dword ptr [__imp__ReadFile@20 (004075a0)]

*** hangs at the next line ***
test      eax,eax      
jne       CLabTestView::Com+0000012a (00401c88)

Any suggestions?
0
Comment
Question by:Grailman
2 Comments
 
LVL 1

Accepted Solution

by:
IgorGrebnev earned 50 total points
ID: 1311329
Dear Grailman.
I worked a lot with serial ports, sometimes it works tricky and there is no good documentation. There is also subtle difference between windows 95 and windows NT. You cannot use GetOverlappedResults in win 95 to get the number of bytes written to serial port. Also if you do not specify comm. events mask, then WaitCommEvents blocks in win95 and doesn't blocks in windows NT.
Now about your question. I just send you a code that opens the serial port in my program and it works. There are additional calls for initialisation, thius explains your problem:

BOOL CUMDSerPort::OpenDevCommun( LPCSTR lpszPortName, int BaudRate )

{      if ( lpszPortName == NULL )  goto FileOpenError;
//-------------- Saves the port name and buffers sizes.  
  m_strPortName   =  lpszPortName;
//------------- Opens
  m_hPort = CreateFile
  ( lpszPortName,
            GENERIC_READ | GENERIC_WRITE,
            0,                                                                              // exclusive access                
            NULL,                           // no security attributes
            OPEN_EXISTING,
            FILE_FLAG_OVERLAPPED, //| FILE_FLAG_OVERLAPPED,
            NULL                            // No template
  );
      if ( m_hPort == INVALID_HANDLE_VALUE )                        goto FileOpenError;
//---------------      
  m_inBufferSize  =  4096 ;
  m_outBufferSize =  256;
  if ( !SetupComm( m_inBufferSize, m_outBufferSize ) )          goto FileOpenError;
//----------------- Set comm timeouts.
  COMMTIMEOUTS commTimeOuts;
      ZeroMemory( &commTimeOuts, sizeof( commTimeOuts ) );
  if ( !SetCommTimeouts( &commTimeOuts ) )                      goto FileOpenError;

//********************** Code copied from Moshe for tuning of port ************
  DCB  dcb;
  dcb.DCBlength = sizeof( DCB );
  GetCommState( &dcb );
    //For a string such as 96,n,8,1 or any other older-form mode string
    //    that doesn't end with an x or a p:
    //  fInX, fOutX, fOutXDsrFlow,
    //           and fOutXCtsFlow   are all set to FALSE
    //  fDtrControl                 is set to DTR_CONTROL_ENABLE
    //  sfRtsControl                is set to RTS_CONTROL_ENABLE
  dcb.BaudRate  = BaudRate;
  dcb.ByteSize  = 8;
  dcb.Parity    = NOPARITY;
  dcb.StopBits  = ONESTOPBIT;
     // Resume as if wire has not been pulled out, in case it has.  If
    // setting is TRUE, default, I will have to use a ClearCommError()
    // in the serial thread procedure.                            Hmm..?
  dcb.fAbortOnError=FALSE;  
 
  if ( !SetCommState( &dcb ) )                                  goto FileOpenError;
//****************************************************************************************  
//----------- The port is opened successfully
  return TRUE;
//*************** If error occured during opening, the control jump here.
FileOpenError :
//------------- We do not send message box from Server Application.
  Close();      
  return FALSE;
}

It is a member function of the CUMDSerPort, so instead of
SetupComm( m_inBufferSize, m_outBufferSize ) use API -
SetupComm( m_hPort, m_inBufferSize, m_outBufferSize ),
I just created wrappers in my class for all API.
If you are interested I can send you the source code for theis class. YOu can write to my e-mail igor_u@ultramind.co.il
Yours Igor
0
 
LVL 1

Author Comment

by:Grailman
ID: 1311330
I think this should get me going.

Thanks, Grailman
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Detect CR LF to each line 12 153
WinWaitActive parameters 12 31
while loop over for loop 7 93
sum67 challenge 35 93
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now