CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2001
    Posts
    17

    writing into parallel port

    hi,

    i want to write into parallel port and i have done this. it doesn't work but i don't know why.
    Any idea?

    HANDLE hPuerto;
    DCB m_dcb;
    BOOL m_PuertoPreparado;
    BYTE dato;
    DWORD iBytesEscritos;
    bool error=false;

    hPuerto=CreateFile("LPT1", // pointer to name of the file
    GENERIC_READ|GENERIC_WRITE, // access (read-write) mode
    0, // share mode
    0, // pointer to security attributes
    OPEN_EXISTING, // how to create
    FILE_ATTRIBUTE_NORMAL, // file attributes
    0); // handle to file with attributes to copy

    //// the handle seems to be ok.

    if (hPuerto==INVALID_HANDLE_VALUE)
    {
    error=true;
    }


    //// GetCommState gives an error : error 87 Invalid parameters

    m_PuertoPreparado = GetCommState(hPuerto,&m_dcb);

    ///////////////////////////////////////////////////////////////////////




    if(m_PuertoPreparado ==0) error=true;

    // here i have modified the settings of the parallel port
    m_dcb.BaudRate = 9600;
    m_dcb.ByteSize = 8;
    m_dcb.Parity = NOPARITY;
    m_dcb.StopBits = ONESTOPBIT;

    m_PuertoPreparado = SetCommState(hPuerto, &m_dcb);

    if(m_PuertoPreparado ==0) error=true;


    //// now we write into the port
    dato=1;

    //// WriteFile gives this error : error 6 The handle is invalid

    if ( WriteFile(hPuerto,&dato,1,&iBytesEscritos,NULL)==0) error=true;

    //////////////////////////////////////////

    CloseHandle(hComm);


    Thanks. (sorry for my english)

  2. #2
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    Re: writing into parallel port

    According to MSDN (Configuring a Communication Resource)

    Code:
      hCom = CreateFile( pcCommPort, // "LPT1" in your case
                         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
                         );
    You are passing FILE_ATTRIBUTE_NORMAL as the penultimate parameter. FILE_ATTRIBUTE_NORMAL has a value of 0x80 and you need to pass 0 here.

    Presumably, there's a different value for overlapped I/O but I don't know whether it's 0x80.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  3. #3
    Join Date
    Jun 2005
    Posts
    1,255

    Question Re: writing into parallel port

    You have modified the settings as if you were using a serial port instead of a parallel port.

    > // here i have modified the settings of the parallel port
    > m_dcb.BaudRate = 9600;
    > m_dcb.ByteSize = 8;
    > m_dcb.Parity = NOPARITY;
    > m_dcb.StopBits = ONESTOPBIT;

    With a serial port, there are groups of 8 bits with one, one and a half or two stop bits. But with a parallel port, all the bits are sent at once.

    The baud rate is the speed between each one of the 8 bits in the serial port, but that means nothing with a parallel port.

    Are you sure you really want to use a parallel port and not a serial port?

  4. #4
    Join Date
    Nov 2001
    Posts
    17

    Re: writing into parallel port

    thanks a lot for reply so quickly.

    i have changed sixth parameter to 0, and the error with GetCommState persists(error number 87). In relation with the settings of parallel port , i found these settings in this page:

    http://support.microsoft.com/default...Product=vb6#10

    Although here, they are using .NET, instead of visual studio 6 that is the one i am using.

    if i remove the instructions from GetCommState to SetCommState and, simply write the data after opening the port, the program hangs, it doesn't respond.

    i don't know what is the problem, because the port is well opened.
    Before writing, is necesary to configure the parallel port using the DCB structure?
    if not, does anybody know why the program gets blocked?

    thanks again.

  5. #5
    Join Date
    Jun 2005
    Posts
    1,255

    Re: writing into parallel port

    Excuse-me for the question about the settings, I didn't know this was a recommandation from Microsoft (although, it's still strange to me than a stop bit and a baud rare are required with a parallel port, but maybe this doesn't matter).

    Anyway, I think I have found an error in your code:

    > BYTE dato;
    ...
    > dato=1;
    > if ( WriteFile(hPuerto,&dato,1,&iBytesEscritos,NULL)==0) error=true;

    I would rather have written:

    char dato[10];
    ...
    strcpy(dato, "1"); // or dato[0] = 1; if that's really the binary 1 that you want.
    if ( WriteFile(hPuerto,&dato,1,&iBytesEscritos,NULL)==0) error=true;

  6. #6
    Join Date
    Nov 2001
    Posts
    17

    Re: writing into parallel port

    hi,

    i have already probed it and the same result. The program hangs if i remove the instructions from getcommstate() to setcommstate() and if not error 87.

    I suppouse i will have to use some library and the inp and outp functions, but it's strange that the program fails with createFile and WriteFile.

    http://www.zealsoftstudio.com/ntport/



    thank you very much anyway.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured