C# serial connection problems

  • Thread starter Daniel Passwater via DotNetMonster.com
  • Start date
D

Daniel Passwater via DotNetMonster.com

I've written an app in C# in the compact framework. I've got a serial connection with a device running embedded C. We are using no flow control. That's not optional at this point. We are talking at 19200 baud, 8 bits, no parity, one stop bit. I can't get stable communication into my side.

Symptoms: If we put interrupts in the embedded C (also not an option), I get my data just fine. However, without the interrupts, I only get the first byte.

I have a tester that sends the data as I key it in, and I receive all of the data. However, if I user Hyper Terminal, sending all of the data from a file, I only get one byte. Here's the strange thing: If I close my form and reopen it, while continuing to send data via Hyper Terminal each time I open it, I move up a character in the line of data each time. My form is called from a main form, which I don't close each time; but I initialize my buffer from the constructor of the form. I also call dispose each time I close the form.

Questions:
1. It seems that my event handler is just not fast enough to recover and get the next byte. Is there anything that I can do to speed things up?
2. Is there a way to access the hardware buffer where the data is being stored during the transfer?

Thanks in advance for any insights.
 
C

Claire

The following was written by me in visual c++ for use on a windows CE device
talking to a pc. I had to use flow control on wired comms or it wouldn't
work. Flow control turned off for use with our CE device's IR connection.
Sorry, I know you're using compact framework but I assume you're utilising
some object that allows you to set up SetCommState values.
Maybe it will help.

void CJFMComport::Connect(BYTE portnum)
{
CString Port;
int CaseCounter = 0;
DCB dcb;
WORD result = E_OK;
BOOL Finished = false;

if (m_hComport != INVALID_HANDLE_VALUE)
{
DoShowError(E_PORTOPEN, false);
return;
}

// Create the port string
Port.Format(_T("COM%d:"),portnum);

while ((result == E_OK)&&(Finished == false))
{
switch (CaseCounter)
{

case 0:
{
// Open port
m_hComport = CreateFile(Port,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (m_hComport == INVALID_HANDLE_VALUE)
result = E_UNOPPORT;

break;
}// case 0

case 1:
{

// Set timeouts
COMMTIMEOUTS ct;
ct.ReadIntervalTimeout = -1; // 2 sec between 2 bytes signifys time
out
ct.ReadTotalTimeoutConstant = 1000;
ct.ReadTotalTimeoutMultiplier = 0;
ct.WriteTotalTimeoutConstant = 1000;
ct.WriteTotalTimeoutMultiplier = 0;

if (!SetCommTimeouts(m_hComport, &ct))
result = E_UNTIMEOUT;

break;

}//case 1

case 2:
{
// Read current communication parameters
dcb.DCBlength = sizeof(DCB);
if (!GetCommState(m_hComport, &dcb))
result = E_UNDCB;

break;
}// case 2

case 3:
{
// on the DI we only have the CTS, RTS, TX and RX pins available
// so disable other pins and types of flow control
// and enable RTS/CTS flow control.
dcb.DCBlength = sizeof(DCB);
dcb.BaudRate = ((CCollectorApp*)(AfxGetApp()))->nBaudRate;
dcb.fBinary = true;
dcb.fParity = false;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fDtrControl = DTR_CONTROL_ENABLE;// Set DTR high
dcb.fOutX = false;//x on/off flow control not used
dcb.fInX = false;// x on/off flow control not used
dcb.fOutxDsrFlow = false;// NO DSR

// If this is wired RS232 then use CTS handshaking
if (portnum != 5)
{
dcb.fOutxCtsFlow = true;// CTS/RTS flow control used
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; // CTS/RTS flow
control used
}
else
{
// IR(flow control is turned off)
dcb.fOutxCtsFlow = false;
dcb.fRtsControl = RTS_CONTROL_ENABLE; // Keep RTS high
}


if (!SetCommState(m_hComport, &dcb))
result = E_UNDCB;

break;
}// case 3

case 4:
{
// Set buffer sizes
if (SetupComm(m_hComport, TEMPBUFFSIZE * 2, TEMPBUFFSIZE * 2) ==
false)
result = E_UNBUFFSIZE;

break;
}

default:
Finished = true;
}//switch (CaseCounter)

// increment the counter
CaseCounter++;

}//while (CaseCounter < 100)

if (result != E_OK)
{
Disconnect();
DoShowError(result, true);
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top