使用C#从Com1端口读取

时间:2018-09-25 16:22:05

标签: c# serial-port

我正在尝试编写一个程序,该程序可以读取通过COM1连接的当前秤(梅特勒-托利多pg802-S)的输入。使用当前代码,我可以列出所有提供的COM,但是在被秤推动时很难列出它。

秤位于COM1上,所以我正在尝试获取秤推入的数据。使用WinWedge ,我可以使用Com1、2400波特率,偶校验,七个数据位,1个停止位,无流控制,1024个输入缓冲区大小和512个输出缓冲区大小来推送结果。如何以这种方式获取数据?

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();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

要使用c#从串行端口接收数据,可以使用SerialPort类。您可以使用单独的线程连续从端口读取数据并进行处理。请参见下面的示例代码:

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();
    }
}

然后创建一个像这样的单独线程

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

答案 1 :(得分:0)

我开发了一个用于将秤连接到c#的库,该库具有一个组件,并且它可以通过连续操作通信,需要的命令或witout命令发送到秤来配置为所有秤,实际上可以使用arduino秤模拟器和JUSTA LS7510A的指示器,它的代码在GITHUB上。

enter image description here