Please help with Serial Comms

S

salsipius

I can't figure this out, Please Help!!!! I would appreciate any remarks
or opinions or help about the code below.

A you can see I am writing and reading from the serial port, that is
going OK MOST of the time. My big problem is when I want to break the
while loop. I am expecting a message in HEX to come in from the serial
port. for instance "0004 600A", which I get most of the time, sometimes
I just get random ASCII chars, anyway to the point

Question #1 is the way I am storing the read return(it seems to work
alright) the best way to do it, char in[13] ?

Question #2 I need to test the response in terms of binary?
0000 0000 0000 0100 0110 0000 0000 1010

I can manage to test the bit that I am interested in but how to get to
this point I am not to sure about.

Lastly, any remarks or bug fixes on the general structure of the my
code would be great

//************************************************************************************************************//

int _tmain(int argc, _TCHAR* argv[])
{
CSerial serial;
//CSyncSerialComm serial2("COM1");

char in[13];
char command[20];
DWORD bytesRead;
int value=1000;
int end;
int test;

serial.Open(1, 9600);
//serial.HomePosition(4);
while(1)
{
printf("\nEnter Command >>");
scanf("%d", &value);
sprintf(command, "4 lr %d\r4 m\r4 st\r", value);
serial.SendData(command, sizeof(command));
end = serial.ReadData(&in,20);
in[end] = '\0';
printf("value = %s",in);

if((int)in[7] > 4)
break;
}
serial.Close();
getchar();

return(0);
}


//***************************************Writing
Data**************************************//

bool CSerial::WriteCommByte( unsigned char ucByte )
{
bool bWriteStat;
DWORD dwBytesWritten;

bWriteStat = WriteFile( m_hIDComDev, (LPSTR) &ucByte, 1,
&dwBytesWritten, &m_OverlappedWrite );
if( !bWriteStat && ( GetLastError() == ERROR_IO_PENDING ) ){
if( WaitForSingleObject( m_OverlappedWrite.hEvent, 1000 ) )
dwBytesWritten = 0;
else{
GetOverlappedResult( m_hIDComDev, &m_OverlappedWrite,
&dwBytesWritten, FALSE );
m_OverlappedWrite.Offset += dwBytesWritten;
}
}

return( TRUE );

}

int CSerial::SendData( const char *buffer, int size )
{

if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

DWORD dwBytesWritten = 0;
int i;
for( i=0; i<size; i++ ){
WriteCommByte( buffer );
dwBytesWritten++;
}

return( (int) dwBytesWritten );

}

//************************************Reading
Data****************************************************//


int CSerial::ReadDataWaiting( void )
{

if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

DWORD dwErrorFlags;
COMSTAT ComStat;

ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );

return( (int) ComStat.cbInQue );

}

int CSerial::ReadData( void *buffer, int limit )
{

if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

bool bReadStatus;
DWORD dwBytesRead, dwErrorFlags;
COMSTAT ComStat;

ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
//if( !ComStat.cbInQue ) return( 0 );

dwBytesRead = (DWORD) ComStat.cbInQue;
if( limit < (int) dwBytesRead ) dwBytesRead = (DWORD) limit;

bReadStatus = ReadFile( m_hIDComDev, buffer, dwBytesRead,
&dwBytesRead, &m_OverlappedRead );
if( !bReadStatus ){
if( GetLastError() == ERROR_IO_PENDING ){
WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );
return( (int) dwBytesRead );
}
return( 0 );
}
return( (int) dwBytesRead );

}
 
P

Puppet_Sock

salsipius said:
I can't figure this out, Please Help!!!! I would appreciate any remarks
or opinions or help about the code below.

A you can see I am writing and reading from the serial port, that is
going OK MOST of the time. My big problem is when I want to break the
while loop. I am expecting a message in HEX to come in from the serial
port. for instance "0004 600A", which I get most of the time, sometimes
I just get random ASCII chars, anyway to the point

Question #1 is the way I am storing the read return(it seems to work
alright) the best way to do it, char in[13] ?

Question #2 I need to test the response in terms of binary?
0000 0000 0000 0100 0110 0000 0000 1010

I can manage to test the bit that I am interested in but how to get to
this point I am not to sure about.

Lastly, any remarks or bug fixes on the general structure of the my
code would be great
[snip]

Parts of your post are on topic, parts are off topic.

First note that the hardware communication you are talking about is
clearly an extension to C++. The standard lang does not know from
serial ports. So, it's very hard to answer your question about
what is going wrong. But the "usual suspects" are things like:
- You may have missed the port having some extra behaviour or
signals indicating conditions. Maybe there is a system flag
or message. Maybe it does something on special conditions.
This is all off topic in this group.
- You may have to set some flag into the port to get it to behave
the way you want. Again, off topic here.
- You may have some signed/unsigned thing or some such type thing.
Maybe you've got more data in the port than fits in your buffer.
This is on-topic here, but it's hard to tell if this is the case,
since I don't have the rest of the class, nor the header files,
nor the docs for using your hardware nor compiler.

My suggestion is, find a news group where people talk about your
specific hardware, or your specific OS, or your specific compiler.
Socks
 

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top