2011-06-02 55 views
1

這是交易。我找到了一個源代碼並稍微改了一下,所以我可以從com6上的接收器中檢索數據。我收到的數據是二進制的。我想要的是將它轉換爲一個字符串,所以我可以剪切部分字符串並單獨解碼。我怎樣才能做到這一點? 源代碼位於下方。C#二進制數據轉換爲字符串

using System; 
using System.IO.Ports; 
using System.Threading; 

public class PortChat 
{ 
    static bool _continue; 
    static SerialPort _serialPort; 

    public static void Main() 
    { 
     string name; 
     string message; 
     StringComparer stringComparer = StringComparer.OrdinalIgnoreCase; 
     Thread readThread = new Thread(Read); 

     // Create a new SerialPort object with default settings. 
     _serialPort = new SerialPort(); 

     // Allow the user to set the appropriate properties. 
     _serialPort.PortName = SetPortName(_serialPort.PortName); 
     _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate); 
     _serialPort.Parity = SetPortParity(_serialPort.Parity); 
     _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits); 
     _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits); 
     _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake); 

     // Set the read/write timeouts 
     _serialPort.ReadTimeout = 1000; 
     _serialPort.WriteTimeout = 1000; 

     _serialPort.Open(); 
     _continue = true; 
     readThread.Start(); 

     Console.Write("Name: "); 
     name = Console.ReadLine(); 

     Console.WriteLine("Type QUIT to exit"); 

     while (_continue) 
     { 
      message = Console.ReadLine(); 

      if (stringComparer.Equals("quit", message)) 
      { 
       _continue = false; 
      } 
      else 
      { 
       _serialPort.WriteLine(
        String.Format("<{0}>: {1}", name, message)); 
      } 
     } 

     readThread.Join(); 
     _serialPort.Close(); 
    } 

    public static void Read() 
    { 
     while (_continue) 
     { 

      try 
      {    


       string message = _serialPort.ReadLine(); 
       Console.WriteLine(message); 
      } 
      catch (TimeoutException) { } 
     } 
    } 

    public static string SetPortName(string defaultPortName) 
    { 
     string portName; 

      portName = "COM6"; 

     return portName; 
    } 

    public static int SetPortBaudRate(int defaultPortBaudRate) 
    { 
     string baudRate; 


     baudRate = "9600"; 

     return int.Parse(baudRate); 
    } 

    public static Parity SetPortParity(Parity defaultPortParity) 
    { 
     string parity; 

     parity = "None"; 

     return (Parity)Enum.Parse(typeof(Parity), parity); 
    } 

    public static int SetPortDataBits(int defaultPortDataBits) 
    { 
     string dataBits; 

     dataBits = "8"; 

     return int.Parse(dataBits); 
    } 

    public static StopBits SetPortStopBits(StopBits defaultPortStopBits) 
    { 
     string stopBits; 

     stopBits = "One"; 

     return (StopBits)Enum.Parse(typeof(StopBits), stopBits); 
    } 

    public static Handshake SetPortHandshake(Handshake defaultPortHandshake) 
    { 
     string handshake; 

     handshake = "None"; 

     return (Handshake)Enum.Parse(typeof(Handshake), handshake); 
    } 
} 
+0

我想,如果你使用_SerialPort.ReadLine(),只有給你的字符串。顯示你是否收到數據。我的意思是你收到的二進制格式。 – 2011-06-02 12:44:41

回答

2

來自端口的數據將始終以二進制(字節)形式存在,因此它取決於如何解釋數據。假設字節是ASCII碼,可以按如下其編碼爲一個字符串:

byte[] binaryData ; // assuming binaryData contains the bytes from the port. 

string ascii = Encoding.ASCII.GetString(binaryData); 
相關問題