我正在尝试让服务器能够接收客户端字节并将其用作整数,我该怎么做?这是我试图修复的代码〜唯一的错误就是它无法将字节处理为int ...请帮帮我
try
{
byte[] received = new byte[1024];
mStream.Read(received, 0, received.Length);
updateUI("Received: " + Encoding.ASCII.GetString(received));
int Cir;
Cir = received;
switch (Cir)
{
case 1 :
serialPort1.PortName = "COM5";
serialPort1.BaudRate = 9600;
serialPort1.DataBits = 8;
serialPort1.Parity = System.IO.Ports.Parity.None;
serialPort1.StopBits = System.IO.Ports.StopBits.One;
serialPort1.Open();
serialPort1.Write("a");
serialPort1.Close();
break;
case 2:
serialPort1.PortName = "COM5";
serialPort1.BaudRate = 9600;
serialPort1.DataBits = 8;
serialPort1.Parity = System.IO.Ports.Parity.None;
serialPort1.StopBits = System.IO.Ports.StopBits.One;
serialPort1.Open();
serialPort1.Write("a");
serialPort1.Close();
break;
}
答案 0 :(得分:0)
//Try converting the byte value and setting your int to it that way
int cir = Convert.ToInt32(recieved);
答案 1 :(得分:0)
//Try writing the value in a console to see what you have
Console.WriteLine(received);
Console.Readline();
或者,如果您正在使用像Visual Studios这样的IDE,那么您可以在执行期间停止该功能并检查值。