Could someone please post an entire example (with includes, using , namespaces, etc) I have found several samples for C# and V Basic, but none for V C++. It could be just opening a commport and exitting. I can't quite get things set up and through build. Thanks, Rich

System.IO.SerialPort sample in V C++ Express
Sam_Tuteja
The following code should get the HResult form any exception:
public static int GetHResultFromException(Exception e)
{
Type exceptionType = typeof(Exception);
System.Reflection.FieldInfo hresultFieldInfo = exceptionType.GetField(
"_HResult",
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
return (int)hresultFieldInfo.GetValue(e);
}
Ryan Byington [MS]
floris_olivier
In your first example you are missing a call to SerialPort.Open(). It should be something like the following:
this.serialPort1 = new SerialPort("COM3");
this.serialPort1.Open();
this.serialPort1.Write("hi");
this.serialPort1.Close();
It is a little confusing that you have to explicitly call Open() for the SerialPort class but this is not needed for things like FileStream.
Ryan Byington [MS]
Hovisal
Thanks. Exactly what I was looking for.
Rich
Mark Raimondi
Hi Ryan
Well I do agree that you have to "open" before you can write - of course. But surely it should give me a "file not open" type exception
Im sure I read that when you create a new instance of the serialPort object that it automatically makes it open for ready to write/read from.
I tried as you suggested, and have done this before but I still get the same error.
{System.IO.IOException: IOException
at System.IO.Ports.SerialStream.WinIOError()
at System.IO.Ports.SerialStream.Write()
at System.IO.Ports.SerialPort.Write()
at SerialTest.Form1.DoConnectSerial()
at SerialTest.Form1.menuItem2_Click()
at System.Windows.Forms.MenuItem.OnClick()
at System.Windows.Forms.Menu.ProcessMnuProc()
at System.Windows.Forms.Form.WnProc()
at System.Windows.Forms.Control._InternalWnProc()
at Microsoft.AGL.Forms.EVL.EnterMainLoop()
at System.Windows.Forms.Application.Run()
at SerialTest.Program.Main()
}
base {System.SystemException}: {System.IO.IOException: IOException
at System.IO.Ports.SerialStream.WinIOError()
at System.IO.Ports.SerialStream.Write()
at System.IO.Ports.SerialPort.Write()
at SerialTest.Form1.DoConnectSerial()
at SerialTest.Form1.menuItem2_Click()
at System.Windows.Forms.MenuItem.OnClick()
at System.Windows.Forms.Menu.ProcessMnuProc()
at System.Windows.Forms.Form.WnProc()
at System.Windows.Forms.Control._InternalWnProc()
at Microsoft.AGL.Forms.EVL.EnterMainLoop()
at System.Windows.Forms.Application.Run()
at SerialTest.Program.Main()
}
I am also trying to make it work for WM2003 devices using the FileStream() or the low level calls to WriteFile() (since the serialport is only for WM2005 devices) etc... but of course I am getting errors on them as well.
VMNK
sure... lets see
well firstly interesting thing - created another new project and when I do a serialPort.Write() I get an invalid operation exception.
this is the code:
this.serialPort1 = new SerialPort("COM3");
this.serialPort1.Write("hi");
this.serialPort1.Close();
{System.InvalidOperationException: An error message cannot be displayed because an optional resource assembly containing it cannot be found
at System.IO.Ports.SerialPort.ThrowIfClosed()
at System.IO.Ports.SerialPort.Write()
at SerialTest.Form1.DoConnectSerial()
at SerialTest.Form1.menuItem2_Click()
at System.Windows.Forms.MenuItem.OnClick()
at System.Windows.Forms.Menu.ProcessMnuProc()
at System.Windows.Forms.Form.WnProc()
at System.Windows.Forms.Control._InternalWnProc()
at Microsoft.AGL.Forms.EVL.EnterMainLoop()
at System.Windows.Forms.Application.Run()
at SerialTest.Program.Main()
}
Now, going back to the other thread this is the code:
string someText = "hi";
FileStream d = new FileStream("COM3:", FileMode.Create);
d.Write(someText, 0, someText.Length);
d.Flush();
d.Close();
I get a blank IOException when Flush() occurs - I cannot seem to get the HResult - really have no idea how to.
Finally, going low level and doing the dllImport calls etc... to get a handle to the COM port, I get a valid handle but when I try to SetCommState() I get the "INVALID_HANDLE_ERROR" last win32/marshal error.
When I try to then WriteFile() I get error 1359 which is INTERNAL_ERROR.
so, using all 3 methods to write to the IR/COM port - I am unable to do so successfully. I am stuck
any ideas I really appreciate your help - it's annoying but fun at the same time.
if you can tell me how to get the HResult I could then give you the information you need.
fireladie
DaBarge
i'll give that a shot but after extensive research found out that the smartphone devices apperently do not have support for access a serial port (raw ir) as PPC's do - silly really.
so, its all down to the hardware - which is unfair :(
Jay Duggu
Could you please provide the full exception with stack trace.
Thanks,
Ryan Byington [MS]
gul_111111
With this exact code, I get the following error: "fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source "
If I add stdafx.h, I get a long list of errors:
z:\windat\desktop\research-stuff\repository\branch\serialreader\readtofile\readtofile.cpp(15) : error C2871: 'System' : a namespace with this name does not exist
z:\windat\desktop\research-stuff\repository\branch\serialreader\readtofile\readtofile.cpp(16) : error C2653: 'System' : is not a class or namespace name
z:\windat\desktop\research-stuff\repository\branch\serialreader\readtofile\readtofile.cpp(16) : error C2871: 'Ports' : a namespace with this name does not exist
z:\windat\desktop\research-stuff\repository\branch\serialreader\readtofile\readtofile.cpp(18) : error C2065: 'array' : undeclared identifier
z:\windat\desktop\research-stuff\repository\branch\serialreader\readtofile\readtofile.cpp(18) : error C2653: 'System' : is not a class or namespace name
z:\windat\desktop\research-stuff\repository\branch\serialreader\readtofile\readtofile.cpp(18) : error C2065: 'String' : undeclared identifier
z:\windat\desktop\research-stuff\repository\branch\serialreader\readtofile\readtofile.cpp(18) : error C2059: syntax error : '>'
z:\windat\desktop\research-stuff\repository\branch\serialreader\readtofile\readtofile.cpp(19) : error C2143: syntax error : missing ';' before '{'
z:\windat\desktop\research-stuff\repository\branch\serialreader\readtofile\readtofile.cpp(19) : error C2447: '{' : missing function header (old-style formal list )
I don't quite understand how to solve this issue.
hoobler
hi Ryan:
you give a good example,but can you give us how to receive data use datarecived function,how to receiver data use VC++2005 ,and i see much C# example,but little VC++2005 code.
can you give me as fast as!
thanks a lot!
Bill
leenrob
Ryan - perhaps you can help me with an issue
I am using C# (VS.NET 2005 pro)
The issue I have is that if I write() or flush() the data to the serial port which I have created on my smartphone, I get a BLANK IO Exception. I have no idea why.
Any ideas I did post a couple of topics about this but still to this date, no reply :(
Terrance OBrien
Ryan:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=233171&SiteID=1
thanks :)
Luis GdelC
Can you please give the HResult of the IO Exception This is protected property of IOException but you should be able to get it from the debugger(not sure if this is possible when debugging Compact Framework apps).
Ryan Byington [MS]
alex_f
The following is very simple example of how to user SerialPort in C++.
using namespace System;
using namespace System::IO::Ports;
int main(array<System::String ^> ^args)
{
SerialPort^ serialPort = gcnew SerialPort(
L"COM1",
9600,
Parity::None,
1,
StopBits::One);
serialPort->WriteLine("Hello World");
serialPort->Close();
return 0;
}
Please let me know if you have any additional questions.
Ryan Byington [MS]