Shop OBEX P1 Docs P2 Docs Learn Events
Win32 serial port, update — Parallax Forums

Win32 serial port, update

ArchiverArchiver Posts: 46,084
edited 2001-03-02 15:20 in General Discussion
Greetings all,

Never did receive any response on my Win32 programming question, but I
have figured out the problem and thought I would post it for future
users.

I am writing a Windows program (Win32) that will simply send a command
byte to the BS2, which interprets it and outputs the appropriate signals
to its I/O pins. The serial port used to program the stamp is the I/O
line here as well, i.e. I use SERIN 16, ...., not the normal 0-15 I/O
pins.

I'm using the OEM BS2 chip set sold by Peter Anderson from his web
site, which I highly recommend. A BS2 for $19., fast service and an
array of other parts often used with the BS2 - can't beat it.

I put together his circuit, got it working, and forgot he had a second
schematic to "Provide Communication with a PC Serial Port..." He straps
RTS (Request to Send) to CTS (Clear to Send) and DTR (Data Terminal
Ready) to DSR (Data Set Ready). I always seem to get these confused. In
addition his regular Homebrew BS2 circuit uses DTR to drive the reset
line (keep it high), and grounding it to do a hardware reset, therefore
in my circuit I needed to keep DTR at logical 1, which is it's disabled
state.

Here is the Windows code I ended up using. Hope it helps someone.


DCB dcb;
static HANDLE hCom;
BOOL fSuccess;
char *pcCommPort = "COM1";
DWORD byteswritten;

// Now the serial port

hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened
w/exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use
OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);

if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
wsprintf(temp,"CreateFile Error %d",GetLastError());
MessageBox(hwnd,temp,"Debug",MB_OK | MB_ICONSTOP);
}

// We will build on the current configuration, and skip setting the
size
// of the input and output buffers with SetupComm.

fSuccess = GetCommState(hCom, &dcb);

if (!fSuccess)
{
// Handle the error.
wsprintf(temp,"GetCommState Error %d",GetLastError());
MessageBox(hwnd,temp,"Debug",MB_OK | MB_ICONSTOP);
}

// Fill in the DCB: baud=9,600 bps, 8 data bits, no parity, and 1 stop
bit.

// Initialize the DCBlength member.
dcb.DCBlength = sizeof (dcb);

// Get the default port setting information.
GetCommState (hCom, &dcb);

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

// Configure the port according to the specifications of the DCB
// structure.

fSuccess = SetCommState(hCom, &dcb);

if (!fSuccess)
{
// Handle the error.
wsprintf(temp,"SetCommState Error %d",GetLastError());
MessageBox(hwnd,temp,"Debug",MB_OK | MB_ICONSTOP);
}

wsprintf(temp,"Serial port %s successfully
reconfigured.\n",pcCommPort);
MessageBox(hwnd,temp,"Debug",MB_OK | MB_ICONSTOP);

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2001-03-02 15:20
    Nice job Theron,

    Typically on this group you don't get an answer only when the answer isn't
    readily known.

    Thanks for the code.
    Bill

    Original Message
    From: Theron Wierenga [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=_D2fE6QczLh8FMNIlJ7i31PRjFDqlSRFxdR75uVphYlJ9v0fRPh2asY8rFm2cy1fXACY00bqkl4Wn2dnMZSFy5e4]twiereng@m...[/url
    Sent: Thursday, March 01, 2001 8:35 PM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] Win32 serial port, update


    Greetings all,

    Never did receive any response on my Win32 programming question, but I
    have figured out the problem and thought I would post it for future
    users.

    I am writing a Windows program (Win32) that will simply send a command
    byte to the BS2, which interprets it and outputs the appropriate signals
    to its I/O pins. The serial port used to program the stamp is the I/O
    line here as well, i.e. I use SERIN 16, ...., not the normal 0-15 I/O
    pins.

    I'm using the OEM BS2 chip set sold by Peter Anderson from his web
    site, which I highly recommend. A BS2 for $19., fast service and an
    array of other parts often used with the BS2 - can't beat it.

    I put together his circuit, got it working, and forgot he had a second
    schematic to "Provide Communication with a PC Serial Port..." He straps
    RTS (Request to Send) to CTS (Clear to Send) and DTR (Data Terminal
    Ready) to DSR (Data Set Ready). I always seem to get these confused. In
    addition his regular Homebrew BS2 circuit uses DTR to drive the reset
    line (keep it high), and grounding it to do a hardware reset, therefore
    in my circuit I needed to keep DTR at logical 1, which is it's disabled
    state.

    Here is the Windows code I ended up using. Hope it helps someone.


    DCB dcb;
    static HANDLE hCom;
    BOOL fSuccess;
    char *pcCommPort = "COM1";
    DWORD byteswritten;

    // Now the serial port

    hCom = CreateFile( pcCommPort,
    GENERIC_READ | GENERIC_WRITE,
    0, // comm devices must be opened
    w/exclusive-access
    NULL, // no security attributes
    OPEN_EXISTING, // comm devices must use
    OPEN_EXISTING
    0, // not overlapped I/O
    NULL // hTemplate must be NULL for comm devices
    );

    if (hCom == INVALID_HANDLE_VALUE)
    {
    // Handle the error.
    wsprintf(temp,"CreateFile Error %d",GetLastError());
    MessageBox(hwnd,temp,"Debug",MB_OK | MB_ICONSTOP);
    }

    // We will build on the current configuration, and skip setting the
    size
    // of the input and output buffers with SetupComm.

    fSuccess = GetCommState(hCom, &dcb);

    if (!fSuccess)
    {
    // Handle the error.
    wsprintf(temp,"GetCommState Error %d",GetLastError());
    MessageBox(hwnd,temp,"Debug",MB_OK | MB_ICONSTOP);
    }

    // Fill in the DCB: baud=9,600 bps, 8 data bits, no parity, and 1 stop
    bit.

    // Initialize the DCBlength member.
    dcb.DCBlength = sizeof (dcb);

    // Get the default port setting information.
    GetCommState (hCom, &dcb);

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

    // Configure the port according to the specifications of the DCB
    // structure.

    fSuccess = SetCommState(hCom, &dcb);

    if (!fSuccess)
    {
    // Handle the error.
    wsprintf(temp,"SetCommState Error %d",GetLastError());
    MessageBox(hwnd,temp,"Debug",MB_OK | MB_ICONSTOP);
    }

    wsprintf(temp,"Serial port %s successfully
    reconfigured.\n",pcCommPort);
    MessageBox(hwnd,temp,"Debug",MB_OK | MB_ICONSTOP);



    Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Sign In or Register to comment.