C#BILANCIAI体重秤 [英] C# BILANCIAI Scale weight

查看:155
本文介绍了C#BILANCIAI体重秤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我需要一些认真的帮助.我需要与电子秤的主机进行通信. BILANCIAI(D400)

问题是我不知道我需要传递什么字符串才能接收数据.

我有波特率,奇偶校验,停止位等,这样就可以了,但是一旦我打开端口并发送写入命令​​",它就什么也没做.

我已经尝试过使用回送连接,并且可以正常工作.我键入的任何内容都会发回到文本框中.


请帮助!

代码如下:

Hi Guys.

I need some serious help with this. I need to communicate with a head unit of a weigh bridge. BILANCIAI(D400)

The thing is that I do not know what the string is that I need to pass in order to receive data.

I have the Baud rate, Parity, Stop Bits etc so thats fine but as soon as I open the port and send the write the "command" it doesn''t do anything.

I have tried this with a loopback connection and it works. Whatever I type in is posted back in a textbox.


PLEASE HELP!

Code as follows:

using System.IO.Ports;
using System.IO;

namespace PortListening
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        StreamReader read;

        string rxString;

        const char ENQ = (char)0x05;

        private void Buttonstart_Click(object sender, EventArgs e)
        {
            const char ENQ = (char)0x05; // Try Different Commands

            byte[] buff = new byte[1];
            buff[0] = 0x05;

            try
            {
                serialPort1.PortName = comboBox1.SelectedItem.ToString();

                serialPort1.BaudRate = Convert.ToInt32(comboBox2.SelectedItem); //Change 4800 , 9600 , 19200 , 38400 , 115400
                serialPort1.Parity = (Parity)comboBox3.SelectedIndex; // unknown
                serialPort1.Handshake = (Handshake)comboBox4.SelectedIndex; // unknown
                serialPort1.DataBits = 8;
                serialPort1.StopBits = StopBits.One;
                serialPort1.RtsEnable = true;
              

                if (!serialPort1.IsOpen)
                {
                    serialPort1.Open();
                    MessageBox.Show("Port: " + comboBox1.SelectedItem + " is now open.");
                    Buttonstart.Enabled = false;
                    buttonStop.Enabled = true;
                    textBox1.ReadOnly = false;

                    serialPort1.Write(ENQ.ToString());


                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Port Can't be opened: " + ex.Message);
            }
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            if(serialPort1.IsOpen)
            {
                serialPort1.Close();
                MessageBox.Show("Port: " + comboBox1.SelectedItem + " is now closed.");
                Buttonstart.Enabled = true;
                buttonStop.Enabled = false;
                textBox1.ReadOnly = true;
            }
        }


        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            rxString = serialPort1.ReadExisting();
            this.Invoke(new EventHandler(DisplayText));
        }

        private void DisplayText(object sender, EventArgs e)
        {
            textBox1.AppendText(rxString);
        }

推荐答案

您应该与创建它的人交谈-他们应该提供技术支持,并且比我们更了解他们的产品.如果他们没有,请找另一家供应商并要求您退款!
You should talk to the people who created it - they should provide technical support and will know more about their product than we will. If they don''t, then find another supplier and demand your money back!


尝试这个

try this

public string GetWeight(string PortName, string PortSetting, string ReadScaleCommand)
{
    Parity odd;
    StopBits one;
    string[] strArray = new string[5];
    strArray = PortSetting.Split(new char[0]);
    int baudRate = Convert.ToInt32(strArray[0]);
    switch (strArray[1])
    {
        case "O":
        case "o":
            odd = Parity.Odd;
            break;

        case "E":
        case "e":
            odd = Parity.Even;
            break;

        case "N":
        case "n":
            odd = Parity.None;
            break;

        default:
            odd = Parity.Even;
            break;
    }
    int dataBits = Convert.ToInt32(strArray[2]);
    switch (strArray[3])
    {
        case "1":
            one = StopBits.One;
            break;

        case "1.5":
        case "1,5":
            one = StopBits.OnePointFive;
            break;

        case "2":
            one = StopBits.Two;
            break;

        default:
            one = StopBits.One;
            break;
    }
    SerialPort port = new SerialPort(PortName, baudRate, odd, dataBits, one);
    port.NewLine = "\r";
    port.WriteTimeout = 500;
    port.ReadTimeout = 500;
    port.Open();
    string str = "";
    while (str == "")
    {
        port.DiscardInBuffer();
        port.DiscardOutBuffer();
        port.WriteLine(ReadScaleCommand);
        try
        {
            str = port.ReadLine();
            continue;
        }
        catch (TimeoutException)
        {
            str = "";
            continue;
        }
    }
    port.Close();
    return str;
}


这篇关于C#BILANCIAI体重秤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆