Click here to Skip to main content
15,891,708 members
Home / Discussions / C#
   

C#

 
Questioncasting at runtime in c# Pin
prasadbuddhika31-Oct-12 6:15
prasadbuddhika31-Oct-12 6:15 
AnswerRe: casting at runtime in c# Pin
Simon_Whale31-Oct-12 6:31
Simon_Whale31-Oct-12 6:31 
GeneralRe: casting at runtime in c# Pin
prasadbuddhika31-Oct-12 6:34
prasadbuddhika31-Oct-12 6:34 
GeneralRe: casting at runtime in c# Pin
Simon_Whale31-Oct-12 6:41
Simon_Whale31-Oct-12 6:41 
GeneralRe: casting at runtime in c# Pin
prasadbuddhika31-Oct-12 6:48
prasadbuddhika31-Oct-12 6:48 
GeneralRe: casting at runtime in c# Pin
Simon_Whale31-Oct-12 6:48
Simon_Whale31-Oct-12 6:48 
GeneralRe: casting at runtime in c# Pin
prasadbuddhika31-Oct-12 17:04
prasadbuddhika31-Oct-12 17:04 
GeneralRe: casting at runtime in c# Pin
Simon_Whale1-Nov-12 0:45
Simon_Whale1-Nov-12 0:45 
GeneralRe: casting at runtime in c# Pin
prasadbuddhika1-Nov-12 4:24
prasadbuddhika1-Nov-12 4:24 
GeneralRe: casting at runtime in c# Pin
Simon_Whale1-Nov-12 5:05
Simon_Whale1-Nov-12 5:05 
GeneralRe: casting at runtime in c# Pin
prasadbuddhika1-Nov-12 5:25
prasadbuddhika1-Nov-12 5:25 
GeneralRe: casting at runtime in c# Pin
BobJanova1-Nov-12 5:19
BobJanova1-Nov-12 5:19 
GeneralRe: casting at runtime in c# Pin
prasadbuddhika1-Nov-12 5:28
prasadbuddhika1-Nov-12 5:28 
QuestionC# Experts Pin
Member 955692930-Oct-12 19:32
Member 955692930-Oct-12 19:32 
AnswerRe: C# Experts Pin
Pete O'Hanlon30-Oct-12 21:29
mvePete O'Hanlon30-Oct-12 21:29 
QuestionSocket read deserialization exception (fyi - problem already resolved) Pin
devvvy30-Oct-12 17:19
devvvy30-Oct-12 17:19 
QuestionMaking Serial Port Visible Pin
C-P-User-330-Oct-12 11:35
C-P-User-330-Oct-12 11:35 
AnswerRe: Making Serial Port Visible Pin
Pete O'Hanlon30-Oct-12 11:59
mvePete O'Hanlon30-Oct-12 11:59 
AnswerRe: Making Serial Port Visible Pin
Big Daddy Farang30-Oct-12 12:05
Big Daddy Farang30-Oct-12 12:05 
GeneralRe: Making Serial Port Visible Pin
C-P-User-330-Oct-12 12:33
C-P-User-330-Oct-12 12:33 
GeneralRe: Making Serial Port Visible Pin
C-P-User-330-Oct-12 13:09
C-P-User-330-Oct-12 13:09 
GeneralRe: Making Serial Port Visible Pin
Big Daddy Farang30-Oct-12 13:42
Big Daddy Farang30-Oct-12 13:42 
Here's some sample code that might help. I've removed most of the stuff, but left some to give you some ideas. I used this in its original form to communicate with a device, a bit different from what you're doing in terms of amount of traffic.

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Ports;

namespace YourNameHere
{
   class Device
   {
      private SerialPort Port;
      private const int BufferSize = 256;
      private char[] ReadBuffer;
      private string LastResponse;

      
      public Device(string CommPort)
      {
         if (CommPort.IndexOf("COM", StringComparison.CurrentCultureIgnoreCase) < 0)
         {
            throw new ArgumentException("Argument must be of the form \"COMn\"", "CommPort");
         }
         // the SerialPort constructor may also throw an exception
         Port = new SerialPort(CommPort, 9600, Parity.None, 8, StopBits.One);

         Port.RtsEnable = true;
         Port.DtrEnable = true;
         Port.ReadTimeout = 500;
         Port.WriteTimeout = 500;
         Port.Open();
         Port.DiscardInBuffer();

         ReadBuffer = new char[BufferSize];
      }

      
      // might be handy for debug
      public string GetLastResponse()
      {
         return LastResponse;
      }

      public bool Connect()
      {
         Send("");
         LastResponse = Receive();
         if (LastResponse == "!>")
         {
            return true;
         }
         else
         {
            return false;
         }
      }

      
      public bool SetLoad(int Load)
      {
         string digits;
         if (Load < 10 || Load > 5200)
         {
            throw new ArgumentOutOfRangeException("Load", "Must be between 10 and 5200");
         }

         digits = ResistanceTable(Load);
         Send("L" + digits);
         LastResponse = Receive();
         if (LastResponse == "!>")
         {
            return true;
         }
         else
         {
            return false;
         }
      }

      
      public bool StartMeasuring()
      {
         Send("S");
         LastResponse = Receive();
         if (LastResponse == "!>")
         {
            return true;
         }
         else
         {
            return false;
         }
      }

      public bool StopMeasuring()
      {
         Send("E");
         LastResponse = Receive();
         if (LastResponse == "!>")
         {
            return true;
         }
         else
         {
            return false;
         }
      }
      

      public void Send(string Command)
      {
         if (Port.IsOpen)
         {
            char[] buffer = Command.ToCharArray();
            Port.Write(buffer, 0, buffer.Length);
            Port.Write(new char[] { (char)0x0D }, 0, 1);
         }
      }

      public string Receive()
      {
         const int delay = 75;
         int received = 0;
         char ch;
         StringBuilder result = new StringBuilder();

         if (Port.IsOpen)
         {
            System.Threading.Thread.Sleep(delay);
            try
            {
               received = Port.Read(ReadBuffer, 0, BufferSize);
            }
            catch (TimeoutException)
            {
               result.Append("Error.");
            }
            if (received > 0)
            {
               for (int loop = 0; loop < received; loop++)
               {
                  ch = ReadBuffer[loop];
                  if (ch >= ' ' && ch <= '~')
                  {
                     result.Append(ch);
                  }
               }
            }
         }
         else
         {
            result.Append("Not open.");
         }
         return result.ToString();
      }

      public void Close()
      {
         if (Port.IsOpen)
         {
            Port.Close();
         }
      }
      
   }
}

BDF

I often make very large prints from unexposed film, and every one of them turns out to be a picture of myself as I once dreamed I would be.
-- BillWoodruff

GeneralRe: Making Serial Port Visible Pin
C-P-User-331-Oct-12 4:43
C-P-User-331-Oct-12 4:43 
GeneralRe: Making Serial Port Visible Pin
Pete O'Hanlon31-Oct-12 5:02
mvePete O'Hanlon31-Oct-12 5:02 
GeneralRe: Making Serial Port Visible Pin
C-P-User-32-Nov-12 6:47
C-P-User-32-Nov-12 6:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.