c#想用串口传送文件,并把文件切割以字符串数组显示,但失败了,求助

如题 以下是代码,啥也不会,也是从网上找的代码,自己加了一点。我用x替换原文件的“\r\nNNNN”,并拆分成为字符串数组,但是串口显示时,s还是没有变,还是\r\nNNNN,求高人指点。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace chuankou
{
public partial class Form1 : Form
{
string s = "";
string ss = "";
string[] s_1 = new string[100];

    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
    public void serialPort_DataReceived(object sender,SerialDataReceivedEventArgs e)
    {

        int count = myserialPort.BytesToRead;
        byte[] data = new byte[count];
        myserialPort.Read(data, 0, count);
         foreach (byte item in data)
         {
             s += Convert.ToChar(item);

         }
            s = s.Replace("\r\nNNNN", "x");

        string[] s_1 = ss.Split(new char[] { 'x'});

    }

    private void button1_Click(object sender, EventArgs e)
    {


        if (!myserialPort.IsOpen)
        {
            myserialPort.Open();
        }
        myserialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (myserialPort.IsOpen)
           myserialPort.Close();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        //timer1.Enabled = true;
       // timer1.Interval = 1000;
        int i;
        for (i = 0; i < s_1.Length; i++)
        {
            if (this.InvokeRequired)
            {


                this.Invoke(new MethodInvoker(delegate { this.textBox1.Text = s; }));
            }
            else

            {
                this.textBox1.Text = s;
            }

        }





    }

    private void timer1_Tick(object sender, EventArgs e)
    {



    }
}

}

2个回答

s= s.Replace("\r\nNNNN", "x");
在这个前面调试下,看看是不是有"\r\nNNNN",特别注意转义,你要是不是换行,而是\r\n字面的值,需要双斜杠

shmilyshushu
shmilyshushu 哦谢谢大佬,是转义的问题 我变成@“\r\nNNNN”就对了
一年多之前 回复

byte[] byteArray = new byte[24];
byteArray = System.Text.Encoding.Default.GetBytes("111\r\nNNNN123");
string str = System.Text.Encoding.Default.GetString(byteArray);
Debug.Log(str);
str=str.Replace("\r\nNNNN", "x");
Debug.Log(str);
string[] s_1 = str.Split('x');
Debug.Log(s_1.Length);
图片说明

shmilyshushu
shmilyshushu 能加你微信吗,实在是搞不懂了
一年多之前 回复
shmilyshushu
shmilyshushu 大佬,我还是看不懂,为什么我把从缓存区收到的文件存到字符串s后,还是不能对s进行操作呢。按理说我都把“\r\nNNNN”替换为x了啊?
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问