Click here to Skip to main content
15,889,281 members
Home / Discussions / C#
   

C#

 
Questionreading from a dynamically created textarea Pin
Hbr_Tha_real27-Aug-13 4:27
Hbr_Tha_real27-Aug-13 4:27 
AnswerRe: reading from a dynamically created textarea Pin
Jason Gleim27-Aug-13 4:59
professionalJason Gleim27-Aug-13 4:59 
GeneralRe: reading from a dynamically created textarea Pin
Hbr_Tha_real28-Aug-13 1:53
Hbr_Tha_real28-Aug-13 1:53 
GeneralRe: reading from a dynamically created textarea Pin
Jason Gleim28-Aug-13 7:24
professionalJason Gleim28-Aug-13 7:24 
GeneralRe: reading from a dynamically created textarea Pin
Hbr_Tha_real28-Aug-13 11:19
Hbr_Tha_real28-Aug-13 11:19 
GeneralRe: reading from a dynamically created textarea Pin
Jason Gleim29-Aug-13 4:14
professionalJason Gleim29-Aug-13 4:14 
GeneralRe: reading from a dynamically created textarea Pin
Hbr_Tha_real29-Aug-13 12:16
Hbr_Tha_real29-Aug-13 12:16 
QuestionSerial Port reading char string Pin
Blubbo27-Aug-13 3:02
Blubbo27-Aug-13 3:02 
Oddly is that when I send the 9 character length of a string (i.e. 123A45678), my serial port dataReceived event recieves the message but it reads as:

123A4567
8

Unsure on how to get the whole string as was sent by the other com port?

here's my code:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

using RS232;

namespace RemoteCFG
{
    public class RemoteConfiguration
    {
        SerialPort serialPort;
        RS232Comm rs232Comm = new RS232Comm();
        Object lockingObj = new object();

        public RemoteConfiguration()
        {
            
        }

        public string strMessage { get; set; }
        public string cfgNumber { get; set; }
        public string vloValue { get; set; }
        public string atcValue { get; set; }
            

        public List<string> EnumerateComPorts()
        {
            return rs232Comm.GetAvailableComPorts();
        }

        public int OpenPort(string comPort)
        {
            serialPort = rs232Comm.OpenPort(comPort);
            serialPort.DataReceived +=new SerialDataReceivedEventHandler(serialPort_DataReceived);

            if (serialPort != null)
                return 1;
            else
                return 0;
        }

        public int ClosePort()
        {
            try
            {
                serialPort.Close();
                return 1;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string inData = sp.ReadExisting().Trim();
            strMessage = inData;

            switch (inData)
            {
                case "PLC": SendMsg("OK"); break;
                case "CFG": ValidateCFG(); break;
                case "OFF": serialPort.Close(); break;
                case "VLO": GetVLO(); break;
                case "ATC": GetATC(); break;
                default: System.Diagnostics.Debug.WriteLine(strMessage); break;
            }
        }

        private void GetATC()
        {
            if (atcValue != null && atcValue != string.Empty)
            {
                int value = 0;

                if (int.TryParse(atcValue, out value))
                {
                    if (value >= 0 || value <= 99)
                    {
                        SendMsg(atcValue);
                    }
                }
            }
        }

        private void GetVLO()
        {
            if (vloValue != null && vloValue != string.Empty)
            {
                int value = 0;

                if (int.TryParse(vloValue, out value))
                {
                    if (value >= 60 || value <= 120)
                    {
                        SendMsg(vloValue);
                    }
                }
            }
        }

        private void ValidateCFG()
        {
            if (cfgNumber != null && cfgNumber != null && cfgNumber != string.Empty)
            {
                if (cfgNumber.Length == 9) 
                {
                    try
                    {
                        int Num = 0;

                        if (int.TryParse(cfgNumber.Substring(0, 3), out Num))
                        {
                            int errorCounter = System.Text.RegularExpressions.Regex.Matches(cfgNumber.Substring(3, 1), @"[a-zA-Z]").Count;

                            if (errorCounter == 1) 
                            {
                                if (int.TryParse(cfgNumber.Substring(4), out Num))
                                    SendMsg(cfgNumber);
                            }
                        }
                    }
                    catch
                    {
                    }
                }
            }
        }

        public int GetCFG(ref string configNumber)
        {
            int status = 0;

            try
            {
                SendMsg("CFG");
                configNumber = strMessage;
            }
            catch
            {
            }

            return status;
        }

        int GetDynData(string[] dynamicData)
        {
            int status = 0;
            return status;
        }

        public int SendMsg(string msg)
        {
            int status = 0;
            
            serialPort.Write(msg + "\r");

            return status;
        }
    }
}


The serial port is initilized as:
C#
serialPort = new SerialPort(comPortName, 9600, Parity.Even, 7, StopBits.One);
serialPort.Handshake = Handshake.XOnXOff;

AnswerRe: Serial Port reading char string Pin
OriginalGriff27-Aug-13 3:41
mveOriginalGriff27-Aug-13 3:41 
AnswerRe: Serial Port reading char string Pin
Jason Gleim27-Aug-13 5:24
professionalJason Gleim27-Aug-13 5:24 
QuestionHow to paste outside of my application Pin
Mahmoud EL-Shazly26-Aug-13 13:32
Mahmoud EL-Shazly26-Aug-13 13:32 
AnswerRe: How to paste outside of my application Pin
BillWoodruff26-Aug-13 13:46
professionalBillWoodruff26-Aug-13 13:46 
GeneralRe: How to paste outside of my application Pin
Mahmoud EL-Shazly27-Aug-13 11:51
Mahmoud EL-Shazly27-Aug-13 11:51 
GeneralRe: How to paste outside of my application Pin
Bernhard Hiller27-Aug-13 20:38
Bernhard Hiller27-Aug-13 20:38 
GeneralRe: How to paste outside of my application Pin
Mahmoud EL-Shazly28-Aug-13 0:42
Mahmoud EL-Shazly28-Aug-13 0:42 
GeneralRe: How to paste outside of my application Pin
DaveyM6928-Aug-13 1:35
professionalDaveyM6928-Aug-13 1:35 
GeneralRe: How to paste outside of my application Pin
Mahmoud EL-Shazly1-Oct-13 20:01
Mahmoud EL-Shazly1-Oct-13 20:01 
AnswerRe: How to paste outside of my application Pin
Bernhard Hiller26-Aug-13 20:52
Bernhard Hiller26-Aug-13 20:52 
AnswerRe: How to paste outside of my application Pin
OriginalGriff27-Aug-13 3:42
mveOriginalGriff27-Aug-13 3:42 
GeneralRe: How to paste outside of my application Pin
Mahmoud EL-Shazly27-Aug-13 14:17
Mahmoud EL-Shazly27-Aug-13 14:17 
QuestionStrongname vs Obfuscation - resistant to tampering? Pin
devvvy26-Aug-13 13:01
devvvy26-Aug-13 13:01 
AnswerRe: Strongname vs Obfuscation - resistant to tampering? Pin
Abhinav S26-Aug-13 19:09
Abhinav S26-Aug-13 19:09 
GeneralRe: Strongname vs Obfuscation - resistant to tampering? Pin
devvvy26-Aug-13 20:02
devvvy26-Aug-13 20:02 
GeneralRe: Strongname vs Obfuscation - resistant to tampering? Pin
Eddy Vluggen26-Aug-13 23:15
professionalEddy Vluggen26-Aug-13 23:15 
GeneralRe: Strongname vs Obfuscation - resistant to tampering? Pin
Dave Kreskowiak27-Aug-13 2:12
mveDave Kreskowiak27-Aug-13 2:12 

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.