Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
A

AnotherProgrammer

@AnotherProgrammer
About
Posts
11
Topics
5
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • CSerialPort Class Problem and Error 87
    A AnotherProgrammer

    Sorry, messed up the HTML tags. Here's the link to that other post containing the CSerialPort class problems: Link to CodeGuru post about CSerialPort class problem

    C / C++ / MFC help php com tutorial question

  • CSerialPort Class Problem and Error 87
    A AnotherProgrammer

    Hello. First, I'd like you to take a look at this post from another forum: http://www.codeguru.com/forum/showthread.php?s=4f35cbebe00b8299d7c4f45da97051c1&threadid=193904 (it explains the problem with CSerialPort -- this should also be brought to the attention of the authors of the class) Anyway, I came across the exact same problem, except I was not using the class. I simply had WaitCommEvent and GetLastError() give me the same error 87 using this piece of code:

    if (WaitCommEvent(hCom, &event, &obj))
    {
    cout << "Character Detected! ";
    fsuccess = ReadFile (hCom, &data, 1, &transferred, &obj);
    if (fsuccess)
    cout << "Character Read! Here it is: " << data;
    getch();
    }
    else
    {
    if (GetLastError() == ERROR_IO_PENDING)
    cout << "Still waiting";
    else
    {
    cout << "Fatal error: " << GetLastError();
    getch();
    exit(1);
    }
    }

    As I said, the output for this program is Fatal error: 87. I should explain, this is after the COM1 port has been opened successfully for reading. Does anyone have any suggestions as to how to solve this? I will be very thankful if someone could help. Thanks..

    C / C++ / MFC help php com tutorial question

  • Correct Usage of WaitCommEvent?
    A AnotherProgrammer

    The ReadFile function also has me a bit concerned. Assuming WaitCommEvent works, and the "event" is that a character has been received (EV_RXCHAR), would this piece of code output the character that was received (data)?

    OVERLAPPED obj;
    BOOL fsuccess;
    HANDLE hCom;
    BYTE data;
    DWORD event = EV_RXCHAR;
    DWORD transferred;
    
    // ...
    // Open the port, give the correct settings, 
    // ...
    
    if (WaitCommEvent(hCom, &event, &obj))
    {
          cout << "IT DETECTED SOMETHING! ";
          fsuccess = ReadFile (hCom, &data, 1, &transferred, &obj);
          if (fsuccess)
          {
               cout << "IT READ A CHARACTER: " << data;
          }
          getch();
    }
    

    Oh, in both of the above programs, the serial port is constantly being fed data continuously. Thanks for any help.

    C / C++ / MFC help css com security json

  • Correct Usage of WaitCommEvent?
    A AnotherProgrammer

    Hello... I'm currently writing a program that has to read in data from the serial port and then process it. But I thought before I tackle that part, I should write a small sample one to test to see that I can actually receive data. I got this sample program off the MSDN website and made a couple of minor changes (original can be found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/monitoring\_communications\_events.asp ) to reflect the fact that it's supposed to look for any characters received.

    // used square brackets to make libraries show up here - actual code contains
    // greater than and less than signs
    #include [windows.h]
    #include [assert.h]
    #include [conio.h]
    #include [iostream]
    void main()
    {
        HANDLE hCom;
        OVERLAPPED o;
        BOOL fSuccess;
        DWORD dwEvtMask;
    
        hCom = CreateFile( "COM1",
            GENERIC_READ,
            0,    // exclusive access
            NULL, // default security attributes
            OPEN_EXISTING,
            FILE_FLAG_OVERLAPPED,
            NULL
            );
    
        if (hCom == INVALID_HANDLE_VALUE)
        {
            // Handle the error.
            return;
        }
    
        // Set the event mask.
    
        fSuccess = SetCommMask(hCom, EV_CTS | EV_DSR);
    
        if (!fSuccess)
        {
            // Handle the error.
            return;
        }
    
        // Create an event object for use by WaitCommEvent.
    
        o.hEvent = CreateEvent(
            NULL,   // default security attributes
            FALSE,  // auto reset event
            FALSE,  // not signaled
            NULL    // no name
    		);
    
    
        // Intialize the rest of the OVERLAPPED structure to zero.
        o.Internal = 0;
        o.InternalHigh = 0;
        o.Offset = 0;
        o.OffsetHigh = 0;
    
        assert(o.hEvent);
    
        if (WaitCommEvent(hCom, &dwEvtMask, &o))
        {
            if (dwEvtMask & EV_RXCHAR)
            {
    				cout << "received!";
            }
    
            if (dwEvtMask & EV_CTS)
            {
                // To do.
            }
        }
        getch();
    }
    

    This program, when compiled, does not output "received!", even though I know there is data being sent to the serial port (COM1). Any suggestions as to why? Is there a better way to monitor for the reception of characters through the serial port? Any help is appreciated. Thanks in advance.

    C / C++ / MFC help css com security json

  • Serial Port - overlapped I/O - WaitCommEvent/SetCommMask/ReadFile/etc questions
    A AnotherProgrammer

    Sorry for reposting this, but I thought I may get some help now that it's the weekend... I have now undertaken rewriting the serial portion of my code to use CreateFile and the like. It's all a bit complicated though, and being new to this, I have a few questions. It starts with overlapping, and also involves WaitCommEvent and GetCommMask. I did a search on the topics and read a few articles that were found, but while they cleared a few things up, they still left me with a few questions unanswered. First, I understand that SetCommMask allows you to screen for only a few of the events. But after calling SetCommMask to look for only what you want, how does one screen for these events and process them appropriately? I know you can use WaitCommEvent, but in that case, why would you even need GetCommMask? I also need to use overlapping for this program, but I'm not sure how to implement it or even what it actually is (do I need to use the overlapped structure at all?). I was able to find this piece of example code on MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/monitoring\_communications\_events.asp I do not fully understand how it operates, though. Also, this article was very helpful, to a point: http://www.codeproject.com/system/serial\_com.asp Where I got lost was the huge block of code where he uses the OVERLAPPED structure and the Threadfn function, after which he writes "If you understood the above code , you will understand the whole of this article and the source code provided." Gulp. Basically, I have trouble understanding what he does there and where that overlapped structure comes into play. As always, the help is very much appreciated.

    C / C++ / MFC tutorial com help question

  • WaitCommEvent and GetCommMask (and overlapping)
    A AnotherProgrammer

    I'm now down to the point where I have the code written for everything up to where I understand -- meaning, the WaitCommEvent part is the only part that needs completing. Any help with this?

    C / C++ / MFC help tutorial com question

  • WaitCommEvent and GetCommMask (and overlapping)
    A AnotherProgrammer

    Thanks Paul -- that was it! I don't know why I didn't catch it myself. Still looking for some clarification and answers about my original post... your help is greatly appreciated.

    C / C++ / MFC help tutorial com question

  • WaitCommEvent and GetCommMask (and overlapping)
    A AnotherProgrammer

    From this link: Configuring a communications resource: #include int main(int argc, char *argv[]) { DCB dcb; HANDLE hCom; BOOL fSuccess; char *pcCommPort = "COM2"; hCom = CreateFile( pcCommPort, GENERIC_READ | GENERIC_WRITE, 0, // must be opened with exclusive-access NULL, // no security attributes OPEN_EXISTING, // must use OPEN_EXISTING 0, // not overlapped I/O NULL // hTemplate must be NULL for comm devices ); pcCommPort is declared as a pointer to a character, which is assigned "COM2". How does this make sense? A char cannot be more than one character in length, right? Upon testing, trying to do *pcCommPort = "COM3" later in the program does indeed yield "error C2440: '=' : cannot convert from 'char [5]' to 'char'" which actually makes sense. So why is that same assignment statement allowed upon declaration? Also, if at some point, I need to change the COM port to say, COM3, how would I accomplish that? *pcCommPort = "COM3" did not work, as I mentioned. Thanks in advance.

    C / C++ / MFC help tutorial com question

  • WaitCommEvent and GetCommMask (and overlapping)
    A AnotherProgrammer

    Thanks for the help on my earlier problem.. I have now undertaken rewriting the serial portion of my code to use CreateFile and the like. It's all a bit complicated though, and being new to this, I have a few questions. It starts with overlapping, and also involves WaitCommEvent and GetCommMask. I did a search on the topics and read a few articles that were found, but while they cleared a few things up, they still left me with a few questions unanswered. First, I understand that SetCommMask allows you to screen for only a few of the events. But after calling SetCommMask to look for only what you want, how does one screen for these events and process them appropriately? I know you can use WaitCommEvent, but in that case, why would you even need GetCommMask? I also need to use overlapping for this program, but I'm not sure how to implement it or even what it actually is (do I need to use the overlapped structure at all?). I was able to find this piece of example code on MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/monitoring\_communications\_events.asp I do not fully understand how it operates, though. Also, this article was very helpful, to a point: http://www.codeproject.com/system/serial\_com.asp Where I got lost was the huge block of code where he uses the OVERLAPPED structure and the Threadfn function, after which he writes "If you understood the above code , you will understand the whole of this article and the source code provided." Gulp. As always, the help is very much appreciated.

    C / C++ / MFC help tutorial com question

  • Serial Port Communication in WinXP (or NT/2000)
    A AnotherProgrammer

    Thanks for the reply. So I have to basically rewrite a portion of my code to comply with using CreateFile and whatnot. This is where it gets confusing for me. From the link you provided: ---- /* A sample program to illustrate setting up a serial port. */ #include int main(int argc, char *argv[]) { DCB dcb; HANDLE hCom; BOOL fSuccess; char *pcCommPort = "COM2"; hCom = CreateFile( pcCommPort, GENERIC_READ | GENERIC_WRITE, 0, // must be opened with exclusive-access NULL, // no security attributes OPEN_EXISTING, // must use OPEN_EXISTING 0, // not overlapped I/O NULL // hTemplate must be NULL for comm devices ); if (hCom == INVALID_HANDLE_VALUE) { // Handle the error. printf ("CreateFile failed with error %d.\n", GetLastError()); return (1); } // Build on the current configuration, and skip setting the size // of the input and output buffers with SetupComm. fSuccess = GetCommState(hCom, &dcb); if (!fSuccess) { // Handle the error. printf ("GetCommState failed with error %d.\n", GetLastError()); return (2); } // Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit. dcb.BaudRate = CBR_57600; // set the baud rate dcb.ByteSize = 8; // data size, xmit, and rcv dcb.Parity = NOPARITY; // no parity bit dcb.StopBits = ONESTOPBIT; // one stop bit fSuccess = SetCommState(hCom, &dcb); if (!fSuccess) { // Handle the error. printf ("SetCommState failed with error %d.\n", GetLastError()); return (3); } printf ("Serial port %s successfully reconfigured.\n", pcCommPort); return (0); } ----- This sample program confuses me a great deal. Why does the main program need parameters (I was under the impression that it was called by the OS and executing began there -- thus, there are no inputs... and to further complicate things, neither of those parameters are used in the program!). Also, what are these data types that are declared - DCB, HANDLE, and BOOL (capital bool? C++ is case-sensitive, no?)? I guess I'm just overall sketchy on how to use CreateFile and those data types (HANDLE seems to be the port's data type, I guess), since I thought using a class that already did these things was the way to go and so didn't take the tim

    C / C++ / MFC help c++ delphi com tutorial

  • Serial Port Communication in WinXP (or NT/2000)
    A AnotherProgrammer

    Hello, I've been trying to write a program in C++ (using Borland C++ 5.02 compiler, alternating with MSVC++ 6.0) that communicates with the serial port to read in data. I used the book "Serial Communications in C and C++" by Mark Goodwin as a reference, as it came with a floppy that contained source files including a class that reduced serial communications to a matter of declaring a variable and calling some member functions on it (eg. Port1.in_ready(), Port1.get(), etc). Anyway, the code compiles, but it fails in the linking stage as it says that there are external errors dealing with the class's commands -- even the example code would not work. So I did a bit of research and found out that XP does not allow users to freely access the serial COM ports (1-4), among others. One site suggested that downloading a certain driver would fix things, but I have not been able to find anything of that nature. Does anyone have any experience dealing with a problem similar to this? Is there something out there that I haven't found yet? Any help is greatly appreciated...

    C / C++ / MFC help c++ delphi com tutorial
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups