Serial port....pins help C programming

[code] [blue]Id like to be able to turn on and off the pins of the serial
port Using C. If someone can help me with that thanks. Id
additionally like to be able to realtime check whether or not there is
voltage at certain pins, coming from an external sources connected to
them.[/blue]

[red] This is for a robotics project of mine. Any help is greatly
appreciated. Thank you.[/red]

bennydh

[/code]

Comments

  • [b][red]This message was edited by Lundin at 2003-11-25 7:12:23[/red][/b][hr]
    : [code] [blue]Id like to be able to turn on and off the pins of the serial
    : port Using C. If someone can help me with that thanks. Id
    : additionally like to be able to realtime check whether or not there is
    : voltage at certain pins, coming from an external sources connected to
    : them.[/blue]
    :
    : [red] This is for a robotics project of mine. Any help is greatly
    : appreciated. Thank you.[/red]
    :
    : bennydh
    :
    : [/code]
    :

    First of all, it depends on which OS you are running.

    In Windows, you can only talk to the serial port in a way that makes sence to RS-232. You can send data on TxD, recieve data on RxD, use or not use CTS, DSR etc etc. You have to do this through the Windows API functions. Here is an example:

    [code]
    #include

    HANDLE hComm = NULL;
    COMMTIMEOUTS ctmoNew = {0}, ctmoOld;


    /* Init port COM1 to: baudrate 2400, no parity, 8 bits, 1 stopbit. */
    DCB dcbCommPort;
    hComm = CreateFile("COM1",
    GENERIC_READ | GENERIC_WRITE,
    0,
    0,
    OPEN_EXISTING,
    0,
    0);

    if(hComm == INVALID_HANDLE_VALUE)
    ; /* error handling here */

    GetCommTimeouts(hComm,&ctmoOld);
    ctmoNew.ReadTotalTimeoutConstant = 100;
    ctmoNew.ReadTotalTimeoutMultiplier = 0;
    ctmoNew.WriteTotalTimeoutMultiplier = 0;
    ctmoNew.WriteTotalTimeoutConstant = 0;
    SetCommTimeouts(hComm, &ctmoNew);
    dcbCommPort.DCBlength = sizeof(DCB);
    GetCommState(hComm, &dcbCommPort);
    BuildCommDCB("baud=2400 parity=N data=8 stop=1", &dcbCommPort);
    SetCommState(hComm, &dcbCommPort);


    ...

    /* Write to the port*/
    char ch = 'A';
    TransmitCommChar(hComm, ch);

    ...

    /* Read from the port */
    DWORD dwBytesRead;
    char InBuf[100];
    while(1)
    {
    if(ReadFile(hComm, InBuf, 100, &dwBytesRead, NULL))
    {
    if(dwBytesRead)
    {
    InBuff[dwBytesRead] = 0; // null-terminate the string
    // do something with InBuf
    }
    }
    } // end while

    ...


    /* Release COM1 from the program*/
    if(hComm)
    {
    SetCommTimeouts(hComm, &ctmoOld);
    CloseHandle(hComm);
    }
    }

    [/code]

    Check out the Win API documentation (www.msdn.microsoft.com) for details on those functions. The struct DCB will for example contain signals like DTR, CTS etc.

    Regarding the voltage, I'm not sure if I understand what you mean by "check if there is voltage", but according to RS-232 electrical specification (V.28), a logical one is a voltage less than -3V and a logical zero is a voltage higher than +3V. You can check if a pin is either 1 or 0, but you cannot check the exact voltage on the pins from the Windows API.


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion