Brad
Brad

Reputation: 1480

Reading from Com1 port using C#

I am attempting to write a program that can read inputs from a current scale(Mettler Toldeo pg802-S) I have connected via COM1. With the current code, I can list all of the COM's that are offered but am having issues getting it to list when is being pushed by the scale.

The scale is located on COM1, so I am trying to get the data the scale is pushing. Using WinWedge, I can get the results to push using Com1, 2400 baud rate, even parity, seven data bits, 1 stop bits, no flow control, 1024 input buffer size, & 512 output buffer size. How can I get the data this way?

using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;


namespace SerialPortWrite{
    class GetSerialData{
        static void Main(string[] args){

            Form1 form1 = new Form1();
            form1.ShowDialog();

             Console.WriteLine("Program Started For COM1...");
            // List all COMS
             ListComPorts();

            // Read this COM.
            var reader = new ArduinoSerialReader("COM1");

            // Read COM input.
               Console.ReadLine();
        }

        static void ListComPorts(){
            // Get a list of port names. 
            string[] ports = SerialPort.GetPortNames();

            Console.WriteLine("The following serial ports were found:");

            // Get all COM ports to list out. 
            foreach (string port in ports){
                Console.WriteLine(port);
            }
        }
    }

    // Get input.
    public class ArduinoSerialReader : IDisposable{
        private SerialPort _serialPort;

        public ArduinoSerialReader(string portName){
            _serialPort = new SerialPort(portName);
            _serialPort.Open();
            _serialPort.DataReceived += serialPort_DataReceived;
        }

        void serialPort_DataReceived(object s, SerialDataReceivedEventArgs e){
            Console.WriteLine(_serialPort.ReadLine());
        }

        public void Dispose(){
            if (_serialPort != null){
                _serialPort.Dispose();
            }
        }
    }
}

Upvotes: 0

Views: 978

Answers (2)

henoc salinas
henoc salinas

Reputation: 1054

I developed a library for connect a scale to c#, the library have a component, and its configurable for all scales with continuos communication, with command needed or witout command for send to scale, actually a try this with arduino scale emulator and an indicator of JUSTA LS7510A, the code of library its on GITHUB.

enter image description here

Upvotes: 1

user8190410
user8190410

Reputation: 1314

To receive the data from serial port in c#, you can use SerialPort class. You can use a separate thread to continuously read data from port and process it. See example code below:

void readFromSerial()
{
    SerialPort sp;
    try
    {
        string[] ports = SerialPort.GetPortNames();    // get all the available port names in string array
        if (ports.Length > 0)
        {
            // create SerialPort object 
            sp = new SerialPort(ports[0], 2400, Parity.Even, 7, StopBits.One);
            sp.Open();

            string str = "";
            // infinite loop to read data byte by byte. you can also use for loop to read constant amount of bytes
            while (true)
            {
                // sp.ReadByte is blocking read, it will wait until a byte is available for reading
                byte data = (byte)sp.ReadByte();
                Console.Write(" "+data);
                str += data + " ";
            }
        }
    }
    catch(Exception ex)
    {}
    finally
    {
        if (sp != null && sp.IsOpen())
            sp.Close();
    }
}

Then create a separate thread like this

Thread t = new Thread(new ThreadStart(readFromSerial));
t.Start();

Upvotes: 1

Related Questions