Shop OBEX P1 Docs P2 Docs Learn Events
c++ serial not reading bs2 — Parallax Forums

c++ serial not reading bs2

ArchiverArchiver Posts: 46,084
edited 2003-07-31 20:09 in General Discussion
Hello all,

I am using Visual Studio.NET c++ to interface with my stamp, however
my code is not reading what my bs2 is sending (possibly a timeout
error?) The code I am using is essentially what is found on MSDN for
serial communications. I have also modified my carrier board
correctly (i know this because other com port testing programs pick
up my stamp and read my from the stamp fine).

I was hoping someone could read through the short c++ code to see if
I am initializing anything wrong.

The code can be found at:
http://www.geocities.com/zatakur/cpp/serial.txt

And my BS2 code is simply:

'{$STAMP BS2}
serData VAR BYTE
serData = 10
Main:
SEROUT 16, 16780,[noparse][[/noparse]serData]
End

-Dave Boz

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-07-31 14:26
    I don't see anything wrong with your code, but I've done a considerable amount
    of development involving stamps and PCs... here's the code I use. It's part of
    a class I wrote, which I'd be happy to send you if you'd like it. The important
    part of the code follows. Note that it was written for a UNICODE system, which
    is why it uses wide chars and the TEXT() macro. It should be easy enough for
    you to convert. It's been working like a charm for me for more than 2 years...

    WCHAR Buffer[noparse][[/noparse]80];
    swprintf(Buffer, TEXT("COM%d:"), PortNum)[noparse];)[/noparse]

    // Open the serial port.
    hPort = CreateFile(Buffer, GENERIC_READ | GENERIC_WRITE, 0, NULL,
    OPEN_EXISTING, 0, NULL);

    if (hPort == INVALID_HANDLE_VALUE)
    {
    //LastError = ERR_PORT;
    //WriteLog(TEXT("Unable to open the COM port"), true,
    TEXT("CSerialComm::Open()"));
    return false;
    }

    //swprintf(Buffer, TEXT("Serial connection opened on COM%d"), PortNum);
    //WriteLog(Buffer);

    PortDCB.DCBlength = sizeof(DCB);

    GetCommState(hPort, &PortDCB); // Get the default port setting information.

    // Change the port settings for no flow control
    PortDCB.BaudRate = 9600; // Current baud
    PortDCB.fBinary = TRUE; // Binary mode; no EOF check
    PortDCB.fParity = FALSE; // Enable parity checking.
    PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
    PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
    PortDCB.fDtrControl = DTR_CONTROL_DISABLE; // DTR flow control type
    PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
    PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
    PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
    PortDCB.fInX = FALSE; // No XON/XOFF in flow control
    PortDCB.fErrorChar = FALSE; // Disable error replacement.
    PortDCB.fNull = FALSE; // Disable null stripping.
    PortDCB.fRtsControl = RTS_CONTROL_DISABLE; // RTS flow control
    PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on error.
    PortDCB.ByteSize = 8; // Number of bits/bytes, 4-8
    PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
    PortDCB.StopBits = TWOSTOPBITS; // 0,1,2 = 1, 1.5, 2

    if (!SetCommState(hPort, &PortDCB))
    {
    Close();
    return false;
    }

    // modify timeouts
    GetCommTimeouts(hPort, &CommTimeouts);

    CommTimeouts.ReadIntervalTimeout = 2; // at 9600 baud, should be only 0.83, so
    this is sufficient
    CommTimeouts.ReadTotalTimeoutMultiplier = 0;
    CommTimeouts.ReadTotalTimeoutConstant = 20;
    CommTimeouts.WriteTotalTimeoutMultiplier = 0;
    CommTimeouts.WriteTotalTimeoutConstant = 0;

    if (!SetCommTimeouts(hPort, &CommTimeouts))
    {
    LastError = ERR_TIMEOUT;
    Close();
    //WriteLog(TEXT("Unable to set COM time-out parameters"), true,
    TEXT("CSerialComm::Open()"));

    return false;
    }

    return true;



    Original Message
    From: zatakur [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=A0E7_my223s7GLJFQO9M17ve9iWn34sdHd1XLmum5S8L-my8fwzAClIwSGLxjgxUdm9pKkXRbx2lDg]zatakur@y...[/url
    Sent: Thursday, July 31, 2003 2:23 AM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] c++ serial not reading bs2


    Hello all,

    I am using Visual Studio.NET c++ to interface with my stamp, however
    my code is not reading what my bs2 is sending (possibly a timeout
    error?) The code I am using is essentially what is found on MSDN for
    serial communications. I have also modified my carrier board
    correctly (i know this because other com port testing programs pick
    up my stamp and read my from the stamp fine).

    I was hoping someone could read through the short c++ code to see if
    I am initializing anything wrong.

    The code can be found at:
    http://www.geocities.com/zatakur/cpp/serial.txt

    And my BS2 code is simply:

    '{$STAMP BS2}
    serData VAR BYTE
    serData = 10
    Main:
    SEROUT 16, 16780,[noparse][[/noparse]serData]
    End

    -Dave Boz



    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject and Body
    of the message will be ignored.


    Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
  • ArchiverArchiver Posts: 46,084
    edited 2003-07-31 14:34
    If I'd only been a little slower hitting that send button... I had a brainwave.
    I think the reason your code doesn't work is that a colon is required after the
    port name when you pass it to CreateFile(). You should be passing "COM1:".
    Check the MSDN docs.

    Original Message
    From: zatakur [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=ND3NK2rsx7RmT_l6RdjWA0gTEk3BluPUxieXz3L2FZdHBNWICppuGSE1OoyPwOyOO-QipcR0Vg]zatakur@y...[/url
    Sent: Thursday, July 31, 2003 2:23 AM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] c++ serial not reading bs2


    Hello all,

    I am using Visual Studio.NET c++ to interface with my stamp, however
    my code is not reading what my bs2 is sending (possibly a timeout
    error?) The code I am using is essentially what is found on MSDN for
    serial communications. I have also modified my carrier board
    correctly (i know this because other com port testing programs pick
    up my stamp and read my from the stamp fine).

    I was hoping someone could read through the short c++ code to see if
    I am initializing anything wrong.

    The code can be found at:
    http://www.geocities.com/zatakur/cpp/serial.txt

    And my BS2 code is simply:

    '{$STAMP BS2}
    serData VAR BYTE
    serData = 10
    Main:
    SEROUT 16, 16780,[noparse][[/noparse]serData]
    End

    -Dave Boz



    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject and Body
    of the message will be ignored.


    Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
  • ArchiverArchiver Posts: 46,084
    edited 2003-07-31 20:09
    ah..HA!! thanks for your help. Looking over yours and mine, i
    noticed it was a time out error. Here is how i fixed it:

    // Change the COMMTIMEOUTS structure settings.
    // I set them all to zero.
    CommTimeouts.ReadIntervalTimeout = 0;
    CommTimeouts.ReadTotalTimeoutMultiplier = 0;
    CommTimeouts.ReadTotalTimeoutConstant = 0;
    CommTimeouts.WriteTotalTimeoutMultiplier =0;
    CommTimeouts.WriteTotalTimeoutConstant = 0;



    -Dave Boz



    --- In basicstamps@yahoogroups.com, "Meador, Ryan D ERDC-CRREL-NH"
    <Ryan.D.Meador@e...> wrote:
    > If I'd only been a little slower hitting that send button... I
    had a brainwave. I think the reason your code doesn't work is that
    a colon is required after the port name when you pass it to
    CreateFile(). You should be passing "COM1:". Check the MSDN docs.
    >
    >
    Original Message
    > From: zatakur [noparse][[/noparse]mailto:zatakur@y...]
    > Sent: Thursday, July 31, 2003 2:23 AM
    > To: basicstamps@yahoogroups.com
    > Subject: [noparse][[/noparse]basicstamps] c++ serial not reading bs2
    >
    >
    > Hello all,
    >
    > I am using Visual Studio.NET c++ to interface with my stamp,
    however
    > my code is not reading what my bs2 is sending (possibly a timeout
    > error?) The code I am using is essentially what is found on MSDN
    for
    > serial communications. I have also modified my carrier board
    > correctly (i know this because other com port testing programs
    pick
    > up my stamp and read my from the stamp fine).
    >
    > I was hoping someone could read through the short c++ code to see
    if
    > I am initializing anything wrong.
    >
    > The code can be found at:
    > http://www.geocities.com/zatakur/cpp/serial.txt
    >
    > And my BS2 code is simply:
    >
    > '{$STAMP BS2}
    > serData VAR BYTE
    > serData = 10
    > Main:
    > SEROUT 16, 16780,[noparse][[/noparse]serData]
    > End
    >
    > -Dave Boz
    >
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the
    Subject and Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to
    http://docs.yahoo.com/info/terms/
Sign In or Register to comment.