Click here to Skip to main content
15,911,139 members
Home / Discussions / C#
   

C#

 
GeneralRe: Porting this to event-based? Pin
Christian Graus24-Sep-06 15:29
protectorChristian Graus24-Sep-06 15:29 
GeneralRe: Porting this to event-based? Pin
Lord Kixdemp25-Sep-06 8:14
Lord Kixdemp25-Sep-06 8:14 
Questionadd column to dataset Pin
Mohammed Elkholy24-Sep-06 10:39
Mohammed Elkholy24-Sep-06 10:39 
AnswerRe: add column to dataset Pin
Nader Elshehabi24-Sep-06 12:08
Nader Elshehabi24-Sep-06 12:08 
Question[Message Deleted] Pin
code_wiz24-Sep-06 8:06
code_wiz24-Sep-06 8:06 
AnswerRe: extracting a message value from LParam in Message in WndProc(...) Pin
Christian Graus24-Sep-06 11:58
protectorChristian Graus24-Sep-06 11:58 
QuestionRe: extracting a message value from LParam in Message in WndProc(...) Pin
Nader Elshehabi24-Sep-06 12:04
Nader Elshehabi24-Sep-06 12:04 
QuestionProblem getting serial port to open Pin
NYTSX24-Sep-06 7:50
NYTSX24-Sep-06 7:50 
Hi,

I am new to C# and trying to develop an app that communicates through the serial port. I found some sample code on the net that seems to work, but when i use the same code in my app it throws an exception. Currently I am developing using .NET version 2.0 on a Windows XP SP2 machine.

Am I doing something wrong? Is there a setting that I have to set or include?

Here is the code I have, which is run when I click on a "Start" button:
using System.IO.Ports;

private static void SendSampleData()
{
  // Instantiate the communications port with some basic settings
  SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
  
  // Open the port for communications
  port.Open();
  
  // Write a string
  port.Write("Hello World");
  
  // Write a set of bytes
  port.Write(new byte[] {0x0A, 0xE2, 0xFF}, 0, 3);
  
  // Close the port
  port.Close();
}


I don't know how much more simple I can get with this.

Here is the exception I get when I try and execute the port.Open():
System.Security.SecurityException was unhandled
  Message="Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."
  Source="mscorlib"
  StackTrace:
       at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
       at System.Security.CodeAccessPermission.Demand()
       at System.IO.Ports.SerialPort.Open()
       at CITDataRecorder.CITDataRecorder.Start_Stop_Button_Click(Object sender, EventArgs e)
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at CITDataRecorder.Program.Main()
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()


thanks in advance!
AnswerRe: Problem getting serial port to open Pin
Ed.Poore24-Sep-06 8:23
Ed.Poore24-Sep-06 8:23 
GeneralRe: Problem getting serial port to open Pin
NYTSX25-Sep-06 0:59
NYTSX25-Sep-06 0:59 
GeneralRe: Problem getting serial port to open Pin
NYTSX25-Sep-06 1:12
NYTSX25-Sep-06 1:12 
GeneralRe: Problem getting serial port to open Pin
Ed.Poore25-Sep-06 1:19
Ed.Poore25-Sep-06 1:19 
QuestionSaving objects in Files Pin
ezazazel24-Sep-06 6:32
ezazazel24-Sep-06 6:32 
AnswerRe: Saving objects in Files Pin
Ed.Poore24-Sep-06 8:26
Ed.Poore24-Sep-06 8:26 
GeneralRe: Saving objects in Files Pin
Niiiissssshhhhhuuuuu24-Sep-06 17:10
Niiiissssshhhhhuuuuu24-Sep-06 17:10 
GeneralRe: Saving objects in Files Pin
Ed.Poore24-Sep-06 22:54
Ed.Poore24-Sep-06 22:54 
GeneralRe: Saving objects in Files Pin
ezazazel25-Sep-06 8:25
ezazazel25-Sep-06 8:25 
GeneralRe: Saving objects in Files Pin
ezazazel25-Sep-06 8:26
ezazazel25-Sep-06 8:26 
QuestionKeyboard focus trouble with unmanaged code Pin
zapsolution24-Sep-06 5:13
zapsolution24-Sep-06 5:13 
AnswerRe: Keyboard focus trouble with unmanaged code Pin
Nader Elshehabi24-Sep-06 11:11
Nader Elshehabi24-Sep-06 11:11 
GeneralRe: Keyboard focus trouble with unmanaged code Pin
zapsolution24-Sep-06 11:43
zapsolution24-Sep-06 11:43 
GeneralRe: Keyboard focus trouble with unmanaged code Pin
Nader Elshehabi24-Sep-06 12:02
Nader Elshehabi24-Sep-06 12:02 
GeneralRe: Keyboard focus trouble with unmanaged code Pin
zapsolution24-Sep-06 20:29
zapsolution24-Sep-06 20:29 
QuestionCustom DataGridColumStyle with color dialog Pin
TeamWild24-Sep-06 3:27
TeamWild24-Sep-06 3:27 
AnswerRe: Custom DataGridColumStyle with color dialog Pin
Alexandr Sergeevich Ilyin24-Sep-06 6:06
Alexandr Sergeevich Ilyin24-Sep-06 6:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.