C# newbie fråga

Elektronik- och mekanikrelaterad mjukvara/litteratur. (T.ex schema-CAD, simulering, böcker, manualer mm. OS-problem hör inte hit!)
thepirateboy
EF Sponsor
Inlägg: 2109
Blev medlem: 27 augusti 2005, 20:57:58
Ort: Borlänge

C# newbie fråga

Inlägg 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


    }
pheer
EF Sponsor
Inlägg: 1283
Blev medlem: 16 januari 2005, 18:05:21

Inlägg 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*
Senast redigerad av pheer 15 april 2007, 20:07:29, redigerad totalt 1 gång.
TERdON
EF Sponsor
Inlägg: 295
Blev medlem: 15 november 2006, 04:38:29
Ort: Solna/Laholm
Kontakt:

Inlägg 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?
pheer
EF Sponsor
Inlägg: 1283
Blev medlem: 16 januari 2005, 18:05:21

Inlägg av pheer »

Det är så sant som det är sagt. Det finns nog ingen genväg. Ändrar ovan.
Skriv svar