Please help with Serial Comms

Discussion in 'C++' started by salsipius, Oct 12, 2005.

  1. salsipius

    salsipius Guest

    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 );

    }
    salsipius, Oct 12, 2005
    #1
    1. Advertising

  2. salsipius

    Puppet_Sock Guest

    salsipius wrote:
    > 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
    Puppet_Sock, Oct 12, 2005
    #2
    1. Advertising

  3. salsipius

    Jay Nabonne Guest

    On Wed, 12 Oct 2005 09:34:40 -0700, salsipius wrote:

    >
    > char in[13];
    > char command[20];
    >
    > ...
    > end = serial.ReadData(&in,20);


    This looks really suspicious to me. Should the 20 be "sizeof(in)"?

    - Jay
    Jay Nabonne, Oct 12, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim McCoy

    single wire serial comms module

    Tim McCoy, Jun 12, 2005, in forum: VHDL
    Replies:
    5
    Views:
    1,166
    info_
    Jun 23, 2005
  2. Dave Brown

    Cross Platform Comms

    Dave Brown, Jul 8, 2005, in forum: Java
    Replies:
    10
    Views:
    751
    Thomas Weidenfeller
    Jul 18, 2005
  3. Andrew E
    Replies:
    1
    Views:
    349
  4. =?Utf-8?B?Sm9uYXRoYW4gQXR0cmVl?=

    Serial port comms with ASP.Net

    =?Utf-8?B?Sm9uYXRoYW4gQXR0cmVl?=, Sep 25, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    1,078
    Eliyahu Goldin
    Sep 26, 2006
  5. Perl for serial comms

    , Sep 23, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    127
    Bob Walton
    Sep 24, 2005
Loading...

Share This Page