RS-232 communication

I am writing in Borland C Win16, and I have a problem when sending and recieving files over the comport.
I have opened the comport and then filled the complete DCB structure and then used SetCommState to set the struct.
Then I have a small loop where I reads data from a file
and then sends the same data to the comport.
It looks more or less like this

do
{
      IOStatus = _lread(hFile,(void _huge*)pBuf,fSize);
      if(IOStatus==0)
        break;
      /* This is the only control I does when sending */
      /* Is that correct ? */
      do
      {  
         err=GetComError(PortResult,&comstat);
      }while((outQue-comstat.cbOutQue)<IOStatus);
    WriteComm(PortResult,(const void FAR*)pBuf,IOStatus);
}while(IOStatus>=fSize);

But it seems like the handshaking does not work with higher baudrates, or maybe not at all, with lower baudrates maybe the handshaking is not needed.. And if I understood everything right I don't need to take care of the handshaking only not to send more then is room in the outque.
It does not matter whether I use software or hardware flow control.
Would be very happy if someone could help me or explane to me what can be the problem or what I do wrong.
    Best Regards
    Tord Karlstrom
tordAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gvgCommented:
I don't know if this is you actual code but if it is I have few comments

while((outQue-comstat.cbOutQue)>IOStatus);

This doesn't work.   comstat.cbOutQue is getting smaller when the port is sending so (outQue-comstat.cbOutQue) whitch indicates how much space you have in the queue is growing so you get stuck in an
endless loop.

The sign should be changed to < instead of > 

You also have an extra do loop with no while.  I can't see that this extra loop makes any problem if it compiles.

Handshaking depends on what the receiver wants.  If the receiver
doesn't use handshake you shouldn't either.  But if handshake is not
used you have to be sure that the sender and receiver are both using
the same baudrate.
0
tordAuthor Commented:
Edited text of question
0
tordAuthor Commented:
I'am very sorry that my question included a few errors. The example
I did give was not taken from my real source.
Of course the > sign should be < and not the extra do loop should be included.

But the problems still exists. When I go with baudrates over 56000 baud it seems like this problem occur.
I always get a buffer overflow on the recieving side.
But it works better when both computers have more or less the same cpuspeed.
Is this correct ?
I have tried to use EscapeCommFunction to control the handshaking myself but it does not work ?
I hope I get a good answer.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

MikeDabbsCommented:
How exactly are you setting up the DCB?  I'm sure I could get an answer for you if I saw this section of cade as well.
0
tordAuthor Commented:
Here are my DCB structure. As you can see I have a Machine structure where I have all my parameters wich have been set from a dialogbox.
Port i a string that is "COM1:" or "COM2:"
Everything works good up to 19200 and sometimes 38400 baud
but then my problems starts.
Is it a problem that I am sending from a Pentium machine to a 386 machine ?


   #define inQue 1024
   #define outQue 128

   if((PortResult=OpenComm(Port,inQue,outQue))>=0)
   {
      GetCommState(PortResult,&DCB);

      if(Machine.Baud==0)
        DCB.BaudRate=CBR_110;
      else
      if(Machine.Baud==1)
        DCB.BaudRate=CBR_300;
      else
      if(Machine.Baud==2)
        DCB.BaudRate=CBR_600;
      else
      if(Machine.Baud==3)
        DCB.BaudRate=CBR_1200;
      else
      if(Machine.Baud==4)
        DCB.BaudRate=CBR_2400;
      else
      if(Machine.Baud==5)
        DCB.BaudRate=CBR_4800;
      else
      if(Machine.Baud==6)
        DCB.BaudRate=CBR_9600;
      else
      if(Machine.Baud==7)
        DCB.BaudRate=CBR_19200;
      else
      if(Machine.Baud==8)
        DCB.BaudRate=CBR_38400;
      else
      if(Machine.Baud==9)
        DCB.BaudRate=CBR_56000;
      else
      if(Machine.Baud==10)
        DCB.BaudRate=CBR_128000;
      else
      if(Machine.Baud==11)
        DCB.BaudRate=CBR_256000;

      /* Machine.ByteSize=1/2/3/4
      DCB.ByteSize = Machine.Bytesize+4;

      if(Machine.Stopbits==1)
        DCB.StopBits       = ONESTOPBIT;
      else
      if(Machine.Stopbits==2)
        DCB.StopBits       = ONE5STOPBITS;
      else
      if(Machine.Stopbits==3)
        DCB.StopBits       = TWOSTOPBITS;

      if(Machine.Parity==0)
        DCB.Parity       = NOPARITY;
      else
      if(Machine.Parity==1)
        DCB.Parity       = EVENPARITY;
      else
      if(Machine.Parity==2)
        DCB.Parity       = ODDPARITY;
      else
      if(Machine.Parity==3)
        DCB.Parity       = MARKPARITY;

      if(Machine.Binary==0)
        DCB.fBinary  = 0;
      else
        DCB.fBinary  = 1;

      /* Machine.RtsCts=0/1
      DCB.fRtsflow = DCB.fOutxCtsFlow = (UINT)Machine.RtsCts;
      if(DCB.fRtsflow)
      {
        DCB.CtsTimeout  = 30;  /* Have tried to set to 0
        DCB.fRtsDisable = 0;
      }
      else
      {
        DCB.CtsTimeout  = 0;
        DCB.fRtsDisable = 1;
      }

      /* Machine.DtrDsr=0/1
      DCB.fDtrflow = DCB.fOutxDsrFlow = (UINT)Machine.DtrDsr;
      if(DCB.fDtrflow)
      {
        DCB.DsrTimeout  = 50;  /* Have tried to set to 0
        DCB.fDtrDisable = 0;
      }
      else
      {
        DCB.DsrTimeout  = 0;
        DCB.fDtrDisable = 1;
      }

      /* Machine.XonXoff=0/1
      DCB.fOutX = DCB.fInX  = (UINT)Machine.XonXoff;

      DCB.XonChar                     = 0x11;
      DCB.XoffChar                    = 0x13;
      DCB.XonLim                      = 128;
      DCB.XoffLim                     = inQue-256;
      if(Machine.Binary==0)
        DCB.fNull                       = 1;
      else
        DCB.fNull                       = 0;

      /* Machine.ParControl=0/1
      DCB.fParity = Machine.ParControl ;

      if(SetCommState(&DCB)!=0)
      {
          MessageBox(NULL,"Error","",MB_OK);
      }

0
MikeDabbsCommented:
You look as though you're being quite thorough while setting up the DCB.  I never try to do everything myself.  Have you investigated the BuildCommDCB function?  It sets up a lot of the DCB for you based on a definition string (i.e. COM1:9600,n,8,1)  Then you fill in the rest.  I do this, then fill in the remainder of the fields I NEED to set.  Aside from the easy ones like BAUD rate, parity, data and stop bits, these are the ones I use:

      dcb.fBinary = 1;
      dcb.fOutxCtsFlow = 1;
      dcb.fOutX = dcb.fInX = 1;
      dcb.fRtsflow = 1;
      dcb.XonChar = XON; // ascii 0x11
      dcb.XoffChar = XOFF; // ascii 0x13
      dcb.XonLim = inQue/4;
      dcb.XoffLim = (inQue*3)/4;

It looks very similar to what you have.  That's why I think that something is missing that BuildCommDCB is handling for me.  This code fragment works for me at ANY speed and (processor speed willing) never loses any characters.  It lacks the customization of what TYPE of handshaking to use, though.  I think most of them default to ON anyway, except for XON/XOFF.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.