首页上一页 1 下一页尾页 5 条记录 1/1页
serialport1
发表在C#图书答疑
2010-07-24
是否精华
是
否
版块置顶:
是
否
老师您好:
我想用串口接收和发送数据
我把串口的2.3短接。
我想做个循环,不断地向串口发送和接收数据,就用了个for语句,总是报以下的错
总是报错:未处理unauthorizeaccessexception
对端口COM1的访问被拒绝
我要是吧for循环去掉,程序运转正常,能发送和接收数据
请老师帮忙,看看那里的错
程序如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
/// <summary>
/// 开启接收数据线程
/// </summary>
private void ReceiveData(SerialPort serialPort)
{
//可用异步接收数据线程
Thread threadReceiveSub = new Thread(new ParameterizedThreadStart(AsyReceiveData));
threadReceiveSub.Start(serialPort);
}
//异步读取
private void AsyReceiveData(object serialPortobj)
{
SerialPort serialPort = (SerialPort)serialPortobj;
System.Threading.Thread.Sleep(500);
try
{
//txtReceive.Text = serialPort.ReadExisting();
int bytes = serialPort.BytesToRead;
byte[] combuffer = new byte[bytes];
serialPort.Read(combuffer, 0, bytes);
string str = BitConverter.ToString(combuffer);
txtReceive.Text = str + txtReceive.Text;
//字串中是否存在指定字符
if (str.Contains("63") && str.Contains("5B"))
{
textBox1.Text = "存在";
}
else
{
textBox1.Text = "不存在";
}
//查找字符串在字串中的索引
int m = str.IndexOf("5B");
txtSend.Text = m.ToString();
//截取字符串的一部分
string s1 = str.Substring(0+m, 2);
textBox2.Text = s1;
string s2=str.Substring(3+m,2);
//字符串相加显示
string s23 = s2 + s1;
textBox3.Text = s23;
textBox4.Text = s1 + s2;
//获取当前年月日
DateTime currentTime = DateTime.Now;
string strYMD = currentTime.ToString("d");
textBox5.Text =strYMD;
//比较两个字符串,返回值为-1,0,1
int result=String.Compare(s2,s1);
textBox6.Text = result.ToString();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
//处理错误
}
serialPort.Close();
}
//想串口发送并接收
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 2; i++)
{
System.Threading.Thread.Sleep(1000); //1 second
//实例化串口对象(默认:COMM1,9600,e,8,1)
SerialPort serialPort1 = new SerialPort();
//更改参数
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 2400;
//serialPort1.bytesize = 8;
serialPort1.Parity = Parity.Even;
serialPort1.StopBits = StopBits.One;
//try { serialPort1.Open(); }
//catch { MessageBox.Show("有错误"); }
[font color=#FF0000] serialPort1.Open();
[/font] int a1 = 34;
int b1 = 91;
int c1 = a1 + b1;
byte[] t = new byte[5];
t[0] = 0x10;
t[1] = 0x5B;
t[2] = Convert.ToByte(a1.ToString("X"), 16);
t[3] = Convert.ToByte(c1.ToString("X"), 16);
t[4] = 0x16;
serialPort1.Write(t, 0, 5);
////////开启接收数据线程
ReceiveData(serialPort1);
}
}
}
}
我想用串口接收和发送数据
我把串口的2.3短接。
我想做个循环,不断地向串口发送和接收数据,就用了个for语句,总是报以下的错
总是报错:未处理unauthorizeaccessexception
对端口COM1的访问被拒绝
我要是吧for循环去掉,程序运转正常,能发送和接收数据
请老师帮忙,看看那里的错
程序如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
/// <summary>
/// 开启接收数据线程
/// </summary>
private void ReceiveData(SerialPort serialPort)
{
//可用异步接收数据线程
Thread threadReceiveSub = new Thread(new ParameterizedThreadStart(AsyReceiveData));
threadReceiveSub.Start(serialPort);
}
//异步读取
private void AsyReceiveData(object serialPortobj)
{
SerialPort serialPort = (SerialPort)serialPortobj;
System.Threading.Thread.Sleep(500);
try
{
//txtReceive.Text = serialPort.ReadExisting();
int bytes = serialPort.BytesToRead;
byte[] combuffer = new byte[bytes];
serialPort.Read(combuffer, 0, bytes);
string str = BitConverter.ToString(combuffer);
txtReceive.Text = str + txtReceive.Text;
//字串中是否存在指定字符
if (str.Contains("63") && str.Contains("5B"))
{
textBox1.Text = "存在";
}
else
{
textBox1.Text = "不存在";
}
//查找字符串在字串中的索引
int m = str.IndexOf("5B");
txtSend.Text = m.ToString();
//截取字符串的一部分
string s1 = str.Substring(0+m, 2);
textBox2.Text = s1;
string s2=str.Substring(3+m,2);
//字符串相加显示
string s23 = s2 + s1;
textBox3.Text = s23;
textBox4.Text = s1 + s2;
//获取当前年月日
DateTime currentTime = DateTime.Now;
string strYMD = currentTime.ToString("d");
textBox5.Text =strYMD;
//比较两个字符串,返回值为-1,0,1
int result=String.Compare(s2,s1);
textBox6.Text = result.ToString();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
//处理错误
}
serialPort.Close();
}
//想串口发送并接收
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 2; i++)
{
System.Threading.Thread.Sleep(1000); //1 second
//实例化串口对象(默认:COMM1,9600,e,8,1)
SerialPort serialPort1 = new SerialPort();
//更改参数
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 2400;
//serialPort1.bytesize = 8;
serialPort1.Parity = Parity.Even;
serialPort1.StopBits = StopBits.One;
//try { serialPort1.Open(); }
//catch { MessageBox.Show("有错误"); }
[font color=#FF0000] serialPort1.Open();
[/font] int a1 = 34;
int b1 = 91;
int c1 = a1 + b1;
byte[] t = new byte[5];
t[0] = 0x10;
t[1] = 0x5B;
t[2] = Convert.ToByte(a1.ToString("X"), 16);
t[3] = Convert.ToByte(c1.ToString("X"), 16);
t[4] = 0x16;
serialPort1.Write(t, 0, 5);
////////开启接收数据线程
ReceiveData(serialPort1);
}
}
}
}