Click here to Skip to main content
15,884,099 members
Home / Discussions / C#
   

C#

 
GeneralRe: how about this book? Pin
kidult11-Jan-15 0:43
kidult11-Jan-15 0:43 
AnswerRe: how about this book? Pin
BillWoodruff11-Jan-15 5:13
professionalBillWoodruff11-Jan-15 5:13 
GeneralRe: how about this book? Pin
kidult15-Jan-15 14:35
kidult15-Jan-15 14:35 
GeneralShockwave flash object Pin
Member 1136607910-Jan-15 21:01
Member 1136607910-Jan-15 21:01 
GeneralRe: Shockwave flash object Pin
Kornfeld Eliyahu Peter10-Jan-15 22:58
professionalKornfeld Eliyahu Peter10-Jan-15 22:58 
Questionquestion Pin
Syeada Sanjida Jahan10-Jan-15 18:03
Syeada Sanjida Jahan10-Jan-15 18:03 
AnswerRe: question Pin
Peter Leow10-Jan-15 18:20
professionalPeter Leow10-Jan-15 18:20 
QuestionHow to get array data in correct sequence from serial port in C# Pin
DredgPost10-Jan-15 9:52
DredgPost10-Jan-15 9:52 
My purpose in below program is getting 16 bytes of data from microcontroller and processing data for appropriate instructions. There are a lot of related questions and answers here but I couldnt find anything about in below issue. I can get 16 bytes from MCU. Values of bytes are correct and I can see them in dataGridView but the sequence of bytes is changing . For example at first MCUData[0] = 0x01 , MCUData[1] = 0xFE , MCUData[2] = 0xCC then it changes to MCUData[0] = 0xFE , MCUData[1] = 0xCC , MCUData[2] = 0x01. İt is like some problem shifting my datas in byte array. I am sure my MCU is sending data correctly because I checked in one of serial terminal program. My code is in below

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;

namespace SerialCommunicationMCU
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            dataGridView1.Columns.Add("MCUData", "Byte Name");
            dataGridView1.Columns.Add("MCUData", "Byte Value");
        }

        public System.IO.Ports.SerialPort SerialPc;
        

        #region Variables
        public string AvailablePort;
        public string[] Ports = SerialPort.GetPortNames();
        byte[] MCUData = new byte[16];
        #endregion


        private void Connect_Click(object sender, EventArgs e)
        {
            DataGreedByteNameShow();
            SerialConnectandRead();
            ConnectButton.Enabled = false;
            DisconnectButton.Enabled = true;
        }

        private void Disconnect_Click(object sender, EventArgs e)
        {
            SerialPc.Close();
            ConnectButton.Enabled = true;
            DisconnectButton.Enabled = false;
        }

        public void SerialConnectandRead()
        {
            SerialPc = new SerialPort(AvailablePort, 115200, Parity.None, 8, StopBits.One);

            try
            {
                SerialPc.Open();
                SerialPc.DataReceived += new SerialDataReceivedEventHandler(SerialPc_DataReceived);
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Serial Port Error");
            }

        }

        private void SerialPc_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPc.DiscardNull = false;
            SerialPc.Read(MCUData, 0, 16);
            SerialPc.ReceivedBytesThreshold = 16;
            DataGreedByteValueShow();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (string port in Ports)
            {
                comboBox1.Items.Add(port);
            }
            DisconnectButton.Enabled = false;
        }

        public void DataGreedByteNameShow()
        {
            dataGridView1.Rows.Add("MCUData[0]");
            dataGridView1.Rows.Add("MCUData[1]");
            dataGridView1.Rows.Add("MCUData[2]");
            dataGridView1.Rows.Add("MCUData[3]");
            dataGridView1.Rows.Add("MCUData[4]");
            dataGridView1.Rows.Add("MCUData[5]");
            dataGridView1.Rows.Add("MCUData[6]");
            dataGridView1.Rows.Add("MCUData[7]");
            dataGridView1.Rows.Add("MCUData[8]");
            dataGridView1.Rows.Add("MCUData[9]");
            dataGridView1.Rows.Add("MCUData[10]");
            dataGridView1.Rows.Add("MCUData[11]");
            dataGridView1.Rows.Add("MCUData[12]");
            dataGridView1.Rows.Add("MCUData[13]");
            dataGridView1.Rows.Add("MCUData[14]");
            dataGridView1.Rows.Add("MCUData[15]");
        }

        private void DataGreedByteValueShow()
        {
            dataGridView1.Rows[0].Cells[1].Value = MCUData[0];
            dataGridView1.Rows[1].Cells[1].Value = MCUData[1];
            dataGridView1.Rows[2].Cells[1].Value = MCUData[2];
            dataGridView1.Rows[3].Cells[1].Value = MCUData[3];
            dataGridView1.Rows[4].Cells[1].Value = MCUData[4];
            dataGridView1.Rows[5].Cells[1].Value = MCUData[5];
            dataGridView1.Rows[6].Cells[1].Value = MCUData[6];
            dataGridView1.Rows[7].Cells[1].Value = MCUData[7];
            dataGridView1.Rows[8].Cells[1].Value = MCUData[8];
            dataGridView1.Rows[9].Cells[1].Value = MCUData[9];
            dataGridView1.Rows[10].Cells[1].Value = MCUData[10];
            dataGridView1.Rows[11].Cells[1].Value = MCUData[11];
            dataGridView1.Rows[12].Cells[1].Value = MCUData[12];
            dataGridView1.Rows[13].Cells[1].Value = MCUData[13];
            dataGridView1.Rows[14].Cells[1].Value = MCUData[14];
            dataGridView1.Rows[15].Cells[1].Value = MCUData[15];
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            AvailablePort = comboBox1.SelectedItem.ToString();
        }
    }
}


modified 10-Jan-15 18:35pm.

SuggestionRe: How to get array data in correct sequence from serial port in C# Pin
Richard MacCutchan10-Jan-15 21:40
mveRichard MacCutchan10-Jan-15 21:40 
GeneralRe: How to get array data in correct sequence from serial port in C# Pin
DredgPost11-Jan-15 1:48
DredgPost11-Jan-15 1:48 
AnswerRe: How to get array data in correct sequence from serial port in C# Pin
OriginalGriff10-Jan-15 22:39
mveOriginalGriff10-Jan-15 22:39 
GeneralRe: How to get array data in correct sequence from serial port in C# Pin
DredgPost11-Jan-15 11:39
DredgPost11-Jan-15 11:39 
Generalspeack to text Pin
Mạnh Tuyên9-Jan-15 17:27
Mạnh Tuyên9-Jan-15 17:27 
GeneralRe: speack to text Pin
OriginalGriff9-Jan-15 21:30
mveOriginalGriff9-Jan-15 21:30 
GeneralRe: speack to text Pin
Pete O'Hanlon10-Jan-15 2:44
mvePete O'Hanlon10-Jan-15 2:44 
AnswerRe: speack to text Pin
Afzaal Ahmad Zeeshan10-Jan-15 9:26
professionalAfzaal Ahmad Zeeshan10-Jan-15 9:26 
QuestionMultiline textbox scrollbars cannot be moved Pin
robwm19-Jan-15 13:00
robwm19-Jan-15 13:00 
AnswerRe: Multiline textbox scrollbars cannot be moved Pin
Eddy Vluggen9-Jan-15 22:04
professionalEddy Vluggen9-Jan-15 22:04 
GeneralRe: Multiline textbox scrollbars cannot be moved Pin
robwm112-Jan-15 7:39
robwm112-Jan-15 7:39 
GeneralRe: Multiline textbox scrollbars cannot be moved Pin
Eddy Vluggen12-Jan-15 7:56
professionalEddy Vluggen12-Jan-15 7:56 
GeneralRe: Multiline textbox scrollbars cannot be moved Pin
robwm112-Jan-15 7:59
robwm112-Jan-15 7:59 
GeneralRe: Multiline textbox scrollbars cannot be moved Pin
Eddy Vluggen12-Jan-15 10:14
professionalEddy Vluggen12-Jan-15 10:14 
GeneralRe: Multiline textbox scrollbars cannot be moved Pin
robwm112-Jan-15 10:28
robwm112-Jan-15 10:28 
Question3 Dimensional Array C# Pin
Member 113642179-Jan-15 10:35
Member 113642179-Jan-15 10:35 
AnswerRe: 3 Dimensional Array C# Pin
PIEBALDconsult9-Jan-15 10:51
mvePIEBALDconsult9-Jan-15 10:51 

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.