[Mono-dev] System.IO.Ports not loading when running app

Shawn Singh radesh.singh at rfsmart.com
Mon Aug 15 17:33:15 EDT 2005



I would like to write an app in C# that will gather data that is being
collected on /dev/ttyS0 and push that data to a web service. I wrote my
app in C# in Visual Studio .NET 2005 and compiled it on a computer
running Mandrake Linux 9.2 with Mono v installed on it.


When I attempt to run the program I receive the following warning(s) /


** (Program.exe:13337): WARNING **: The class System.IO.Ports.SerialPort
could not be loaded, used in /export/home/shawn/src/c#/v2/Program.exe
(token 0x01000002). 
Unhandled Exception: System.NullReferenceException: Object reference not
set to an instance of an object


Here is my source code:

Here is a copy of my source code: 
// Part 1:  COM Listener & Data Receiver 
  This code will watch for the COM Port to be open 
  and when it is, it will read data from it into memory. 
  When the port is closed, the data will be pushed to the 
  web service (Part 2 is the push) 
using System.IO.Ports;        // NOT available in VS .NET 2003 
                                      // IS provided with VS .NET 2005 
                                      // TO work with MONO, you need to
have at least Mono v1.1.8 
// Part 2:  POST to Web Service 

  Transfer data that was captured from the COM Port 
  to the specified web service. 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Net; 
using System.Net.Sockets; 
using System.Text; 
using System.Web; 
namespace rfidapp 
 /// <summary> 
 /// Summary description for Class1. 
 /// </summary> 
    class Program 
   /// <summary> 
   /// The main entry point for the application. 
   /// </summary> 
   static void Main(string[] args) 
// begin serial port stuff 
      // Listen & Receive 
      string devName = "/dev/ttyS0";     // COM1 
      int readTimeOut = 300;   
      byte rawDTA; 
      string rXDTA = "thisizenishilizerdtuh"; 
      SerialPort COMPort = new SerialPort(devName); 
      COMPort.ReadTimeout = readTimeOut; 
      for (; ; ) 
     while (COMPort.IsOpen) 
    rawDTA = (byte)COMPort.ReadByte(); 
    rXDTA += ((char)rawDTA); 
     if (COMPort.IsOpen) 
// end serial port stuff
     // Post 
     if (!rXDTA.Contains("thisizenishilizerdtuh"))      // something to
prevent this code from being executed
    string uri = "http://smartserver/rfidlistener";     // bogus name
    byte[] bytes = new System.Text.UnicodeEncoding().GetBytes(rXDTA); 
    WebRequest request = WebRequest.Create(uri); 
    request.Method = "POST"; 
    request.ContentType = "text/html"; 
    request.ContentLength = bytes.Length; 
    Stream output = request.GetRequestStream(); 
    output.Write(bytes, 0, bytes.Length); 
    WebResponse response = request.GetResponse(); 
     Console.WriteLine("Listening for data ..."); 
  catch (Exception e) 
      Console.WriteLine("Error: " + e.Message.ToString()); 
I suspect that an instance of the SerialPort class cannot be created, so
that is causing the problem; however, I'm puzzled as to why that would
be the case.


Thank you,


Shawn Singh

Programmer, RFSmart

Jacksonville, FL 32256

904-399-8500 ext. 1667

shawn.singh at rfsmart.com


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20050815/cc9264a1/attachment.html 

More information about the Mono-devel-list mailing list