2012-01-07 9 views
-2

私は行列d1000 +デジタルスケールを持っています。私はそれが私のアプリケーションではC#を使用して体重が欲しいが、私はこれを行う方法がわからない。それはrs232を介して私のコンピュータに接続されています。C#を使ってデジタルスケールと通信する方法

私はこのコードを使用していますが、重量は1 + e00023のように間違った形式で表示されます。どうすれば修正できるか教えてください。

public partial class ManageSale : Form 
{ 
    private SerialPort port = new SerialPort(
           "COM1", 4800, Parity.None,8, StopBits.One); 

    public ManageSale() 
    { 
     InitializeComponent(); 
     port.DataReceived += 
      new SerialDataReceivedEventHandler(port_DataReceived); 
     port.Open(); 
     port.DtrEnable = true; 
     port.RtsEnable = true; 
    } 

    private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) 
    { 
     this.label1.Text = port.ReadExisting(); 
    } 

    private void ManageSale_Load(object sender, EventArgs e) 
    { 
     port.WriteLine("5"); 
    } 
} 

私の問題は、まだ私が間違っている私のコードを参照してください。この問題を解決するために私を助けるがpleseはされevryoneやあ、それを修正する方法を教えてください。

+0

.netフレームワークには、シリアルIO機能があります –

答えて

3

APIキットの製造元に問い合わせてください。 DllImport文を使用して、おそらくC/C++で書かれたdllにリンクする必要があります。 C/C++間の適切なデータ型の作業は時間がかかることがありますが、実行可能です。

Serial Port Api in the .Net Frameworkを参照してください。ここには例があります:

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 = 500; 
     _serialPort.WriteTimeout = 500; 

     _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; 

     Console.WriteLine("Available Ports:"); 
     foreach (string s in SerialPort.GetPortNames()) 
     { 
      Console.WriteLine(" {0}", s); 
     } 

     Console.Write("COM port({0}): ", defaultPortName); 
     portName = Console.ReadLine(); 

     if (portName == "") 
     { 
      portName = defaultPortName; 
     } 
     return portName; 
    } 

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

     Console.Write("Baud Rate({0}): ", defaultPortBaudRate); 
     baudRate = Console.ReadLine(); 

     if (baudRate == "") 
     { 
      baudRate = defaultPortBaudRate.ToString(); 
     } 

     return int.Parse(baudRate); 
    } 

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

     Console.WriteLine("Available Parity options:"); 
     foreach (string s in Enum.GetNames(typeof(Parity))) 
     { 
      Console.WriteLine(" {0}", s); 
     } 

     Console.Write("Parity({0}):", defaultPortParity.ToString()); 
     parity = Console.ReadLine(); 

     if (parity == "") 
     { 
      parity = defaultPortParity.ToString(); 
     } 

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

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

     Console.Write("Data Bits({0}): ", defaultPortDataBits); 
     dataBits = Console.ReadLine(); 

     if (dataBits == "") 
     { 
      dataBits = defaultPortDataBits.ToString(); 
     } 

     return int.Parse(dataBits); 
    } 

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

     Console.WriteLine("Available Stop Bits options:"); 
     foreach (string s in Enum.GetNames(typeof(StopBits))) 
     { 
      Console.WriteLine(" {0}", s); 
     } 

     Console.Write("Stop Bits({0}):", defaultPortStopBits.ToString()); 
     stopBits = Console.ReadLine(); 

     if (stopBits == "") 
     { 
      stopBits = defaultPortStopBits.ToString(); 
     } 

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

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

     Console.WriteLine("Available Handshake options:"); 
     foreach (string s in Enum.GetNames(typeof(Handshake))) 
     { 
      Console.WriteLine(" {0}", s); 
     } 

     Console.Write("Handshake({0}):", defaultPortHandshake.ToString()); 
     handshake = Console.ReadLine(); 

     if (handshake == "") 
     { 
      handshake = defaultPortHandshake.ToString(); 
     } 

     return (Handshake)Enum.Parse(typeof(Handshake), handshake); 
    } 
} 
関連する問題