-
February 21st, 2012, 12:21 PM
#1
Com-port sometimes work incorrectly...
I wrote 2 program
Reader:
Code:
for (;;)
while (!dwRead)
{
ClearCommError(hComPort, &Err, &ComStat);
dwRead = ComStat.cbInQue;
Sleep(10);
be_inside++;
if (be_inside > 30000) break;
}
if (be_inside > 30000)
break;
ReadFile(hComPort, read, min(dwRead, 256), &tmpBytesRead, NULL);
read[dwRead] = '\0';
for (int i=0; i < dwRead; i++)
{
int temp = 0;
temp = read[i];
std::cout<<temp<<' ';
}
std::cout<<'\n';
buff[0] = 31;
buff[1] = 0x66;
buff[2] = 'C';
Sleep(5);
WriteFile(hComPort, buff, 3, dwBytesWritten, NULL);
}
Writer:
Code:
BYTE buff[50];
BYTE read[50];
LPDWORD dwBytesWritten = new DWORD;
for (char c = 'A'; c <= 'Z'; c++)
{
buff[0] = 31;
buff[1] = 0x66;
buff[2] = c;
WORD CRC = CRC16(3, buff);
buff[3] = LOBYTE(CRC);
buff[4] = HIBYTE(CRC);
PurgeComm( hComPort, PURGE_TXCLEAR);
WriteFile(hComPort, buff, 5, dwBytesWritten, NULL);
COMSTAT ComStat;
DWORD Err;
ClearCommError(hComPort, &Err, &ComStat);
DWORD dwRead = ComStat.cbInQue;
int be_inside = 0;
while (!dwRead)
{
ClearCommError(hComPort, &Err, &ComStat);
dwRead = ComStat.cbInQue;
Sleep(1);
be_inside++;
if (be_inside > 10000) break;
}
if (be_inside > 10000)
continue;
DWORD tmpBytesRead = 256;
ReadFile(hComPort, read, min(dwRead, 256), &tmpBytesRead, NULL);
PurgeComm(hComPort, PURGE_RXCLEAR);
}
But sometimes a package from Writer differs with a package from Reader. Why problem is appeared?
Last edited by AKE; February 21st, 2012 at 12:26 PM.
-
February 21st, 2012, 12:38 PM
#2
Re: Com-port sometimes work incorrectly...
How are these snippets related? Separate programs/threads?
As far as I can see you don't bother how many bytes you actually read in the first snippet. You just assume you get min(dwRead, 256) bytes.
In the second snippet you call PurgeComm, What if not all previously written bytes have been transferred yet? The same goes for the PURGE_RXCLEAR
There are many good com port classes available for free. Why not use one of those instead?
-
February 21st, 2012, 01:44 PM
#3
Re: Com-port sometimes work incorrectly...
Originally Posted by S_M_A
How are these snippets related? Separate programs/threads?
As far as I can see you don't bother how many bytes you actually read in the first snippet. You just assume you get min(dwRead, 256) bytes.
In the second snippet you call PurgeComm, What if not all previously written bytes have been transferred yet? The same goes for the PURGE_RXCLEAR
There are many good com port classes available for free. Why not use one of those instead?
Separate program.
surprising, that this error is appeared only in special kind of transferring (not always), like this:
1fh 67h x x x
1fh 67h x x
In other cases I don't meet this error.
Maybe it's firewall work or mask or special command?
Please give links on normal com-port classes...
Last edited by AKE; February 21st, 2012 at 02:30 PM.
-
February 21st, 2012, 04:21 PM
#4
Re: Com-port sometimes work incorrectly...
-
February 22nd, 2012, 03:12 AM
#5
Re: Com-port sometimes work incorrectly...
S_M_A , Thanks, I'm testing first link... Maybe problems will soon
-
February 22nd, 2012, 10:02 AM
#6
Re: Com-port sometimes work incorrectly...
S_M_A
Execuse me.
The same error. I used first your class such:
Reader
Code:
COMMCONFIG config;
CSerialPort::GetDefaultConfig(1, config);
CSerialPort port;
BYTE pBuf[256];
port.Open(L"COM1", CBR_9600, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::NoFlowControl);
bool d;
for (int i = 1; i <30; i++)
{
d = port.DataWaiting(300000);
if (d)
{
DWORD dwCount = port.BytesWaiting();
try
{
port.Read(pBuf, dwCount);
CString str;
if (pBuf[0] != 31)
{
d=d; //for breakpoint if error;
}
for (int i = 0; i < dwCount; i++)
{
//str += (int) pBuf[i];
}
port.Write(pBuf, dwCount);
}
catch(CSerialException* pEx)
{
int i = 0;
}
}
}
return 0;
}
Writer:
Code:
COMMCONFIG config;
CSerialPort::GetDefaultConfig(1, config);
CSerialPort port;
BYTE pBuf[256];
port.Open(L"COM3", CBR_9600, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::NoFlowControl);
for (char ch = 'A'; ch <= 'Z'; ch++)
{
pBuf[0] = 31;
pBuf[1] = 0x67;
pBuf[2] = ch;
WORD CRC = CRC16(3, pBuf);
pBuf[3] = LOBYTE(CRC);
pBuf[4] = HIBYTE(CRC);
try
{
port.Write(pBuf, 5);
}
catch(CSerialException* pEx)
{
if (pEx->m_dwError == ERROR_IO_PENDING)
{
int i = 0;//for breakpoint;
}
}
bool d = true;
if (port.BytesWaiting() == 0)
d = port.DataWaiting(10000);
if (d)
{
DWORD dwCount = port.BytesWaiting();
try
{
port.Read(pBuf, dwCount);
}
catch(CSerialException* pEx)
{
int i = 0;//for breakpoint
}
}
}
if i set pBuf[1] = 0x66; pBuff[1] = 0x68 all work without error... Mistery)
P.S. This two snippets from two programs on different computers...
Last edited by AKE; February 22nd, 2012 at 10:30 AM.
-
February 22nd, 2012, 10:32 AM
#7
Re: Com-port sometimes work incorrectly...
Before I dig any deeper into this, I neither have a serial cable nor 2 ports/machines available so I have to read the documentation and the code, what compiler/platform do you use?
-
February 22nd, 2012, 10:43 AM
#8
Re: Com-port sometimes work incorrectly...
The zip file contains a test code as well. Have you runned that? I guess it should be possible to run that code on both 2 computers with a connected serial cable to do a more thorough check. If that works you have done something wrong.
According to the documentation the code requires MSVC 2005 or newer.
-
February 22nd, 2012, 11:36 AM
#9
Re: Com-port sometimes work incorrectly...
WinXP
VisualStudio2008
Now I'm testing including program.
-
February 22nd, 2012, 11:49 AM
#10
Re: Com-port sometimes work incorrectly...
Program copies are not bind together.
About my program
Strange situation:
Notebook->PC - has bugs...
PC->Notebook - absent...
I use USB2COM connector...
-
February 22nd, 2012, 03:38 PM
#11
Re: Com-port sometimes work incorrectly...
Weird, since Win3.11 the only issues I have with RS232 communication is whether to use a twisted cable or not...
USB2COM devices work well these days, even when using speeds of several Mbits/sec, but of course there's always a possibility for malfunction. Just to rule it out, can you get another one (preferably another brand) to try with?
You might also try using the test application in one end and a terminal program in the other end and see if that makes a difference. If you have issues change the amount of data that is sent at the same time to rule out buffer handling issues.
-
February 22nd, 2012, 04:04 PM
#12
Re: Com-port sometimes work incorrectly...
S_M_A,
I tested it without USB2COM, but this error is appeared again...
-
February 22nd, 2012, 05:09 PM
#13
Re: Com-port sometimes work incorrectly...
Have you tried with a terminal program? I.e. the test application on the PC, terminal program on the notebook and also the other way around.
Edit: Another test would also be to open a terminal program at both platforms. Send an ascii text file of say about 100 bytes from one side at a time and check if all characters are transferred. A modern hardware shouldn't drop any characters at 115200 baud even without any flow control.
Last edited by S_M_A; February 22nd, 2012 at 05:15 PM.
-
February 23rd, 2012, 05:26 AM
#14
Re: Com-port sometimes work incorrectly...
S_M_A
Please, advice me normal free terminal program...
-
February 23rd, 2012, 05:33 AM
#15
Re: Com-port sometimes work incorrectly...
In XP you have HyperTerminal but that's really a crappy one so go for putty instead. The full version is capeable of running all kinds of protocols so you have good use for it in other situations as well.
http://www.putty.org/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|