Note that there are some explanatory texts on larger screens.

plurals
  1. POSerial port communication between threads
    primarykey
    data
    text
    <p>After much research I am still stumped. I have a serial port object which is reading data continuously. What I am able to do it generate the dataReceived event, communicate with the port, and output the received values to the debug window. So, I'm pretty sure it's all working physically. The problem is when I try to pass the serial port output to my original thread I get an error. It says I can't have thread cross talk (or something to that effect). I've been trying to use a backgroundWorker but I'm not sure that is the solution I want plus with my novice skills it's a little over my head. And I tried to use invoke but the method doesn't seem to be available. (I might be calling from the wrong object?) Anyway section is below. </p> <pre><code>namespace Photometer { class csRadiometerILT1700 { //manufacturer specs for baud rate, databits, and stop bits static string portName="COM1"; static int baudRate = 1200; static int dataBits = 8; //instantialize a serial port object for the Radiometer private SerialPort RadiometerSerial = new SerialPort(portName, baudRate, Parity.None, dataBits, StopBits.One); //constructor //public csRadiometerILT1700(Form ParentForm, Chart outputChart) public csRadiometerILT1700() { //two handshaking properties of the ILT1700. Handshaking is enabled and //http://stackoverflow.com/questions/6277619/problem-reading-serial-port-c-net-2-0-to-get-weighing-machine-output RadiometerSerial.Handshake= Handshake.RequestToSend; RadiometerSerial.DtrEnable = true; RadiometerSerial.DataReceived += new SerialDataReceivedEventHandler(RadiometerSerial_DataReceived); } public void openPort() { if (!RadiometerSerial.IsOpen) { RadiometerSerial.Open(); } } public void closePort() { RadiometerSerial.Close(); } string RadiometerVoltageReadingString; int RadiometerVoltageReadingInt; private void RadiometerSerial_DataReceived(object sender, SerialDataReceivedEventArgs e) { //It's here that this.invoke()... cannot be called. RadiometerVoltageReadingString= (RadiometerSerial.ReadExisting().ToString()); //y-value Debug.Print(RadiometerVoltageReadingString.ToString()); makeRadioReadingDouble(RadiometerVoltageReadingString); } private void makeRadioReadingDouble(string inputVoltageString) { List&lt;double&gt; outputVoltageDouble=new List&lt;double&gt;(2); if (!(inputVoltageString == "\r\n" || inputVoltageString == "")) { string[] voltageValAndExpo = inputVoltageString.Split(new string[] { "e", "\r\n" }, StringSplitOptions.RemoveEmptyEntries); for (int inCounter = 0; inCounter &lt; voltageValAndExpo.Count(); inCounter=inCounter+2) { double voltageVal = Convert.ToDouble(voltageValAndExpo[inCounter]); double voltageExpo = Convert.ToDouble(voltageValAndExpo[inCounter + 1]); outputVoltageDouble.Add(Math.Pow(voltageVal, voltageExpo)); } } } } } </code></pre> <p>This is all called when I form loads with the code</p> <pre><code> csRadiometerILT1700 Radiometer; ... Radiometer = new csRadiometerILT1700(); Radiometer.openPort(); </code></pre> <p>Any insight is appreciated.</p> <p>EDIT: I altered my csRadiometerILT1700 constructor to </p> <pre><code> public csRadiometerILT1700(Form inputForm) { //inputForm.Invoke( //two handshaking properties of the ILT1700. Handshaking is enabled and //http://stackoverflow.com/questions/6277619/problem-reading-serial-port-c-net-2-0-to-get-weighing-machine-output RadiometerSerial.Handshake= Handshake.RequestToSend; RadiometerSerial.DtrEnable = true; RadiometerSerial.DataReceived += new SerialDataReceivedEventHandler(RadiometerSerial_DataReceived); inputForm.Invoke(DataReceived); } </code></pre> <p>and declare</p> <pre><code>public event Delegate DataReceived; </code></pre> <p>in the csRadiometerILT1700 class. But this gives me the error of "Datareceived must be of a delegate type." How do I resolve this now? Am I on the right track?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload