C# scanning COM ports for specific input

Internet of Things IoT Frameworks 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ I'm making a program that can control an Arduino board through C#. Here is the steps the program needs to take: Scan COM Ports Receive inputs from the devices When an input has a specific phrase such as "connectAlready", Close all ports and create a new one on the port that received the phrase. Now that the program knows what COM port the Arduino is on, it can carry on its tasks and send it commands through SerialPorts. Below is the code i have so far for the connect() method. public void connect() { string[] ports = SerialPort.GetPortNames(); SerialPort[] serialport = new SerialPort[ports.Length]; foreach(string p in ports) { int i = Array.IndexOf(ports, p); serialport[i].PortName = p; serialport[i].BaudRate = 9600; serialport[i].Open(); //Scan inputs for "connectAlready" } Not much i know, and i keep getting a "System.NullReferenceException" at serialport[i].PortName = p; How can I make this program work? Thank you for all the help

Posted on 16 Aug 2022, this text provides information on IoT Frameworks related to Internet of Things. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ Firstly, you need to declare new serial port object for each serial port which you create: string[] ports = SerialPort.GetPortNames(); SerialPort[] serialport = new SerialPort[ports.Length]; foreach(string p in ports) { int i = Array.IndexOf(ports, p); serialport[i] = new SerialPort(); //note this line, otherwise you have no serial port declared, only array reference which can contains real SerialPort object serialport[i].PortName = p; serialport[i].BaudRate = 9600; serialport[i].Open(); //Scan inputs for "connectAlready" } And then, you need at least four things to get and to handle your data: Event handler for DataReceived for your serial port serialport[i].DataReceived += serialPort_DataReceived; //This is to add event handler delegate when data is received by the port private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { //This is to handle the data when it comes //Do something on data received } Get the data from the underlying stream of the port. private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { SerialPort serialPort1 = sender as SerialPort; byte[] data = new byte[serialPort1.BytesToRead]; //this is to provide the data buffer Stream portStream = serialPort1.BaseStream; portStream.Read(data, 0, data.Length); //You get your data from serial port as byte[] //Do something on your data } Encoding.UTF8.GetString to convert the input data from byte[] to ASCII characters (Edit: consider of changing/skip this step and step 2 if the data received is not byte[] but ASCII) private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { SerialPort serialPort1 = sender as SerialPort; byte[] data = new byte[serialPort1.BytesToRead]; //this is to provide the data buffer Stream portStream = serialPort1.BaseStream; portStream.Read(data, 0, data.Length); //You get your data from serial port as byte[] string dataString = Encoding.UTF8.GetString(data); //here is your data in string //Do something on your data } Check if the data string contains the data that you want bool hasData = dataString.Contains("connectAlready"); //this is to check if your data has this, if it doesn't do something And as a last note, beware that the data from your serial port doesn't come together (like "conne" and then "ctAlready"). In such case, you need to copy your received data in a global buffer first then have additional checking (use Buffer.BlockCopy) byte[] globalBuffer = new byte[4000]; //large buffer, put globally //In your data received, use Buffer.BlockCopy to copy data to your globalBuffer //Beware the index if (globalBuffer.Length >= 14){ //less than this length, then the data is incomplete //Do the checking if length is at least 14 }

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Internet of Things Links