从称重秤CAS CL5200接收数据

时间:2015-04-04 14:13:16

标签: c# winforms serial-port

需要CAS CL5200规模的帮助。 我已经看过关于这个问题的所有帖子,但没有一个是关于CAS CL5200与USB电缆连接的缩放连接创建一个虚拟COM端口(COM41)

这是我遵循的步骤 -

  1. 为了测试通信,我安装了CL-Works(为某些功能扩展软件) - >工作
  2. 安装了免费设备监控工作室以查看所有连接参数。
  3. 用超级终端测试 - >
  4. 免费设备监控Studio报告“W”写入com端口但没有任何返回。
  5. 这里的代码是我在其他相关文章中找到的,用byte []和字符串测试它,无论是代码写在COM41“W”上,但没有什么可读的。
  6. 代码:

    public partial class Form1 : Form
    {
        private const int BaudRate = 19200; // BaudRate Constant
    
        private SerialPort _serialPort;
    
        private void button1_Click_1(object sender, EventArgs e)
        {
            if (_serialPort != null && _serialPort.IsOpen)
                _serialPort.Close();
            if (_serialPort != null)
                _serialPort.Dispose();
    
            _serialPort = new SerialPort("COM41", BaudRate, Parity.None, 8, StopBits.One); // Creates new SerialPort using the name selected in the combobox
            _serialPort.Handshake = Handshake.None;
            // _serialPort.DataReceived += SerialPortOnDataReceived; // this event happens everytime when new data is received by the ComPort
            _serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);
            _serialPort.RtsEnable = true;
            _serialPort.DtrEnable = true;
            _serialPort.Open();
    
            textBox1.Text = "Listening on " + _serialPort.PortName + "...\r\n";
    
           // byte[] towrite = GetBytes("W"); // get the weight from scale wiyh "W" as byte[]
           // _serialPort.Write(towrite,0,1);
            _serialPort.WriteLine("W");
        }
    
        private delegate void Closure();
        void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (InvokeRequired) // makes sure the function is invoked to work properly in the UI-Thread
                BeginInvoke(new Closure(() => { _serialPort_DataReceived(sender, e); })); //<-- Function invokes itself
            else
                while (_serialPort.BytesToRead > 0) //<-- repeats until the In-Buffer is empty
                {
                    //textBox1.Text += string.Format("{0:X2} ", _serialPort.ReadByte());
                    textBox1.Text += _serialPort.ReadExisting();
                    //<-- bytewise adds inbuffer to textbox
                }
        }
    
        private byte[] GetBytes(string str)
        {
            byte[] bytes = new byte[str.Length * sizeof(char)];
            Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
            return bytes;
        }
    }
    

0 个答案:

没有答案