c# - Indexer created to look at array doesn't see changes made to array -
i new c#, , i'm creating serial port class board have designed. in class contains methods open/close serial port connected board. should read messages board , write messages ui board (i using forms application input , display values).
i read internal input buffer , place bytes own software buffer, when message complete, prompt form analyse message...
for have created indexer point array (from form) , take bytes desires.
uint[] serialportreceivebuffer = new uint[3]; public delegate void del(); del promptformaction = form1.msgreceived; public void serialport1_datareceived(object sender, serialdatareceivedeventargs e) { (int = 0; <= 2; i++) { serialportreceivebuffer[i] = (uint)serialport1.readbyte(); } promptformaction(); } public uint this[uint i] { { return serialportreceivebuffer[i]; } }
this code within pcbserialport class, , code related in form1 class follows:
public static void msgreceived() { form1 _frm = new form1(); _frm.analyzeincomingmessage(); } public void analyzeincomingmessage() { if (pcb[0] == 63) { setboarddesignator(pcb[1], pcb[2]); } }
my problem when use indexer access serialportreceivebuffer
, doesn't see changes made when placing received bytes same array. example, when receive string of own protocol --> "?10" buffer filled [63][49][48]
though when try access buffer using indexer [0][0][0]
please can help? also, i'm aware there few other things have done better if have general tips great. in language may understand. getting head around many of c# aspects, have been doing embedded software past year wouldn't consider self competent programmer.
thank
from code i'm not quite sure pcb
object you're working in form 1 receives data. might you're working 2 different instances, you're creating new instance of form1
whenever data comes in!
(edit: comment question clear problem. follow these instructions closed want).
i suggest redesign code pass received message event existing form instance instead of how now. problem might run data think overridden next message coming in, das datareceived
event asynchronous.
i'd declare event form instance can subscribe to, passing data analyzed event:
public class messagereceivedeventargs: eventargs { public messagereceivedeventargs(byte[] data) : base() { data = data; } public byte[] data { get; private set; } } public event eventhandler<messagereceivedeventargs> messagereceived;
then, i'd change datareceived
event follows:
public void serialport1_datareceived(object sender, serialdatareceivedeventargs e) { (int = 0; <= 2; i++) { serialportreceivebuffer[i] = (uint)serialport1.readbyte(); } byte[] datacopy = new byte[serialportreceivebuffer.length]; array.copy(serialportreceivebuffer, datacopy, datacopy.length); promptformaction(datacopy); } private void promptforaction(byte[] data) { if (messagereceived != null) messagereceived(this, new messagereceivedeventargs(data)); }
also i'd keep serialportreceivebuffer
totally private class, said, may run synchronization issues if don't. that'y why copy array before passing event.
this change allows subscriber register notifications whenever realize new data came in.
to use this, form1
should (roughly);
public class form1 { pcbserialport pcb; // name of class don't know code public form1() { pcb = new pcbserialport(); pcb.messagereceived += messagereceived; } private void messagereceived(object sender, pcbserialport.messagereceivedeventargs e) { analyzeincomingmessage(e.data); } private void analyzeincomingmessage(byte[] data) { if (data[0] == 63) { setboarddesignator(data[1], data[2]); } } }
another piece of advice on how handle serial data: need decide whether read serial port in loop or whether rely on datareceived
event. putting loop event not idea, event may called upon arriving data again while you're waiting.
what need create buffer takes information serial port that's available. if don't have enough data, don't wait it. instead add buffer whenever datareceived
called , handle message when enough data present.
Comments
Post a Comment