Thread: Serial Communications with win32 api

  1. #1
    Blackthorne
    Guest

    Serial Communications with win32 api

    I am trying to control a serial port with the win32 api in C. I believe that I have caught all the critical elements, but it does not seem to work.

    I am trying to control the DTR/RTS signal on the serial port in order to control an X10 device. Here is the code where I try to access the serial port.

    Note: lpComPort == COM2

    if ((hComm=CreateFile(lpComPort, GENERIC_READ | GENERIC_WRITE, 0, 0 , OPEN_EXISTING, 0 ,0))==INVALID_HANDLE_VALUE) {
    printf("Error creating output to COM Port\n");
    exit(1);
    } /* endif */


    if (SendSignal(ComPort, Reset, hComm)==FALSE) printf("Reset Failed\n");
    flushall();
    if (SendSignal(ComPort, Standby, hComm)==FALSE) printf("Standby Failed\n");
    flushall();
    if (SendSignal(ComPort, Header, hComm)==FALSE) printf("Header Failed\n");
    flushall();
    if (SendSignal(ComPort, Signal, hComm)==FALSE) printf("Signal Failed\n");
    flushall();
    if (SendSignal(ComPort, Footer, hComm)==FALSE) printf("Footer Failed\n");
    flushall();

    CloseHandle(hComm);
    return 0;

    } /* end MakeSignal */


    int SendSignal(int ComPort, char *Signal, HANDLE Commi)
    {
    int i;
    int k;
    BOOL Result;

    k=strlen(Signal);
    for (i=0;i<k;i++) {
    if (Signal[i]=='0') {
    Result=ControlCom(0,1, Commi);
    } else if (Signal[i]=='1') {
    Result=ControlCom(1,0, Commi);
    } else if (Signal[i]=='2') {
    Result=ControlCom(0,0, Commi);
    } else if (Signal[i]=='3') {
    Result=ControlCom(1,1, Commi);
    } /* endif */
    SleepEx(3,FALSE);
    } /* end for */

    return Result;
    } /* end SendSignal */


    int ControlCom(int RTS, int DTR, HANDLE Commi)
    {
    DWORD dwFunction;

    if (RTS==0) dwFunction=CLRRTS;
    if (RTS==1) dwFunction=SETRTS;
    EscapeCommFunction(Commi,dwFunction);

    if (DTR==1) dwFunction=(SETDTR);
    if (DTR==0) dwFunction=(CLRDTR);

    return EscapeCommFunction(Commi,dwFunction);

    } /* end ControlCom */

  2. #2
    Blackthorne
    Guest
    Never mind, found my problem....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serial Programming the Win32 api
    By Hexagram1000 in forum Windows Programming
    Replies: 5
    Last Post: 07-18-2008, 08:16 PM
  2. Win32 API or Win32 SDK?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-20-2005, 03:26 PM
  3. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  4. OLE Clipboard :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 08-11-2002, 05:57 PM
  5. Thread Synchronization :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 08-09-2002, 09:09 AM