Click here to Skip to main content
15,885,546 members
Home / Discussions / C#
   

C#

 
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 
GeneralRe: Making Serial Port Visible Pin
Big Daddy Farang31-Oct-12 5:38
Big Daddy Farang31-Oct-12 5:38 
GeneralRe: Making Serial Port Visible Pin
C-P-User-331-Oct-12 6:56
C-P-User-331-Oct-12 6:56 
GeneralRe: Making Serial Port Visible Pin
C-P-User-331-Oct-12 7:02
C-P-User-331-Oct-12 7:02 
GeneralRe: Making Serial Port Visible Pin
David Knechtges31-Oct-12 7:16
David Knechtges31-Oct-12 7:16 
GeneralRe: Making Serial Port Visible Pin
C-P-User-331-Oct-12 7:39
C-P-User-331-Oct-12 7:39 
GeneralRe: Making Serial Port Visible Pin
Big Daddy Farang31-Oct-12 8:26
Big Daddy Farang31-Oct-12 8:26 
GeneralRe: Making Serial Port Visible Pin
C-P-User-32-Nov-12 7:52
C-P-User-32-Nov-12 7:52 
GeneralRe: Making Serial Port Visible Pin
C-P-User-331-Oct-12 8:09
C-P-User-331-Oct-12 8:09 
GeneralRe: Making Serial Port Visible Pin
Big Daddy Farang31-Oct-12 8:37
Big Daddy Farang31-Oct-12 8:37 
GeneralRe: Making Serial Port Visible Pin
C-P-User-331-Oct-12 10:29
C-P-User-331-Oct-12 10:29 
GeneralRe: Making Serial Port Visible Pin
C-P-User-32-Nov-12 6:58
C-P-User-32-Nov-12 6:58 
QuestionC# acess user roles Pin
classy_dog30-Oct-12 9:16
classy_dog30-Oct-12 9:16 
AnswerRe: C# acess user roles Pin
Pete O'Hanlon30-Oct-12 10:29
mvePete O'Hanlon30-Oct-12 10:29 
QuestionWPF - beginner Pin
Nitin S30-Oct-12 1:14
professionalNitin S30-Oct-12 1:14 

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.