Thread: newbie help COM

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    9

    newbie help COM

    Hi I am new here so place bear with me

    I have never programmed in C/C++ but mainly in .NET C# etc...

    What I want to do is this:

    I want to open a com port
    write to it
    close it

    thats all! But here is the catch:

    I do not wish to connect to a device at all - pretty much just want to send a signal (for no reason for example) and thats it.

    I am using a mobile device for this and know that embedded C++ would be the way to go, which is what I have.

    the .NET Framework only has IRDA classes which means that you HAVE to connect to another compatible device.

    the .NET Framework v2.0 has a serial port class, which seems cool but having problems writing to the com port for some reason.


    I thought the best way would be to do it low level, and since im really into low level stuff, I thought i would get started on C++.

    I appreciate anyone giving me help on how to do the 3 functions I require.

    Please, help me

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Since you are using .NET I assume the os is WinCE ? or some other embedded version of win32 api? If you have CreateFile() you can open the com port whether there is anything attached to it or not. see MSDN for description of CreatFile() it describes how to open COM ports.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    9
    Thank-you, I will give that a shot. If you have any more hints/tips/tricks/help please do post as I am willing to learn and want to learn!

    I am using Windows Mobile 2003SE and 2005 (2 different mobile devices)

    I tried making a VC++.NET project - its tough stuff! I have a headache now lol


    reading the createfile() function on msdn - how do I do all this in .NET? I really have no clue

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    9
    ok well i did alot of stuff and this seems to send stuff but no idea if its sending it correctly/throwing errors.

    first:

    in .NET Compact Framework I found a way of writing to the COM port directly, giving it "COM3:" with the colon, seems to send signals BUT when you flush the filestream OR close the filestream, it gives me a blank IOException.

    So what I did, following your advice was to use the CreateFile() and then got the API's for WriteFile() and CloseHandle().

    Now, I seem to be getting a handle, writing to it and closing it fine but of course its unmanaged (correct?). I am developing this in C# .NET Compact Framework 2.0.

    this is the code I have, no idea if you will understand it so please bear with me:

    Code:
    //import libraries:
    [DllImport("Coredll.dll", SetLastError=true)]
            static extern IntPtr CreateFile(
                string filename,
                [MarshalAs(UnmanagedType.U4)]FileAccess fileaccess,
                [MarshalAs(UnmanagedType.U4)]FileShare fileshare,
                int securityattributes,
                [MarshalAs(UnmanagedType.U4)]FileMode creationdisposition,
                int flags,
                IntPtr template);
    
    
            [DllImport("Coredll.dll", SetLastError = true)]
            static extern IntPtr WriteFile(
                IntPtr fFile,
                Byte[] lpBuffer,
                UInt32 nNumberOfBytesToWrite,
                out UInt32 lpNumberOfBytesWritten,
                IntPtr lpOverlapped);
    
    
            [DllImport("Coredll.dll", SetLastError = true)]
            static extern bool CloseHandle(
                IntPtr hObject
            );
    Above: code to import libraries and the functions

    Code:
    //createfile and writefile()
    
     IntPtr handle = CreateFile("COM3:", FileAccess.Write, FileShare.Write, 0, FileMode.Create, 0, IntPtr.Zero);
    
    if (handle != IntPtr.Zero)
    {
    string test = "hi";
    byte[] some = System.Text.ASCIIEncoding.ASCII.GetBytes(test);
    uint sent = 0;
    IntPtr ptrUWO = IntPtr.Zero;
    WriteFile(handle, some, (uint)some.Length, out sent, ptrUWO);
    CloseHandle(handle);
    }
    Above: code to createfile() and writefile() and closefile()


    Is this correct?

    I have also just found out that when it starts to send signals, I get the error :

    1359
    which is an internal error - any ideas?
    Last edited by firehawk_1; 02-03-2006 at 12:59 PM.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Search MSDN for CreateFile(), then scroll down the screen until you see the section titled Communications Resources. Follow the link there to Communications articles.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    9
    I dont know if you read my new posts before yours, if you havent please do

    if you have... im still stuck!

    I keep getting error 1359 which is internal error - but no idea whats causing this when im writing to file (WriteFile())

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    9
    after some more research, found out that I had to set the "SetCommState" - the baud rate and so on - once done and when I call SetCommState() - I get error 6 - invalid pointer but the CreateFile() seems to return me a valid pointer!

    Same thing happens when I call GetCommState()

    any ideas?

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    I have some old code lying about (at work) which accesses the COM port in Visual C++ 6 using CreateFile(). There was also an article in the MSDN magazine a couple of years back (I think - the mag's @ work too) that included a .NET assembly for accessing the COM ports. It's online here: http://msdn.microsoft.com/msdnmag/is...netserialcomm/ . I also saw a note about VS .NET 2005 having even simpler serial port communication classes, but don't have any real data to back that up.

    If you want the VC++6 serial comm code, PM me and I'll get it to you on Monday.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    9
    Thanks for that - yes .NET 2.0 DOES have a serialPort class but I have errors given from it - when I flush or write to the port - I get a blank IOException. I will PM you with regards to the serial comm code but still certain I will recieve the same errors as I am now


    It appears I cannot get into your personal profile! here is my email address anyway:

    admin [at] spapps [dot] co [dot] uk

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    9
    anyone knows why I get errors when SetCommState() or WriteFile() is used?

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    I'd have to see more - it's been a while since I wrote the code to handle the serial port using those, and I have yet to successfully wrap any of my C++ in .NET. I'll dig up my code tomorrow and email it to you. It may not be perfect, but I know for a fact it works.

  12. #12
    Registered User
    Join Date
    Feb 2006
    Posts
    9
    thanks
    im sure it will work - but you dont need to know any .NET stuff for this, as I am simply making low level calls from dll entry points

    hope someone can have a solution for this problem

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    9
    *bump*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM