Click here to Skip to main content
15,919,749 members
Home / Discussions / C#
   

C#

 
AnswerRe: distribute stored procedure files Pin
SilimSayo14-Nov-11 10:42
SilimSayo14-Nov-11 10:42 
QuestionMouse move (hover) over an Image with Graphics drawings Pin
eyalbi00714-Nov-11 2:12
eyalbi00714-Nov-11 2:12 
AnswerRe: Mouse move (hover) over an Image with Graphics drawings Pin
Luc Pattyn14-Nov-11 2:43
sitebuilderLuc Pattyn14-Nov-11 2:43 
QuestionClass for Serail Port Comms Pin
KeithF14-Nov-11 1:24
KeithF14-Nov-11 1:24 
AnswerRe: Class for Serail Port Comms Pin
Luc Pattyn14-Nov-11 1:48
sitebuilderLuc Pattyn14-Nov-11 1:48 
GeneralRe: Class for Serail Port Comms Pin
KeithF14-Nov-11 4:24
KeithF14-Nov-11 4:24 
AnswerRe: Class for Serail Port Comms Pin
Luc Pattyn14-Nov-11 5:06
sitebuilderLuc Pattyn14-Nov-11 5:06 
GeneralRe: Class for Serail Port Comms Pin
KeithF16-Nov-11 4:06
KeithF16-Nov-11 4:06 
Hi Luc,

Quick question, I have created a class for the message protocol using States I think, i'm just wondering if this is the correct approach:

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

namespace TEST
{
    public class clsTEST
    {
        public enum CurrentCommsState
        {
            Idle,
            SentENQ,
            RecvdENQ,
            SentACK0,
            RecvdACK0,
            SentACK1,
            RecvdACK1,
            SentMessage,
            RecvdMessage,
            SentEOT,
            RecvdEOT
        }
        private enum MessageDirection
        {
            In,
            Out
        }

        public CurrentCommsState TheState = new CurrentCommsState();
        private MessageDirection TheDirection = new MessageDirection();
        private SerialPort spTest= null;
        private byte[] btRecvdMsg = new byte[200];

        public clsOpenShift(SerialPort spTheSP)
        {
            spTEST= spTheSP;
            TheState = CurrentCommsState.Idle;
            TheDirection = MessageDirection.Out;
        }

        public void SendAndRecv(out byte[] TheBufferRecvd)
        {
            TheBufferRecvd = new Byte[200];

            if (SendMsg())
            {
                if (RecvMsg(ref btRecvdMsg, 1))
                {
                    Array.Copy(btRecvdMsg, TheBufferRecvd, 200);
                }
            }
            else if (RecvMsg(ref btRecvdMsg, 1))
            {
                if (SendMsg())
                {

                }
            }
        }

        private bool RecvMsg(ref byte[] btOutRecvd, int iBytesToRecv)
        {
            bool blnRetVal = false;

            try
            {
                while (spOpenShiftSP.BytesToRead == 0)
                {
                    System.Threading.Thread.Sleep(100);
                }

                byte[] theBuffer = new byte[iBytesToRecv];
                spTEST.Read(theBuffer, 0, iBytesToRecv);
                ChangeState(theBuffer);
                Array.Copy(theBuffer, btOutRecvd, theBuffer.Length);

                blnRetVal = true;
            }
            catch (Exception e)
            {
                blnRetVal = false;
            }

            return blnRetVal;
        }

        private bool SendMsg()
        {
            bool blnRetVal = false;
            
            try
            {
                byte[] btToSend = MessageToSend();
                ChangeState(btToSend);

                if (btToSend != null)
                {
                    spTEST.Write(btToSend, 0, btToSend.Length);
                    blnRetVal = true;
                }
                else
                {
                    blnRetVal = false;
                }
            }
            catch (Exception e)
            {
                blnRetVal = false;
            }

            return blnRetVal;
        }

        private void ChangeState(byte[] theMessage)
        {
            if (TheState == CurrentCommsState.Idle)
            {
                TheState = CurrentCommsState.SentENQ;
            }
            else if (TheState == CurrentCommsState.SentENQ &&string.Compare(ByteToHex(theMessage).Trim(),"THE CORRECT ACK (EITHER 06 or 07)") == 0)
            {

            }
        }

        private byte[] MessageToSend()
        {
            if (TheState == CurrentCommsState.Idle)
            {
                byte[] ENQ = new byte[] { 0x05 };
                return ENQ;                
            }
            else if (TheState == CurrentCommsState.SentENQ)
            {
                byte[] STX_MSG_ETX_LRC = new byte[] {
                       0x02, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31,
                       0x30, 0x30, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 
                       0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x31, 0x20, 0x20, 0x20, 
                       0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x03,
                       0x02 
                    };
                return STX_MSG_ETX_LRC;
            }
            else if(TheState == CurrentCommsState.SentMessage)
            {
                byte[] EOT = new byte[] { 0x04 };
                return EOT;
            }
            else if (TheState == CurrentCommsState.SentEOT)
            {
                return null;
            }
            else
            {
                return new byte[1];
            }
        }

        private string ByteToHex(byte[] comByte)
        {
            StringBuilder builder = new StringBuilder(comByte.Length * 3);

            foreach (byte data in comByte)
            {
                builder.Append(Convert.ToString(data, 16).PadLeft(2, '0').PadRight(3, ' '));
            }
            
            return builder.ToString().ToUpper();
        }

    }
}


I will eventually change the STX_Msg to use Member Vars to populate the correct message not a hardcoded set of values as it is currently.

Also calling the code like so:

C#
byte[] btRecvdMsg = null;
            clsTest COMMS = new clsTest(spSerialPort);

            while (COMMS.TheState != clsTest.CurrentCommsState.RecvdEOT)
            {
                COMMS.SendAndRecv(out btRecvdMsg);
                System.Threading.Thread.Sleep(100);
            }


Am I on the right tracks here?

Thanks in Advance
AnswerRe: Class for Serail Port Comms Pin
Luc Pattyn16-Nov-11 16:02
sitebuilderLuc Pattyn16-Nov-11 16:02 
QuestionIterate all child forms and saving all reports to one excel. Pin
pinifg14-Nov-11 1:18
pinifg14-Nov-11 1:18 
AnswerRe: Iterate all child forms and saving all reports to one excel. Pin
Blue_Boy14-Nov-11 1:26
Blue_Boy14-Nov-11 1:26 
GeneralRe: Iterate all child forms and saving all reports to one excel. Pin
pinifg14-Nov-11 2:45
pinifg14-Nov-11 2:45 
AnswerRe: Iterate all child forms and saving all reports to one excel. Pin
DaveyM6914-Nov-11 1:45
professionalDaveyM6914-Nov-11 1:45 
QuestionConvert To English To Gujarati Pin
ramesh dabhi13-Nov-11 22:38
ramesh dabhi13-Nov-11 22:38 
AnswerRe: Convert To English To Gujarati Pin
Keith Barrow13-Nov-11 23:20
professionalKeith Barrow13-Nov-11 23:20 
AnswerRe: Convert To English To Gujarati Pin
Abhinav S14-Nov-11 1:42
Abhinav S14-Nov-11 1:42 
AnswerRe: Convert To English To Gujarati Pin
thatraja14-Nov-11 1:57
professionalthatraja14-Nov-11 1:57 
QuestionMultiple lines : datagridview Pin
abbd13-Nov-11 11:24
abbd13-Nov-11 11:24 
AnswerRe: Multiple lines : datagridview Pin
Richard MacCutchan13-Nov-11 22:42
mveRichard MacCutchan13-Nov-11 22:42 
AnswerRe: Multiple lines : datagridview Pin
thatraja14-Nov-11 2:05
professionalthatraja14-Nov-11 2:05 
QuestionCheckers Board game error Pin
xnaLearner13-Nov-11 11:03
xnaLearner13-Nov-11 11:03 
AnswerRe: Checkers Board game error Pin
Pete O'Hanlon13-Nov-11 11:27
mvePete O'Hanlon13-Nov-11 11:27 
QuestionMessage Removed Pin
13-Nov-11 9:24
AghaKhan13-Nov-11 9:24 
AnswerRe: What this mean ??. This code works fine Pin
Luc Pattyn13-Nov-11 9:33
sitebuilderLuc Pattyn13-Nov-11 9:33 
GeneralRe: What this mean ??. This code works fine Pin
AghaKhan13-Nov-11 16:00
AghaKhan13-Nov-11 16:00 

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.