i am trying to send/receive data between 2 computers connected by a serial cable using VC++. I make use of CreateFile,WriteFile and ReadFile to do this.I am able to write to the serial port but i have trouble reading the data from the serial port using another PC. The only data that i can get is garbage values with no meaning.Furthermore, it seems that the number of bytes read is greater than the number of bytes i had written.
Here is some sample code i used:
int SerialConnection::Send(const char* send_block, int length)
{
DWORD bytes_written;
for (int i=0;i<length;i++)
{
WriteFile(m_Port,send_block,length,&bytes_written,NULL);
send_block++;
if (bytes_written <1)
{
AfxMessageBox("Serial Port Error!!!"
break;
}
}
return bytes_written;
}
int SerialConnection::Receive(CString &recv_block)
{
DWORD bytes_read=0;
char buffer[1000];
CString b;
ReadFile(m_Port,buffer,999,&bytes_read,NULL);
buffer[bytes_read]=0;
recv_block=buffer;
return bytes_read;
}
I hope someone can give some pointers on why this is happening. Thanks Alot.....
Here is some sample code i used:
int SerialConnection::Send(const char* send_block, int length)
{
DWORD bytes_written;
for (int i=0;i<length;i++)
{
WriteFile(m_Port,send_block,length,&bytes_written,NULL);
send_block++;
if (bytes_written <1)
{
AfxMessageBox("Serial Port Error!!!"
break;
}
}
return bytes_written;
}
int SerialConnection::Receive(CString &recv_block)
{
DWORD bytes_read=0;
char buffer[1000];
CString b;
ReadFile(m_Port,buffer,999,&bytes_read,NULL);
buffer[bytes_read]=0;
recv_block=buffer;
return bytes_read;
}
I hope someone can give some pointers on why this is happening. Thanks Alot.....