标题:请教(Money酬谢)---C#读取电子称串口数据,及时显示在TextBox控件
只看楼主
kimijiang
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-9-14
结帖率:0
已结贴  问题点数:20 回复次数:2 
请教(Money酬谢)---C#读取电子称串口数据,及时显示在TextBox控件
公司要求将电子称串口数据与电脑串口相连接,实现数据的即时通讯,其主要用来称产品,然后计算出相应个数。以下是我写的代码,基本都是参照网上高人写的。
但是就是不能实现这个自动读取电脑称仪表数据的功能,以下是程序核心代码。
哪位高人能指点一下,本人感激万分。如果有朋友愿意协助下,定以适当Money作为酬谢。QQ:526284639
 private void Form2_Load(object sender, EventArgs e)
        {
            InitPort();
        }
        private void InitPort()
        {      
            com.BaudRate = 9600;
            com.PortName = "COM4";
            com.DataBits = 8;
            com.Parity = Parity.None;
            com.StopBits = StopBits.One;
            com.WriteTimeout = 3000;
            com.ReadTimeout = 3000;
            com.ReceivedBytesThreshold = 1;
            com.DtrEnable = true;
            com.RtsEnable = true;      
            com.DataReceived += new SerialDataReceivedEventHandler(com_DataReceived);
            if (!com.IsOpen)
            {
                com.Open();
            }
            else
            {
                MessageBox.Show("Please open relative port");
                return;
            }
        }

        private void com_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            try
            {
                   if (!com.IsOpen)
            {
                com.Open();
            }
            com.ReadTimeout = 500;
            Thread.Sleep(500);
            string res = "";
            byte[] buffer = new byte[com.BytesToRead];
           // MessageBox.Show(buffer.Length.ToString());
            com.Read(buffer, 0, buffer.Length);
            res = System.Text.Encoding.ASCII.GetString(buffer);
            if (res == "")
            {
                MessageBox.Show("串口读取数据为空,参数设置是否正确!");           
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
        }
   private void sendDataRequest()
        {
            byte[] arrByte = new byte[2];
            //发送命令 1BH+70H  与电子称通讯,读取电子称数据,16进制  
            arrByte[0] = Convert.ToByte(0x1B);
            arrByte[1] = 0x70;
            com.Write(arrByte, 0, 2);
            byte[] buffer = new byte[com.BytesToRead];         
        }
搜索更多相关主题的帖子: private 朋友 电子 产品 
2016-09-15 10:43
向洪林
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:重庆市奉节县
等 级:版主
威 望:9
帖 子:118
专家分:405
注 册:2015-3-10
得分:20 
private void com_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            try
            {
                if (!com.IsOpen)
                {
                com.Open();
                }
                com.ReadTimeout = 500;
                Thread.Sleep(500);
                string res = "";
                byte[] buffer = new byte[com.BytesToRead];
               // MessageBox.Show(buffer.Length.ToString());
                com.Read(buffer, 0, buffer.Length);
                res = System.Text.Encoding.ASCII.GetString(buffer);
                if (res == "")
                {
                    MessageBox.Show("串口读取数据为空,参数设置是否正确!");           
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
       }
不知道你这个代码try到哪里?我给你加了个括号,注意规范

新手上路,望大家多多照顾,谢谢!
2016-09-17 21:06
pengzhanggui
Rank: 5Rank: 5
等 级:职业侠客
威 望:8
帖 子:161
专家分:344
注 册:2015-7-20
得分:0 
我有VB的读取方式

来找我试试看
2016-10-19 08:14



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-468819-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.184427 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved