Dave Spencer

Dave Spencer

  • NA
  • 3
  • 628

serial port and C#, both block and byte interactions

Feb 13 2016 12:15 PM

 First, I apologize if this has been asked before. I saw no reference to something quite like this, though. I am also very new to C# programming. I have a C# form application that has to interface to a PIC-based micro through a USB-to-serial interface adapter.

The program needs to send small (one to 5-byte) commands to the PIC and, depending on the command, get a few bytes of response. With these small command responses, the data always ends with a "$" character (no newline character). These commands are used to read the current status and values of the PIC and to set runtime operational values for the PIC. The program also needs to download a large block of data after the PIC completes a local operation. The data is read by the program and displayed as a graph.
 
After seeing several similar articles in this forum, I tried using the srialPort1_DataReceived() event. The event checks a variable that is set prior to calling the write command. Depending on the variable, either serialPort1.Readline() (for the block transfer, which always end with a 'n') or serialPort1.ReadTo("$") (for small interactive commands) is used. In either case, I invoke a delegate to handle the data collected in response. I am very new to the use of delegates, though, so I may be doing this completely wrong. At any rate, I'm getting "Program stopped working" errors.
 
Can someone give me some insight into if it sounds like I'm doing the right approach and any pointers in the right direction? Thanks.