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

C#

 
GeneralRe: Please help me understand serial communication Pin
turbosupramk313-Feb-12 4:18
turbosupramk313-Feb-12 4:18 
GeneralRe: Please help me understand serial communication Pin
turbosupramk317-Feb-12 11:52
turbosupramk317-Feb-12 11:52 
AnswerRe: Please help me understand serial communication Pin
Luc Pattyn17-Feb-12 17:36
sitebuilderLuc Pattyn17-Feb-12 17:36 
GeneralRe: Please help me understand serial communication Pin
turbosupramk317-Feb-12 18:33
turbosupramk317-Feb-12 18:33 
AnswerRe: Please help me understand serial communication Pin
Dan Mos12-Feb-12 2:11
Dan Mos12-Feb-12 2:11 
GeneralRe: Please help me understand serial communication Pin
turbosupramk312-Feb-12 4:21
turbosupramk312-Feb-12 4:21 
GeneralRe: Please help me understand serial communication Pin
Dan Mos12-Feb-12 5:44
Dan Mos12-Feb-12 5:44 
AnswerRe: Please help me understand serial communication Pin
turbosupramk312-Feb-12 7:44
turbosupramk312-Feb-12 7:44 
Ok, I'm actually a little nervous to try this Smile | :) how does this look? I spent all morning trying to sort this out, but have not tested it yet.

If a total rewrite isn't needed, should I have the threads looping all of the time, with a small wait time at the end? Or have the receive thread spawn the other threads?

Also, is serialPort1.ReadByte(), or is serialPort1.ReadLine() a better option?

Thanks for the help everyone, I've learned a lot!

C#
private void produceSerial()
        {
            try
            {
                List serialPortReadLine1 = new List(500);
                byte[] readLine = new byte[500];
                int readLineIndex = 0;
                while(globalVariables.receivingSerial == true)
                {
                    if (serialPort1.BytesToRead > 0) // example of bytes coming in @@@\rrpm=1000\rps1=20\rps2=35\r|||
                    {
                        readLine[readLineIndex] = Convert.ToByte(serialPort1.ReadByte());
                        readLineIndex = readLineIndex + 1;
                    }
                    if (readLine[0] == '@')
                    {
                        if (readLine[1] == '@')
                        {
                            if ((readLine[2] == '@') && (readLine[3] != 0))
                            {
                                serialPortReadLine1.Add(readLine[readLineIndex]);
                                if ((readLine[readLineIndex] == '|') && (readLine[readLineIndex - 1] == '|') && (readLine[readLineIndex - 2] == '|'))
                                {
                                    lock (globalVariables.linesRead)
                                    {
                                        int elements = globalVariables.linesRead.Count;
                                        for (int i = elements; i < (elements + 1); i++)
                                        {
                                            globalVariables.linesRead[i + 1] = serialPortReadLine1.ToString();
                                            serialPortReadLine1.Clear();
                                            readLineIndex = 0;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("produceSerial() failure " + ex.Message);
            }
        }

        private void parseSerial()
        {
            try
            {
                string [] linesUnparsed = new string[500];
                while (globalVariables.receivingSerial == true)
                {
                    if (globalVariables.linesRead.Count != 0)
                    {
                        lock (globalVariables.linesRead)
                        {
                            globalVariables.linesRead.CopyTo(linesUnparsed);
                            globalVariables.linesRead.Clear();
                        }

                        List charactersReplaced = new List(500);
                        string characterReplaced = "";
                        foreach (string line in linesUnparsed)
                        {
                            characterReplaced = line.Replace("\r", "<>");
                            charactersReplaced.Add(characterReplaced);
                        }

                        char[] splitCharacters = new char[] { '<', '>' };
                        string[] linesParsed = new string[500];
                        foreach (string lineUnparsed in linesUnparsed)
                        {
                            linesParsed = lineUnparsed.Split(splitCharacters, StringSplitOptions.None);
                        }
                        Array.Clear(linesUnparsed, 0, linesUnparsed.Length);

                        lock (globalVariables.linesParsedArray)
                        {
                            foreach (string lineParsed in linesParsed)
                            {
                                globalVariables.linesParsedArray.Add(lineParsed);
                            }
                            Array.Clear(linesParsed, 0, linesParsed.Length);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("parseSerial() failure " + ex.Message);
            }
        }

        private void consumeSerial()
        {
            try
            {
                string[] dataOutputQueue = new string[500];
                while (globalVariables.receivingSerial == true)
                {
                    if (globalVariables.linesParsedArray.Count != 0)
                    {
                        lock (globalVariables.lineRead)
                        {
                            globalVariables.linesParsedArray.CopyTo(dataOutputQueue);
                            globalVariables.linesParsedArray.Clear();
                        }

                        foreach (string dataOutputLine in dataOutputQueue)
                        {
                            if (dataOutputLine.Contains("ps1"))
                            {
                                // write to gui
                            }

                            if (dataOutputLine.Contains("ps2"))
                            {
                                // write to gui
                            }
                        }
                        Array.Clear(dataOutputQueue, 0, dataOutputQueue.Length);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("consumeSerial() failure " + ex.Message);
            }
        }

Questioncom add in excel2010 Pin
PozzaVecia11-Feb-12 0:19
PozzaVecia11-Feb-12 0:19 
AnswerRe: com add in excel2010 Pin
Eddy Vluggen11-Feb-12 3:29
professionalEddy Vluggen11-Feb-12 3:29 
GeneralRe: com add in excel2010 Pin
PozzaVecia11-Feb-12 6:18
PozzaVecia11-Feb-12 6:18 
AnswerRe: com add in excel2010 Pin
Abhinav S11-Feb-12 5:08
Abhinav S11-Feb-12 5:08 
GeneralRe: com add in excel2010 Pin
PozzaVecia11-Feb-12 5:31
PozzaVecia11-Feb-12 5:31 
GeneralRe: com add in excel2010 Pin
Abhinav S11-Feb-12 17:04
Abhinav S11-Feb-12 17:04 
GeneralRe: com add in excel2010 Pin
PozzaVecia11-Feb-12 6:09
PozzaVecia11-Feb-12 6:09 
Questionocr from japenese to english using c# with MODI Pin
udayakumard10-Feb-12 23:21
udayakumard10-Feb-12 23:21 
AnswerRe: ocr from japenese to english using c# with MODI Pin
Dave Kreskowiak11-Feb-12 2:08
mveDave Kreskowiak11-Feb-12 2:08 
QuestionAccess is denied Pin
jon-8010-Feb-12 22:29
professionaljon-8010-Feb-12 22:29 
AnswerRe: Access is denied Pin
fjdiewornncalwe11-Feb-12 5:40
professionalfjdiewornncalwe11-Feb-12 5:40 
QuestionWriting to an excel file Pin
JFRobertson10-Feb-12 20:12
JFRobertson10-Feb-12 20:12 
AnswerRe: Writing to an excel file Pin
Richard MacCutchan10-Feb-12 23:48
mveRichard MacCutchan10-Feb-12 23:48 
GeneralRe: Writing to an excel file Pin
JFRobertson11-Feb-12 8:08
JFRobertson11-Feb-12 8:08 
GeneralRe: Writing to an excel file Pin
Richard MacCutchan11-Feb-12 23:32
mveRichard MacCutchan11-Feb-12 23:32 
GeneralRe: Writing to an excel file Pin
Richard MacCutchan12-Feb-12 1:13
mveRichard MacCutchan12-Feb-12 1:13 
GeneralRe: Writing to an excel file Pin
JFRobertson12-Feb-12 8:49
JFRobertson12-Feb-12 8:49 

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.