Menu

Serial Communication

2006-11-10
2012-09-26
  • NewBiProgrammamer

    Hi all,

    i have this code

    include <stdio.h>

    include <stdlib.h>

    include <windows.h>

    include <winbase.h>

    int main()

    {
    OVERLAPPED osWrite = {0};
    DCB PortDCB;
    DWORD dwError,dwNumBytesWritten;
    HANDLE hComm;
    COMMTIMEOUTS CommTimeouts;

    char porta="COM1:\0";
    char pippo='5';
    char
    c={"s1\0"};

    hComm = CreateFile( porta,
    GENERIC_READ | GENERIC_WRITE,
    0,
    0,
    OPEN_EXISTING,
    FILE_FLAG_OVERLAPPED,
    0);
    if (hComm == INVALID_HANDLE_VALUE)
    {}// error opening port; abort

    PortDCB.DCBlength = sizeof (DCB);

    GetCommState(hComm,&PortDCB);

    // Change the DCB structure settings.
    PortDCB.BaudRate = 9600; // Current baud
    PortDCB.fBinary = FALSE; // 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_ENABLE;
    // 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_ENABLE;
    // RTS flow control
    PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on
    // error
    PortDCB.ByteSize = 8; // Number of bits/byte, 4-8
    PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
    PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2

    if(!SetCommState(hComm,&PortDCB))
    perror("");

    GetCommTimeouts (hComm, &CommTimeouts);
    // Change the COMMTIMEOUTS structure settings.
    CommTimeouts.ReadIntervalTimeout = 10;
    CommTimeouts.ReadTotalTimeoutMultiplier = 0;
    CommTimeouts.ReadTotalTimeoutConstant = 0;
    CommTimeouts.WriteTotalTimeoutMultiplier = 10;
    CommTimeouts.WriteTotalTimeoutConstant = 1000;

    if(!SetCommState(hComm,&PortDCB))
    perror("");
    if (!SetCommTimeouts (hComm, &CommTimeouts))
    perror("");

    TransmitCommChar(hComm,pippo);

    WriteFile (hComm, // Port handle
    c, // Pointer to the data to write
    1, // Number of bytes to write
    &dwNumBytesWritten, // Pointer to the number of bytes
    // written
    &osWrite // Must be NULL for Windows CE
    );

    return 0;
    }

    So the TransmitCommChar(hComm,pippo) work fine, in fact i can see the bits to the oscilloscope...but the WriteFile doesn't write anything!!!!

    Someone could help me please?

    Thanks a lot!

    Regards

     
    • Nobody/Anonymous

      Ooops. No you can't, this is all you can do in C:

      fprintf(stdaux, "Give it to me baby"); -- will go to the Serial port(COM1:)
      fprintf(stdprn, "Give it to me baby"); -- will go to the Printer(LPT1:)

      You should still check with GetLastError though. Did you check the dwNumBytesWritten?

       
      • Anonymous

        Anonymous - 2006-11-10

        ... You were right the first time, you simply open the necessary port:

        port = fopen( "com1", wb+ ) ;
        fprintf( port, "No, get it yourself\n");

        However, this is more restrictive that using the Win32 API. See also: https://sourceforge.net/forum/message.php?msg_id=3996248

        Clifford

         
    • Nobody/Anonymous

      Are you checkin the return value from WriteFile?

      If the function fails, the return value is zero. To get extended error information, call GetLastError.

      Have you considered using fopen/fwrite/fclose?

       
    • NewBiProgrammamer

      ...oh is it possible to manage the serial port with fopen, fwrite etc?

       

Log in to post a comment.

MongoDB Logo MongoDB