[Mono-list] C#/Mono Serial port communication
Jon-Eirik Pettersen
lists@jonepet.net
Wed, 24 Nov 2004 23:30:44 +0100
Gonzalo Paniagua Javier wrote:
> On Wed, 2004-11-24 at 22:22 +0100, Jon-Eirik Pettersen wrote:
>
>>I don't think I have enough information to do it.
>>
>>It says the stream is seek-able because it thinks it is a normal file it
>>is opening, but seeking do not seems to work on devices.
>
>
> The bug is exactly that. The runtime should figure out whether the file
> is seekable or not.
>
>
>>But if I send the data directly to the FileStream using the
>>Write-function it works, but I have problems reading the data from the
>>stream.
>
>
> Try using the stream in the file attached.
>
> -Gonzalo
>
>
>
>
> ------------------------------------------------------------------------
>
> using System.IO;
>
> class NoSeekStream : FileStream {
> public NoSeekStream (string name, FileMode mode, FileAccess access)
> : base (name, mode, access)
> {
> }
>
> public override bool CanSeek {
> get { return false; }
> }
> }
>
I have made a buggy application that is able to send data (if I send
ATZxxxxxxxx I can see in the other end that it works), but it is not
able to receive data:
using System;
using System.Text;
using System.IO;
using System.Threading;
using System.Collections;
namespace serialchattest {
class serialchattest {
static string device = "/dev/rfcomm0";
static ArrayList rbuf = new ArrayList();
static FileStream fs = null;
static StreamReader sr = null;
static void SendCommand(string cmd) {
try {
string data = cmd + "\r\n";
byte[] bt = Encoding.UTF8.GetBytes(data);
fs.Write(bt, (int) fs.Position, bt.Length);
sr.ReadLine();
} catch (Exception) {} finally {}
}
static void ReadResponse() {
string data = null;
while ((data = sr.ReadLine()) != null) {
rbuf.Add(data);
}
}
static string GetResponse() {
try {
if (rbuf.Count > 0) {
string ret = (string) rbuf[0];
rbuf.RemoveAt(0);
return ret;
} else {
return null;
}
} catch (Exception) { return null; } finally {}
}
static void Main() {
fs = new FileStream(device, FileMode.Open, FileAccess.ReadWrite,
0, 1024, true);
sr = new StreamReader(fs);
SendCommand("AT");
string res = null;
ReadResponse();
while ((res = GetResponse()) != null) {
Console.WriteLine(res);
}
}
}
}
The result is:
Unhandled Exception: System.NullReferenceException: Object reference not
set to an instance of an object
Unhandled Exception: System.NullReferenceException: Object reference not
set to an instance of an object
Unhandled Exception: System.NullReferenceException: Object reference not
set to an instance of an object
Unhandled Exception: System.NullReferenceException: Object reference not
set to an instance of an object