[Mono-dev] linux Serial Port

Mrhyde88 kiros_s53 at hotmail.com
Sun Mar 13 18:31:27 EDT 2011


Hi guys 

im just starting with Mono on linux, and im having problems getting the
serial port to work, heres the code im using:

##########################################################
using System;
using Gtk;
using System.IO.Ports;



public partial class MainWindow : Gtk.Window
{
	SerialPort sport= new System.IO.Ports.SerialPort("/dev/ttyS0", 9600,
Parity.None,8, StopBits.None);
	public MainWindow () : base(Gtk.WindowType.Toplevel)
	{
		Build ();
		
		
	}

	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		Application.Quit ();
		a.RetVal = true;
	}
	
	//initialize serial port
	public void serial(){
	//SerialPort sport= new System.IO.Ports.SerialPort("/dev/ttyUSB0", 9600);
	
	if (sport.IsOpen==false){
		sport.BaudRate=9600;
		sport.DtrEnable=true;
		sport.RtsEnable=true;
	        sport.Open();
		sport.ReadTimeout=400;
		}
	else
	{
		sport.Close();	
	}
		
	
	}
	
	//read serial port Data
	public string ReadData()
	{
		byte tmpByte;
		string rxString = "";
		
		tmpByte = (byte) sport.ReadByte();

		while (tmpByte != 255)
		{
			rxString += ((char) tmpByte);
		
			tmpByte = (byte) sport.ReadByte();			
		}
	
		return rxString;
	}

	protected virtual void OnBtnimagePressed (object sender, System.EventArgs
e)
	{
		serial();
		txtserial.Text= ReadData();
		
	}
	
	
	
	
}

####################

The program crashes on the sport.Open() instruction, 

System.Reflection.TargetInvocationException: Exception has been thrown by
the target of an invocation. ---> System.IO.IOException: Permission denied


Regards


--
View this message in context: http://mono.1490590.n4.nabble.com/linux-Serial-Port-tp3352705p3352705.html
Sent from the Mono - Dev mailing list archive at Nabble.com.


More information about the Mono-devel-list mailing list