[Mono-list] SerialPortTest

sojan sojankallara at yahoo.com
Thu Mar 5 01:10:43 EST 2009


the code is as follows please have a look

using System;
using System.IO.Ports;
 
public class SerialPortTest
{
	public static void Main(string[] args)
	{
		SerialPortTest myTest = new SerialPortTest();
		myTest.Test();
	}
 
	private SerialPort mySerial;
 
	// Constructor
	public SerialPortTest()
	{
	}
 
	public void Test()
	{
		if (mySerial != null)
			if (mySerial.IsOpen)
				mySerial.Close();
 
		mySerial = new
SerialPort("/dev/ttyS0",9600,System.IO.Ports.Parity.None,8,System.IO.Ports.StopBits.One);
		mySerial.Open();
		mySerial.ReadTimeout = 400;
		SendData("ATI3\r");
 
                // Should output some information about your modem firmware
		Console.WriteLine(ReadData());  
	}
 
	public string ReadData()
	{
		byte tmpByte;
		string rxString = "";
			
		tmpByte = (byte) mySerial.ReadByte();
 
		while (tmpByte != 255) {
			rxString += ((char) tmpByte);
			tmpByte = (byte) mySerial.ReadByte();			
		}
	
		return rxString;
	}
 
	public bool SendData(string Data)
	{
		mySerial.Write(Data);
		return true;		
	}
}


Orfeo wrote:
> 
> 
> sojan wrote:
>> 
>> I  got a sample code for serial port communication.
>> i compiled the code using gmcs Serialport.cs
>> 
>> when i execute the exe as mono Serialport.exe
>> 
>> getting the following exception
>> 
>> Unhandled Exception: System.IO.IOException: I/O Error
>>   at System.IO.Ports.SerialPortStream..ctor (System.String portName,
>> Int32 baudRate, Int32 dataBits, Parity parity, StopBits stopBits, Boolean
>> dtrEnable, Boolean rtsEnable, Handshake handshake, Int32 readTimeout,
>> Int32 writeTimeout, Int32 readBufferSize, Int32 writeBufferSize)
>> [0x00000] 
>>   at (wrapper remoting-invoke-with-check)
>> System.IO.Ports.SerialPortStream:.ctor
>> (string,int,int,System.IO.Ports.Parity,System.IO.Ports.StopBits,bool,bool,System.IO.Ports.Handshake,int,int,int,int)
>>   at System.IO.Ports.SerialPort.Open () [0x00000] 
>>   at (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:Open
>> ()
>>   at SerialPortTest.Test () [0x00000] 
>>   at SerialPortTest.Main (System.String[] args) [0x00000] 
>> 
>> help me to solve this 
>> thanks in advance
>> 
> 
> How did you open the port?
> 
> In linux you must use "/dev/ttyS0" as port name:
> -----
> 
> System.IO.Ports.SerialPort serialport = new
> System.IO.Ports.SerialPort("/dev/ttyS0",
>     br,
>     System.IO.Ports.Parity.None,
>     8,
>     System.IO.Ports.StopBits.One);
> ------
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/SerialPortTest-tp22324797p22345824.html
Sent from the Mono - General mailing list archive at Nabble.com.



More information about the Mono-list mailing list