[Mono-dev] Mono C# Serial Port problem
christopher jones
chris.jones at goscience.co.uk
Mon May 18 04:56:07 EDT 2009
Hi guys, I had som eissues with the RS232 implementation in mono. I found
that the example of using polling did not work inside linux and would like
to suggest the following code instead:
I have tried this on both Windows and under linux in a virtual Fedora
machine and it does work.
using System;
using System.Collections.Generic;
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", 19200);
mySerial.Open();
mySerial.ReadTimeout = 400;
while (true)
{
Console.WriteLine(ReadData());
}
}
public string ReadData()
{
byte tmpByte;
string rxString = "";
try
{
tmpByte = (byte)mySerial.ReadByte();
while (true)
{
Console.WriteLine("Data Received");
try
{
rxString += ((char)tmpByte);
tmpByte = (byte)mySerial.ReadByte();
}
catch
{
rxString += "!2";
break;
}
}
}
catch
{
rxString+="!1";
}
return rxString;
}
public bool SendData(string Data)
{
mySerial.Write(Data);
return true;
}
}
--
View this message in context: http://www.nabble.com/Mono-C--Serial-Port-problem-tp17241892p23593597.html
Sent from the Mono - Dev mailing list archive at Nabble.com.
More information about the Mono-devel-list
mailing list