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
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 :)
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.
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
ok well i did alot of stuff and this seems to send stuff but no idea if its sending it correctly/throwing errors.
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:
Above: code to import libraries and the functions
static extern IntPtr CreateFile(
[DllImport("Coredll.dll", SetLastError = true)]
static extern IntPtr WriteFile(
out UInt32 lpNumberOfBytesWritten,
[DllImport("Coredll.dll", SetLastError = true)]
static extern bool CloseHandle(
Above: code to createfile() and writefile() and closefile()
//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);
Is this correct?
I have also just found out that when it starts to send signals, I get the error :
which is an internal error - any ideas?
Search MSDN for CreateFile(), then scroll down the screen until you see the section titled Communications Resources. Follow the link there to Communications articles.
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())
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()
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.
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
anyone knows why I get errors when SetCommState() or WriteFile() is used?
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.
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