日期:2014-05-18  浏览次数:21464 次

c#通过串口发短信,每次打开串口总死,不知道为什么
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SZCASIC.JC.client.configure;
using System.Collections;
using System.IO.Ports;

namespace MultiLangTest
{
  public partial class SerialPortTest : Form
  {

  private SerialPort sp;
  public SerialPortTest()
  {
  InitializeComponent();
  }

  private void SerialPortTest_Load(object sender, EventArgs e)
  {
  //OuterDevice.getDeviceInfo("ICCard");
  //Hashtable ht = new Hashtable();
  //ht.Add("Port", "USB");
  //ht.Add("type", "usb1");
  //ht.Add("rate", "19200");
  //OuterDevice.setDeviceInfo("ICCard", ht);
  IList list = OuterDevice.getComDataInfo("COM");
  this.comboBox1.DataSource = list[0];
  this.comboBox2.DataSource = list[1];
  this.comboBox5.DataSource = list[2];
  this.comboBox3.DataSource = list[3];
  this.comboBox4.DataSource = list[4];  
  this.comboBox6.DataSource = list[5];
  this.comboBox4.Text = this.comboBox4.Items[1] as string;
  }

  private void button3_Click(object sender, EventArgs e)
  {

  }

  private void button1_Click(object sender, EventArgs e)
  {
  Parity p = Parity.Even;
  switch(comboBox5.Text.ToLower())
  {
  case "none":
  p = Parity.None;
  break;
  case "odd":
  p = Parity.Odd;
  break;
  case "even":
  p = Parity.Even;
  break;
  case "mark":
  p = Parity.Mark;
  break;
  case "space":
  p = Parity.Space;
  break;
  default:
  break;
  }
  StopBits sb = StopBits.None;
  switch (comboBox4.Text.ToLower())
  {
  case "1":
  sb = StopBits.One;
  break;
  case "1.5":
  sb = StopBits.OnePointFive;
  break;
  case "2":
  sb = StopBits.Two;
  break;
  default:
  break;
  }
   
  sp = new SerialPort(comboBox1.Text, Convert.ToInt32(comboBox2.Text), 
  p, Convert.ToInt32(comboBox3.Text), sb);

  //sp.DataReceived += new SerialDataReceivedEventHandler(this.ReadSMSData);

  if (sp.IsOpen)
  {
  //MessageBox.Show(string.Format("{0} 串口已经打开", comboBox1.Text));
  sp