[Mono-dev] linux Serial Port

Mauricio Henriquez buhochileno at gmail.com
Fri Mar 18 16:58:34 EDT 2011


Hi:

try to give user permisions to /dev/ttySx or /dev/ttyUSBx depending on 
the device that you using...
you can do it more permanently with a udev rule,  but try first just 
given permision to the user that is going to run the program...

cheers!

Mauricio

On 03/13/2011 06:31 PM, Mrhyde88 wrote:
> 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.
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>    


-- 
Mauricio R. Henriquez Schott
Escuela de Ingeniería en Computación
Universidad Austral de Chile - Sede Puerto Montt
Los Pinos S/N, Balneario de Pelluco, Puerto Montt - Chile
Tel: 65-487440
Fax: 65-277156
mail: mauriciohenriquez at uach.cl

-------------- next part --------------
A non-text attachment was scrubbed...
Name: buhochileno.vcf
Type: text/x-vcard
Size: 384 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20110318/4ef2f83a/attachment-0001.vcf 


More information about the Mono-devel-list mailing list