Sida 1 av 1

C# newbie fråga

Postat: 15 april 2007, 18:04:08
av thepirateboy
Jag har ett terminalprogram med nedanstående kod. Jag vill visa inkommande data i hexformat. Jag vill dessutom invertera alla bitar samt "spegelvända" byten så att LSB blir MSB. Har någon tips på hur man kan göra?

Kod: Markera allt

        // we want to have the serial port thread report back data received, but to display
        // that data we must create a delegate function to show the data in the richTextBox

        // define the delegate 
        public delegate void SetText();
        // define an instance of the delegate
        SetText setText;

        // create a string that will be loaded with the data received from the port
        public string str = "";

        // note that this function runs in a separate thread and thus we must use a delegate in order
        // to display the results in the richTextBox.
        //void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        //private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            // instantiate the delegate to be invoked by this thread
            setText = new SetText(mySetText);

            // load the data into the string
            try
            {
                str = serialPort1.ReadExisting();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error - port_DataReceived Exception: " + ex);
            }

            // invoke the delegate in the MainForm thread
            this.Invoke(setText);
        }

        // create the instance of the delegate to be used to write the received data to the richTextBox
        public void mySetText()
        {
            // show the text
            richTextBox2.Text += str.ToString();      

            moveCaretToEnd();
        }

        // This rigaramole is needed to keep the last received item displayed
        // it kind of flickers and should be fixed
        private void richTextBoxReceive_TextChanged(object sender, System.EventArgs e)
        {
            moveCaretToEnd();
        }

        private void moveCaretToEnd()
        {
            richTextBox1.SelectionStart = richTextBox1.Text.Length;
            richTextBox1.SelectionLength = 0;
            richTextBox1.ScrollToCaret();
        }

        #endregion


    }

Postat: 15 april 2007, 18:22:27
av pheer
Till att börja med kan du inte läsa in som text. Då finns det en risk att du
förlorar värden. Läs in som char, int eller long beroende på ordstorlek.

Använd xor-operatorn för att invertera:

Kod: Markera allt

invertedWord = word ^ (pow(2, wordsize) - 1);
pow är funktionen för upphöjt i c++ (gissar att den heter likadant i c#).
Har du en konstant ordstorlek kan du ersätta med en konstant, t.ex. 255
för 8-bitars ord.

*ändrad*

Postat: 15 april 2007, 19:45:03
av TERdON
Spegelvändningen blev väl ändå fel nu?

Tyvärr kommer jag inte på något bättre sätt än att skyffla runt bit för bit. Snyggast blir det nog med en for-sats och bit-maskar... Men jag kanske missar något grymt knep nu?

Postat: 15 april 2007, 20:07:03
av pheer
Det är så sant som det är sagt. Det finns nog ingen genväg. Ändrar ovan.