j ai fais un programme pour un appareil qui permet de calculer la quantite d oxigene ds le corps et fréquence cardiaque et j aimerai montrer ces valeur ds la listview ou sur label et ca ce que j arrive pas a le realiser .
avec laide de messagebox.show je peux voir tout mes resultas quand j essaye a ecrire les resultat ds label je recois um mess d exception:Cross-thread operation not valid: Control 'label_HR' accessed from a thread other than the thread it was created on. y a t il qlq qui peut m aide a resoudre ce problem
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Globalization;
using System.IO;
namespace spo2
{
public partial class Form1 : Form
{/*++++++++++++++++++++++++++++++++++++++++++++*
CSPO2Sensor
*+++++++++++++++++++++++++++++++++++++++++*/
[DllImport("kernel32.dll")]
public static extern bool Beep(int Frequenz, int Dauer);
byte[] buffer = new Byte[5] { 0, 0, 0, 0, 0 };
byte msbHR = 0; // Heartrate high Byte
byte lsbHR = 0; // Heartrate low Byte
byte SB = 0;
public int nHR = 0; // Heartrate
public int nSpO2 = 0; // Oxygensaturation
public Form1()
{
InitializeComponent();
}
public void Init_SerialPort()
{
Debug.WriteLine("In Init_SerialPort method.");
serialPort1.BaudRate = 9600;
serialPort1.DataBits = 8;
serialPort1.PortName = "COM1";
serialPort1.ReceivedBytesThreshold = 1;
serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
Debug.WriteLine("Out Init_SerialPort method.");
}
public void holeDaten()
{
try
{
Debug.WriteLine("In holeDaten");
serialPort1.Read(buffer, 0, 1);
if (buffer[0] == 0x01)
{
serialPort1.Read(buffer, 1, 4);
if ((buffer[1] & 0x81) == 0x81)
{
Analyse();
}
}
}
catch( Exception exception)
{
Debug.Write(exception.Message);
}
}
public void Analyse()
{
try
{
SB = buffer[1];
msbHR = buffer[(3 & 0x03)];
serialPort1.Read(buffer, 0, 5);
lsbHR = buffer[3 & 0x7f];
serialPort1.Read(buffer, 0, 5);
nSpO2 = buffer[3];
nHR = (msbHR * 128) + lsbHR;
/* if (nSpO2 == 127)
{
nSpO2 = 0;
String sSPO2 = "nSpO2 =127 ,bad data :" + nSpO2.ToString();
label_SPO2.Text = sSPO2;
}
if (nHR == 511)
{
String sHR = "nHR=511 ,bad data :" + nHR.ToString();
nHR = 0;
label_HR.Text = sHR;
//listView_HR.Items.Add("nHR=511 ,bad data :" + nHR);
}
if ((SB & 0x02) == 0x02)
{
// MessageBox.Show(" Green Perfusion " + SB);
//list.Items.Add("Green Perfusion :" + SB+" ,nSpO2: " + nSpO2);
//listBox2.Items.Add("Green Perfusion :" + SB +" ,nHR: "+ nHR);
}
if ((SB & 0x04) == 0x04)
{
// MessageBox.Show(" Red Perfusion " + SB);
//listBox1.Items.Add("Red Perfusion :" + SB + " ,nSpO2: " + nSpO2);
//listBox2.Items.Add("Red Perfusion :" + SB + " ,nHR: " + nHR);
}
if ((SB & 0x08) == 0x08)
{
// MessageBox.Show(" Sensor Alarm " + SB);
//listBox1.Items.Add("Sensor Alarm :" + SB + " ,nSpO2: " + nSpO2);
//listBox2.Items.Add("Sensor Alarm:" + SB + " ,nHR: " + nHR);
// Alarm();
}
if ((SB & 0x10) == 0x10)
{
//listBox1.Items.Add(nSpO2);
// MessageBox.Show(" Out of Track " + SB);
//listBox1.Items.Add("Out of Track :" + SB + " ,nSpO2: " + nSpO2);
//listBox2.Items.Add("Out of Track :" + SB + " ,nHR: " + nHR);
}
if ((SB & 0x20) == 0x20)
{
// MessageBox.Show(" Bad Pulse " + SB);
//listBox1.Items.Add("Bad Pulse:" + SB + " ,nSpO2: " + nSpO2);
//listBox2.Items.Add("Bad Pulse :" + SB + " ,nHR: " + nHR);
}
if ((SB & 0x40) == 0x40)
{
// MessageBox.Show(" Sensor disconnected " + SB);
//listBox1.Items.Add("Sensor disconnected :" + SB + " ,nSpO2: " + nSpO2);
//listBox2.Items.Add("Sensor disconnected :" + SB + " ,nHR: " + nHR);
}*/
Application.DoEvents();
Debug.WriteLine("out holeDaten");
}
catch( Exception exception)
{
Debug.WriteLine(exception.Message);
}
label_HR.Text = nHR.ToString();
label_SPO2.Text = nSpO2.ToString();
MessageBox.Show(" nspo2=" + nSpO2 + " nhr= " + nHR);
}
/* ++++++++++++++++ Daten von serieller Schnittstelle lesen ++++++++++++++++ */
public void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
Debug.WriteLine("IN serialport1_Dtareceived"); ;
try
{
holeDaten();
Debug.WriteLine("erste Byte suchen ");
}
catch( Exception exception)
{
Debug.WriteLine(exception.Message);
}
Debug.WriteLine("OUT serialport1_datareceived");
}
private void Alarm()
{
Beep(1500, 1300);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
Init_SerialPort();
serialPort1.Open();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
private void label_SPO2_Click(object sender, EventArgs e)
{
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Alarm();
}
}
}
Partager