[Mono-list] SerialPort w/ Even Parity not working, None parity OK

Jared Cooper jaredcooper at fairbanks.co.uk
Fri Dec 12 07:52:16 EST 2008


Using the SerialPort class with parity None (e.g. 9600 8 N 1) works fine,
however, when run with Even parity (e.g. 9600 7 E 1) some data comes through
fine, some comes through with an additional 7F byte following, and in some
cases there are simple additional bytes appearing in the stream.

The example below shows this.

Note:  This is an attempt to port an existing Windows/.Net 2 app to
Linux/Mono 2.  Compile is done in Windows from SharpDevelop (so using
MSBUILD) because we will need to support existing and other future Windows
installs in addition to future Linux installs.

Running the example on Windows works fine using both options (see code for
options) but running in Linux produces the above problem.

Using OpenSUSE 11 with downloaded RPM install of Mono
(mono-core-2.0.1-18.1.i586.rpm, etc).

------------ Sample Code Follows ------------

namespace SerialPortTest
{
	public delegate void PolledDataReceivedDelegate(byte[] data);
	
	public class PollingSerialPort : SerialPort 
	{
		Thread		readThread;
		bool		readActive = false;
		
		public PollingSerialPort() : base(){
			ReadTimeout = 50;
		}
		
		public event PolledDataReceivedDelegate PolledDataReceived;
		
		new public void Open(){
			if( IsOpen ){
				return;
			}
			base.Open();
			if( IsOpen ){
				readActive = true;
				readThread = new Thread(ReadWorker);
				readThread.Start();
			}
		}
		
		new public void Close(){
			if( !IsOpen ){
				return;
			}
			readActive = false;
			base.Close();
		}
		
		void ReadWorker(){
			
			int len;
			
			while(readActive){
				
				try{
					
					len = BytesToRead;
					
					if( len > 0 ){
						
						byte[] data = new byte[len];
						
						Read(data, 0, len);
						
						if( PolledDataReceived != null ){
							PolledDataReceived(data);
						}
						
					} else {
						Thread.Sleep(ReadTimeout);
					}
	
				} catch( TimeoutException ) {
				} catch( Exception e) {
					if( readActive ){
						throw e;
					}
				}
				
			}
				
		}
		
	}
	
	class Program
	{
		public static void Main(string[] args)
		{
			
			Console.WriteLine("1: 9600,8,None,1");
			Console.WriteLine("2: 9600,7,Even,1");
			
			string opt = Console.ReadLine();
			
			PollingSerialPort port = new PollingSerialPort();
			
			//port.PortName = "COM4";
			port.PortName = "/dev/ttyS0";
			
			port.BaudRate = 9600;
			port.Handshake = Handshake.None;
			port.StopBits = StopBits.One;
			
			if( opt == "2" ){
				port.DataBits = 7;
				port.Parity = Parity.Even;
			} else {
				port.DataBits = 8;
				port.Parity = Parity.None;
			}
			
			port.PolledDataReceived += DataIn;
			
			port.Open();
			
			Console.ReadKey();
			
			port.PolledDataReceived -= DataIn;
			
			port.Close();
			
		}
		
		static void DataIn(byte[] data){
			
			StringBuilder s = new StringBuilder();
			
			foreach(byte b in data){
				s.Append(b.ToString("X") + " ");
			}
			
			Console.WriteLine(s.ToString());
			
		}
		
	}
}
-- 
View this message in context: http://www.nabble.com/SerialPort-w--Even-Parity-not-working%2C-None-parity-OK-tp20975076p20975076.html
Sent from the Mono - General mailing list archive at Nabble.com.



More information about the Mono-list mailing list