我使用C#微軟的Visual Studio專業2013年 我寫:爲什麼writeline不停在/ r?
[0] 0 '\0' char
[1] 1 '' char
[2] 0 '\0' char
[3] 0 '\0' char
[4] 0 '\0' char
[5] 8 '\b' char
[6] 1 '' char
[7] 3 '' char
[8] 0 '\0' char
[9] 0 '\0' char
[10] 65535 '.' char
[11] 65534 '' char
[12] 0 '\0' char
[13] 0 '\0' char
[14] 13 '\r' char
但我的最終計劃只看到它沒有看到整個消息。我正在寫一個名爲Modbus Master的程序。這發送我的代碼數據請求,我的代碼回覆。我收到來自Modbus Master的完整消息,但它沒有從我的代碼中得到完整的按摩。
這裏是我的代碼:
void Listener_function()
{
IPHostEntry host;
string localIP = "?";
host = Dns.GetHostEntry(Dns.GetHostName());
TcpListener listener = null;
try
{
listener = new TcpListener(IPAddress.Parse(Moxa_IPtxt.Text), Convert.ToInt16(modemPorttxt.Text));
listener.Start();
// ErrorBox.Text = " EchoServer started ... /n";
MessageBox.Show(" waiting for incoming client connections.../n /r");
TcpClient client = listener.AcceptTcpClient();
MessageBox.Show(" Accepted new client coneection ...");
StreamReader reader = new StreamReader(client.GetStream());
StreamWriter writer = new StreamWriter(client.GetStream());
while (true)
{
string s = string.Empty;
char[] temp = new char[12];
int s1 = reader.Read(temp, 0, 12);
int Registers_number = (((int)temp[10] << 8) + (int)temp[11]);
int Message_Size = (Registers_number * 2) + 8;
char[] Message3 = new char[Message_Size];
TCPProcessing(temp, Message3);
if (writeData == true) {
writer.Write(Message3);
writer.Flush();
}
/* reader.Close();
writer.Close();
client.Close(); */
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
//ErrorBox.Text = e.ToString();
}
finally
{
if (listener != null)
{
listener.Stop();
}
}
}
消息3的大小是一個全球性的陣列30
private void Form1_Load(object sender, EventArgs e)
{
tag = new List<Tag>();
Tagcall = new List<tagcall>();
Message3 = new char[30]; }
爲什麼不的WriteLine在/ R停止?
爲什麼要停在/ r? –
因爲/ r是字符串的結尾......對嗎?如果這是不對的,比我怎麼告訴它停止?直到我收到來自Modbus主站的請求後,才知道返回消息的大小。 – Brandon
嘗試發送esc –