如何获得角色->获取字节 [英] How to get Characters -> Get Bytes

查看:109
本文介绍了如何获得角色->获取字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与使用数组通过串行端口发送相比,将字符转换为字符串以获取字节时遇到麻烦. (我做了我错误地遗漏的解决方案1中建议的更改).问题在于,运行回送测试时,用于串行端口的命令可完美转换,但无法与串行设备通信.该命令协议是正确的,因为当我获取输出并将其插入超级终端时,该命令适用于该设备.任何有类似经验或问题的人都将不胜感激.


Having trouble converting the characters into a string to get the bytes than use an array to send through serial port. (Ive made the changes suggested in solution 1 that I mistakenly missed). The issue is that the commands for the serial port translates perfectly when running a loopback test but does not communicate with the serial device. The command protocol is correct because when I take the output and plug it in hyperterminal, the command works with the device. Anybody with similar experience or issues, Ill appreciate the help.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;

namespace SimpleSerial
{
    
    public partial class Form1 : Form
    {
        // Add this variable 
        string RxString;
              
        
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM3";
            serialPort1.BaudRate = 9600;
            serialPort1.Parity = Parity.None;
            serialPort1.DataBits = 8;
            serialPort1.StopBits = StopBits.One;
            serialPort1.Handshake = Handshake.None;
                
            serialPort1.Open();
            if (serialPort1.IsOpen)
            {
                serialPort1.WriteTimeout = 500;
                buttonStart.Enabled = false;
                buttonStop.Enabled = true;
                textBox1.ReadOnly = false;
            }
        }
        const char STX = '\u0002';
        const char ETX = '\u0003';
        readonly string pull_shelf_104 = string.Format("{0}01P00104##{1}" , STX, ETX);

        private byte[] WrapString(string pull_shelf_104)
        {
            int length = pull_shelf_104.Length;
            byte[] send104 = new byte[length];
            Array.Copy(System.Text.Encoding.ASCII.GetBytes(pull_shelf_104), 0, send104, 0, length);
            return send104;
        }
        
        private void linkLabel_HC1_100_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                byte[] data = WrapString(pull_shelf_104);
                serialPort1.Write(data,0,12);
            }
        }
        private void buttonStop_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
                buttonStart.Enabled = true;
                buttonStop.Enabled = false;
                textBox1.ReadOnly = true;
            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (serialPort1.IsOpen) serialPort1.Close();
        }

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

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

推荐答案

您没有解释问题所在.

基本上,您可以正确地从字符中获取ASCII.
如果您从不更改pull_shelf_104,则最好将其定义更改为
You did not explain what the problem is.

Basically, you correctly get ASCII from characters.
If you never change pull_shelf_104, it would be better to change its definition to
readonly string pull_shelf_104 = string.Format("{0}01P00104##{1}", STX, ETX);



我可以在对WrapString的调用中看到语法错误,应该为



I can see the syntax error in the call to WrapString, should be

byte data = WrapString(pull_shelf_104);


您可能还有其他问题;我只是解释一下我注意到的那些东西.

再次提醒您,在许多情况下,您需要在单独的线程中执行所有通信.在UI中进行一些通信,单击处理程序可能不太好.

—SA


You may have some other problems; I just explain on those I noticed.

Again, I want to remind you that, in many cases, you would need to execute all communications in separate thread(s). It might be not good that you do some communication in UI, some click handler.

—SA


这篇关于如何获得角色->获取字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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