Results 1 to 5 of 5

Thread: Cannot figure out how to write to the serial port in C++

  1. Cannot figure out how to write to the serial port in C++

    I've spent 2 days trying to figure out how to write to the serial port in C++. Here is what I've been trying. I'm compiling in Dev-C++ on windows vista.

    CODE:

    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>


    using namespace std;

    int main(int argc, char *argv[])
    {
    HANDLE hComm;
    char DataBuffer[] = "#31 P1500 T1000 \r\n";
    DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);
    DWORD dwBytesWritten = 0;
    hComm=CreateFile( "COM1",
    GENERIC_READ | GENERIC_WRITE, //bidirectional
    0,
    NULL, //no security
    OPEN_EXISTING, //this must be set; the ports are already created
    FILE_ATTRIBUTE_NORMAL, // maybe with | FILE_FLAG_OVERLAPPED
    NULL );
    if (hComm == INVALID_HANDLE_VALUE)
    {
    printf("Could not open COM1 \n", GetLastError());
    return 0;
    }
    else
    {
    printf("COM1 Succesfully opened \n");
    }
    DCB dcb;

    FillMemory(&dcb, sizeof(dcb), 0);
    if (!GetCommState(hComm, &dcb)) // get current DCB
    // Error in GetCommState
    return FALSE;

    // Update DCB rate.
    dcb.BaudRate = CBR_115200 ;

    // Set new state.
    if (!SetCommState(hComm, &dcb))
    printf("Error in SetCommState. Possibly a problem with the communications port handle or a problem with the DCB structure itself. \n");

    COMMTIMEOUTS timeouts;

    timeouts.ReadIntervalTimeout = 20;
    timeouts.ReadTotalTimeoutMultiplier = 10;
    timeouts.ReadTotalTimeoutConstant = 100;
    timeouts.WriteTotalTimeoutMultiplier = 10;
    timeouts.WriteTotalTimeoutConstant = 100;
    if (!SetCommTimeouts(hComm, &timeouts))

    //THIS IS WHERE I TRY TO WRITE TO hComm or "COM1" as its supposed to be
    WriteFile(hComm, // open file handle
    DataBuffer + dwBytesWritten, // start of data to write
    dwBytesToWrite - dwBytesWritten, // number of bytes to write
    &dwBytesWritten, // number of bytes that were written
    NULL); // no overlapped structure WriteFile(hComm,x,100);

    return 0;
    }


    EndCode

    I don't get any errors, but it won't write to my SSC-32 cause the light stays on which means it is not recieving. :-(

    Basically what I want for now (So I can teach myself based it) is some code that allows me to input a string and then send that string to COM1. I really have very little experience with C++ so this is all very confusing to me. Please help :-D

  2. #2
    Join Date
    Apr 2008
    Location
    Sacramento, CA, USA Area
    Posts
    5,341
    Rep Power
    173

    Re: Cannot figure out how to write to the serial port in C++

    Is your SSC32 set for 115kbaud, and you're sure your com port can handle it, right?
    I Void Warranties´┐Ż

  3. Re: Cannot figure out how to write to the serial port in C++

    yes and yes. I've got the SSC-32 working with my realbasic program and the lynxmotion terminal. I've made sure the comport is not in use when running this program and what not.

  4. #4
    Join Date
    Apr 2008
    Location
    Sacramento, CA, USA Area
    Posts
    5,341
    Rep Power
    173

    Re: Cannot figure out how to write to the serial port in C++

    Looks like the code sample you found is deprecated. I don't believe you can write to the com port in Vista (or XP) directly that way anymore.

    Look-up the MSCOMM Control.
    I Void Warranties´┐Ż

  5. Re: Cannot figure out how to write to the serial port in C++

    sweet. Ill look into that later tonight. Thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Question(s) servo and motor controllers
    By csdude in forum Arbotix, Microcontrollers, Arduino
    Replies: 7
    Last Post: 06-15-2008, 08:23 PM
  2. Kondo web page translated.
    By Droid Works in forum Humanoids, Walkers & Crawlers
    Replies: 4
    Last Post: 10-06-2007, 05:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •