DotNet笔记

兴趣是最好的老师。
posts - 344, comments - 1153, trackbacks - 46, articles - 1
   :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

mycom是串口通信类,在本blog前一篇中有完整代码。( C#串口通信编程类(修改版) )
下面是串口通讯测试程序的源代码,将mycom类放在此项目中
可以实现简单的串口通信,希望读者能通过这个程序对串口通信过程有一个初步的了解:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace BusApp
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.GroupBox groupBox1;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.Label label4;
  private System.Windows.Forms.Label label5;
  private System.Windows.Forms.Label label6;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.Button button3;
  private System.Windows.Forms.Button button4;
  private System.Windows.Forms.TextBox textBox8;
  private System.Windows.Forms.Label label7;

  public int iPort=1; //1,2,3,4
  public int iRate=9600; //1200,2400,4800,9600
  public byte bSize=8; //8 bits
  public byte bParity=0; // 0-4=no,odd,even,mark,space
  public byte bStopBits=1; // 0,1,2 = 1, 1.5, 2
  public int iTimeout=1000;
  public mycom mycom1=new mycom();
  public byte[] recb;

  private System.Windows.Forms.TextBox msg;
  private System.Windows.Forms.TextBox t_port;
  private System.Windows.Forms.TextBox t_rate;
  private System.Windows.Forms.TextBox t_bytesize;
  private System.Windows.Forms.TextBox t_stopbyte;
  private System.Windows.Forms.TextBox t_parity;
  private System.Windows.Forms.TextBox t_send;
  private System.Windows.Forms.Button button5; //readTimeOut
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   InitializeComponent();
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.msg = new System.Windows.Forms.TextBox();
   this.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.t_send = new System.Windows.Forms.TextBox();
   this.button1 = new System.Windows.Forms.Button();
   this.groupBox1 = new System.Windows.Forms.GroupBox();
   this.button2 = new System.Windows.Forms.Button();
   this.t_port = new System.Windows.Forms.TextBox();
   this.label3 = new System.Windows.Forms.Label();
   this.t_rate = new System.Windows.Forms.TextBox();
   this.label4 = new System.Windows.Forms.Label();
   this.t_bytesize = new System.Windows.Forms.TextBox();
   this.label5 = new System.Windows.Forms.Label();
   this.t_stopbyte = new System.Windows.Forms.TextBox();
   this.label6 = new System.Windows.Forms.Label();
   this.t_parity = new System.Windows.Forms.TextBox();
   this.button3 = new System.Windows.Forms.Button();
   this.button4 = new System.Windows.Forms.Button();
   this.textBox8 = new System.Windows.Forms.TextBox();
   this.label7 = new System.Windows.Forms.Label();
   this.button5 = new System.Windows.Forms.Button();
   this.groupBox1.SuspendLayout();
   this.SuspendLayout();
   //
   // msg
   //
   this.msg.ForeColor = System.Drawing.Color.Green;
   this.msg.Location = new System.Drawing.Point(0, 0);
   this.msg.Multiline = true;
   this.msg.Name = "msg";
   this.msg.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
   this.msg.Size = new System.Drawing.Size(512, 264);
   this.msg.TabIndex = 0;
   this.msg.Text = "";
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(16, 24);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(56, 16);
   this.label1.TabIndex = 1;
   this.label1.Text = "串口号:";
   //
   // label2
   //
   this.label2.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(64)), ((System.Byte)(0)));
   this.label2.Location = new System.Drawing.Point(8, 280);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(80, 16);
   this.label2.TabIndex = 1;
   this.label2.Text = "设置数据包:";
   //
   // t_send
   //
   this.t_send.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.t_send.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(0)));
   this.t_send.Location = new System.Drawing.Point(80, 272);
   this.t_send.Name = "t_send";
   this.t_send.Size = new System.Drawing.Size(344, 21);
   this.t_send.TabIndex = 2;
   this.t_send.Text = "";
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(432, 272);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(40, 23);
   this.button1.TabIndex = 3;
   this.button1.Text = "发送";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // groupBox1
   //
   this.groupBox1.Controls.Add(this.button2);
   this.groupBox1.Controls.Add(this.t_port);
   this.groupBox1.Controls.Add(this.label1);
   this.groupBox1.Controls.Add(this.label3);
   this.groupBox1.Controls.Add(this.t_rate);
   this.groupBox1.Controls.Add(this.label4);
   this.groupBox1.Controls.Add(this.t_bytesize);
   this.groupBox1.Controls.Add(this.label5);
   this.groupBox1.Controls.Add(this.t_stopbyte);
   this.groupBox1.Controls.Add(this.label6);
   this.groupBox1.Controls.Add(this.t_parity);
   this.groupBox1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(64)), ((System.Byte)(0)));
   this.groupBox1.Location = new System.Drawing.Point(8, 304);
   this.groupBox1.Name = "groupBox1";
   this.groupBox1.Size = new System.Drawing.Size(176, 216);
   this.groupBox1.TabIndex = 4;
   this.groupBox1.TabStop = false;
   this.groupBox1.Text = "参数设置";
   //
   // button2
   //
   this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
   this.button2.Location = new System.Drawing.Point(80, 184);
   this.button2.Name = "button2";
   this.button2.TabIndex = 3;
   this.button2.Text = "应用设置";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // t_port
   //
   this.t_port.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.t_port.Location = new System.Drawing.Point(80, 16);
   this.t_port.Name = "t_port";
   this.t_port.Size = new System.Drawing.Size(80, 21);
   this.t_port.TabIndex = 2;
   this.t_port.Text = "1";
   //
   // label3
   //
   this.label3.Location = new System.Drawing.Point(16, 58);
   this.label3.Name = "label3";
   this.label3.Size = new System.Drawing.Size(56, 16);
   this.label3.TabIndex = 1;
   this.label3.Text = "波特率:";
   //
   // t_rate
   //
   this.t_rate.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.t_rate.Location = new System.Drawing.Point(80, 50);
   this.t_rate.Name = "t_rate";
   this.t_rate.Size = new System.Drawing.Size(80, 21);
   this.t_rate.TabIndex = 2;
   this.t_rate.Text = "9600";
   //
   // label4
   //
   this.label4.Location = new System.Drawing.Point(16, 92);
   this.label4.Name = "label4";
   this.label4.Size = new System.Drawing.Size(56, 16);
   this.label4.TabIndex = 1;
   this.label4.Text = "数据位:";
   //
   // t_bytesize
   //
   this.t_bytesize.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.t_bytesize.Location = new System.Drawing.Point(80, 84);
   this.t_bytesize.Name = "t_bytesize";
   this.t_bytesize.Size = new System.Drawing.Size(80, 21);
   this.t_bytesize.TabIndex = 2;
   this.t_bytesize.Text = "8";
   //
   // label5
   //
   this.label5.Location = new System.Drawing.Point(16, 126);
   this.label5.Name = "label5";
   this.label5.Size = new System.Drawing.Size(56, 16);
   this.label5.TabIndex = 1;
   this.label5.Text = "停止位:";
   //
   // t_stopbyte
   //
   this.t_stopbyte.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.t_stopbyte.Location = new System.Drawing.Point(80, 118);
   this.t_stopbyte.Name = "t_stopbyte";
   this.t_stopbyte.Size = new System.Drawing.Size(80, 21);
   this.t_stopbyte.TabIndex = 2;
   this.t_stopbyte.Text = "1";
   //
   // label6
   //
   this.label6.Location = new System.Drawing.Point(16, 160);
   this.label6.Name = "label6";
   this.label6.Size = new System.Drawing.Size(56, 16);
   this.label6.TabIndex = 1;
   this.label6.Text = "校验位:";
   //
   // t_parity
   //
   this.t_parity.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.t_parity.Enabled = false;
   this.t_parity.Location = new System.Drawing.Point(80, 152);
   this.t_parity.Name = "t_parity";
   this.t_parity.Size = new System.Drawing.Size(80, 21);
   this.t_parity.TabIndex = 2;
   this.t_parity.Text = "0";
   //
   // button3
   //
   this.button3.Location = new System.Drawing.Point(472, 272);
   this.button3.Name = "button3";
   this.button3.Size = new System.Drawing.Size(40, 23);
   this.button3.TabIndex = 3;
   this.button3.Text = "清空";
   this.button3.Click += new System.EventHandler(this.button3_Click);
   //
   // button4
   //
   this.button4.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
   this.button4.Location = new System.Drawing.Point(432, 312);
   this.button4.Name = "button4";
   this.button4.Size = new System.Drawing.Size(72, 23);
   this.button4.TabIndex = 6;
   this.button4.Text = "初始化";
   //
   // textBox8
   //
   this.textBox8.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.textBox8.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(0)));
   this.textBox8.Location = new System.Drawing.Point(288, 312);
   this.textBox8.Name = "textBox8";
   this.textBox8.Size = new System.Drawing.Size(136, 21);
   this.textBox8.TabIndex = 7;
   this.textBox8.Text = "";
   //
   // label7
   //
   this.label7.Location = new System.Drawing.Point(200, 320);
   this.label7.Name = "label7";
   this.label7.Size = new System.Drawing.Size(100, 16);
   this.label7.TabIndex = 8;
   this.label7.Text = "设置本机地址:";
   //
   // button5
   //
   this.button5.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
   this.button5.Location = new System.Drawing.Point(440, 504);
   this.button5.Name = "button5";
   this.button5.Size = new System.Drawing.Size(64, 23);
   this.button5.TabIndex = 9;
   this.button5.Text = "关闭串口";
   this.button5.Click += new System.EventHandler(this.button5_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(512, 533);
   this.Controls.Add(this.button5);
   this.Controls.Add(this.textBox8);
   this.Controls.Add(this.label7);
   this.Controls.Add(this.button4);
   this.Controls.Add(this.groupBox1);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.t_send);
   this.Controls.Add(this.msg);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.button3);
   this.Name = "Form1";
   this.Text = "串口通讯(小y设计)";
   this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
   this.Load += new System.EventHandler(this.Form1_Load);
   this.groupBox1.ResumeLayout(false);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }
//程序开启,串口初始化
  private void Form1_Load(object sender, System.EventArgs e)
  {
   mycom1.PortNum=iPort;
   mycom1.BaudRate=iRate;
   mycom1.ByteSize=bSize;
   mycom1.Parity=bParity;
   mycom1.StopBits=bStopBits;
   mycom1.ReadTimeout=iTimeout;
   if(this.OpenCom())
    msg.AppendText("串口初始化成功……\r\n");
   else
    msg.AppendText("串口初始化失败!\r\n");
  }
//显示包信息
  public string dis_package(byte[] reb)
  {
   string temp="";
   foreach(byte b in reb)
    temp+=b.ToString("X2")+" ";
   return temp;
  }
//开串口
  public bool OpenCom()
  {
   try
   {
     if (mycom1.Opened)
     {
      mycom1.Close();
      mycom1.Open(); //打开串口
     }
     else
     {
      mycom1.Open();//打开串口
     }
     return true;
   }
   catch(Exception e)
   {
     MessageBox.Show("错误:" + e.Message);
     return false;
   }

  }
//发送按钮
  private void button1_Click(object sender, System.EventArgs e)
  {
   if(t_send.Text=="")
   {MessageBox.Show("发送数据为空!");return;}
   byte[] temp1=mysendb();
   int sendnumb=0;
   try
   {
    sendnumb=mycom1.Write(temp1);
     msg.AppendText("\r\n发送数据("+sendnumb+"):"+dis_package(temp1));
    recb=mycom1.Read(50);
    //if(recb.Length!=0)
     msg.AppendText("\r\n接收到数据包:"+dis_package(recb));
   }
   catch
   {msg.AppendText("\r\n发送失败!");return;}
   
   //OpenCom();
  }
//去掉发送数组中的空格
  public string delspace(string putin)
  {
   string putout="";
   for(int i=0;i<putin.Length;i++)
   {
    if(putin[i]!=' ')
     putout+=putin[i];
   }
   return putout;
  }
//提取数据包
  public byte[] mysendb()
  {
   string temps=delspace(t_send.Text);
   byte[] tempb=new byte[50];
   int j=0;
   for(int i=0;i<temps.Length;i=i+2,j++)
    tempb[j]=Convert.ToByte(temps.Substring(i,2),16);
   byte[] send=new byte[j];
   Array.Copy(tempb,send,j);
   return send;
  }
//清空按钮
  private void button3_Click(object sender, System.EventArgs e)
  {
   t_send.Text=string.Empty;
   msg.Text=string.Empty;
  }

//参数设置
  private void button2_Click(object sender, System.EventArgs e)
  {
    mycom1.PortNum=Convert.ToInt16(t_port.Text); //1,2,3,4
   mycom1.BaudRate=Convert.ToInt16(t_rate.Text); //1200,2400,4800,9600
   mycom1.ByteSize=Convert.ToByte(t_bytesize.Text,10); //8 bits
   mycom1.Parity=Convert.ToByte(t_parity.Text,10); // 0-4=no,odd,even,mark,space
   mycom1.StopBits=Convert.ToByte(t_stopbyte.Text,10); // 0,1,2 = 1, 1.5, 2
   //iTimeout=3;
   if(this.OpenCom())
    msg.AppendText("串口初始化成功……\r\n");
   else
    msg.AppendText("串口初始化失败!\r\n");
  }
//程序关闭,结束串口
  private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  {
   mycom1.Close();
  }

  private void button5_Click(object sender, System.EventArgs e)
  {
   if(mycom1.Opened)
   {
    mycom1.Close();
    button5.Text="开启串口";
    msg.AppendText("\r\n串口被关闭……");
   }
   else
   {
    mycom1.Open();
    button5.Text="关闭串口";
    msg.AppendText("\r\n串口成功开启……");
   }
  }
 }
}

注意:发送数据包的格式是16进制数据如:02 45 66 FA  中间可以有或者没有空格,但要保证有偶数位

TO 大家:

   该程序源码已经放到本blog第一篇提供下载。

---------------------------------------------------------------------

Feedback

#1楼   回复  引用    

2006-11-20 10:56 by 阿鲲[未注册用户]
very good

#2楼   回复  引用    

2006-12-19 11:33 by Taylor[未注册用户]
为什么不能发送?端口号应该设置多少?

#3楼[楼主]   回复  引用  查看    

2006-12-19 13:43 by 小y      
根据你的通信设备而定
我用的是9600

#4楼   回复  引用    

2007-01-04 11:58 by SG[匿名][未注册用户]
我可以发送,但是无法接收,接收的数组是个空的!是应为我没有连接设备么?
另外,发送数据有限制,你的代码是:tempb[j]=Convert.ToByte(temps.Substring(i,2),16);两位两位取,

所以,只能发偶数位数据如:23,2234,445433或34532678
如果是:1,232,34455,5434566或456431234就报错。

#5楼   回复  引用    

2007-02-06 16:43 by syeerzy[未注册用户]
if (mycom1.Opened)
  {
   mycom1.Close();
   mycom1.Open(); //打开串口
  }

请问一下,对已经打开状态的,为什么需要关闭后再重新打开呢?

如果是有某种原因需要如此,个人觉得应该把这个逻辑封装进mycom类里面而不是在调用的时候来写这个.

#6楼   回复  引用    

2007-02-09 16:06 by cc[未注册用户]
private byte[] stringTobyte(string s)
{
ArrayList al=new ArrayList();
for(int i=0;i<s.Length;i++)
{
al.Add((byte)s[i]);
}
return (byte[])al.ToArray(System.Type.GetType("System.Byte"));
}


byte[] b=stringTobyte("\xb4\x41\x23");
你要保证你传过去的字符串里面的数值都在byte精度范围内。

#7楼   回复  引用    

2007-03-23 08:23 by C#菜鸟[未注册用户]
该程式的16进制转换有问题,望版主帮忙看看.

#8楼[楼主]   回复  引用  查看    

2007-03-23 22:11 by 小y      
to SG
所以,只能发偶数位数据如:23,2234,445433或34532678
如果是:1,232,34455,5434566或456431234就报错。

可以在字符串转换为byte的函数之前检测字符串是否是偶数位
如果是奇数位的话,在前面加0

即在函数:
//提取数据包
public byte[] mysendb()
{
string temps=delspace(t_send.Text);
byte[] tempb=new byte[50];
int j=0;
for(int i=0;i<temps.Length;i=i+2,j++)
tempb[j]=Convert.ToByte(temps.Substring(i,2),16);
byte[] send=new byte[j];
Array.Copy(tempb,send,j);
return send;
}
中:
在 byte[] tempb=new byte[50];之前加
if(temps.Length%2!=0)
temps="0"+temps;

#9楼[楼主]   回复  引用  查看    

2007-03-23 22:12 by 小y      
谢谢 cc
提供的代码

#10楼[楼主]   回复  引用  查看    

2007-03-23 22:16 by 小y      
C#菜鸟:
该程式的16进制转换有问题,望版主帮忙看看.

其实这个测试程序是我自己做测试用的,有前提条件
比如输入的十六进制字符串必须是偶数位
而且不能是超过F的数字

#11楼   回复  引用    

2007-04-04 14:40 by C#菜鸟[未注册用户]
哦,明白了,谢谢小Y.
上次有个疑问,已经给你留言,不知道你有没有收到??

#12楼   回复  引用    

2007-04-04 15:35 by RIEU[未注册用户]
调错误 1 类型“BusApp.Form1”的声明上缺少 partial 修饰符;存在此类型的其他分部声明 试时出现的这个问题是怎么回事?

#13楼   回复  引用    

2007-04-11 09:06 by C#菜鸟[未注册用户]
小Y,你的程式我现在需要发送字符串,比如发送"00ffwwm","0500ffwry"等,你的程式需要做什么样的改动,谢谢了.

#14楼[楼主]   回复  引用  查看    

2007-04-11 15:11 by 小y      
给硬件通讯还要发字符串吗?
除非它支持string类型
我的程序发送的是十六进制数据
在C#里面的类型是byte
其实给硬件通讯就是在一块缓冲区中填入数据
如果你的硬件支持字符串,也可以填string类型的数据进去的

string sendStr="aadfasdf";
sendnumb=mycom1.Write(sendStr);

#15楼   回复  引用    

2007-04-17 22:46 by C#菜鸟[未注册用户]
谢谢小Y回答,其实是我搞错了,呵呵......
我发送的也是byte.问题已经解决了.
小Y,最近接到一个任务,要做一个系统,系统要求是这样的:
估计有6~8个左右的人在浏览网页,网页中都有显示数据库数据,当其中任意一个人改变了数据库资料后,其他浏览者的内容也能即时自动更新(不要利用延时自动刷新技术的).不知道WEB能不能实现这种功能??

#16楼   回复  引用    

2007-05-14 14:35 by C#菜鸟[未注册用户]
小Y,我用你的程序运行后,明明我得电脑只有COM1串口.可是当我打开

COM2,COM3,COM4,COM5时,程序竟然也告诉我说串口打开成功.实在是不明白.菜鸟我晕啊

#17楼[楼主]   回复  引用  查看    

2007-05-15 13:50 by 小y      
关于为什么要:
mycom1.Close();
mycom1.Open(); //打开串口

因为你原先开的可能是COM1,现在要打开COM2

关于:
COM2,COM3,COM4,COM5时,程序竟然也告诉我说串口打开成功.

记得以前我试的时候不会出现这种情况,现在也没环境调试了 抱歉

#18楼   回复  引用    

2007-06-01 17:52 by C#.木头[未注册用户]
小y 急呀,,,,我的设备就是只支持发ASCii 所以我照你上面说的改了一下程序

string sendStr="aadfasdf";
sendnumb=mycom1.Write(sendStr);

我编译就出错了.
1.mycom.Write(byte[])”最匹配的重载方法具有一些无效参数
2.参数“1”: 无法从“string”转换为“byte[]”.

我如果就要发ASCii字符给设备.还得怎么改呢.是不是要从新写一个方法.把mycom1.Write() 这个方法改改呢.小y 大哥,帮帮忙.帮我出出主意吧.

先谢谢了哦..每次都烦麻你...

#19楼[楼主]   回复  引用  查看    

2007-06-02 10:18 by 小y      
string sendStr="aadfasdf";

先把string类型转换成byte[]
mycom.Write()的参数是byte[]

#20楼   回复  引用    

2007-06-07 12:10 by maggie[未注册用户]
小y你好,我用你介绍的类试了一下,能够编译成功,但是运行时总是出现“无法加载kernel32.dll”的错误。我该怎么处理呢?希望你给予帮助。谢谢!

#21楼   回复  引用    

2007-06-11 20:33 by host[未注册用户]
要发送字符串 ESC P CR LF
byte[] send={27,80,13,10}//其中数字对应字符串的ascii码即可

mycom.Write(send)

#22楼   回复  引用    

2007-07-22 10:50 by lhx[未注册用户]
调错误 1 类型“BusApp.Form1”的声明上缺少 partial 修饰符;存在此类型的其他分部声明 试时出现的这个问题是怎么回事?

#23楼[楼主]   回复  引用  查看    

2007-07-23 08:50 by 小y      
to lhx

我这个程序是在vs2003下写的
如果你在vs2005打开会出现这个问题

#24楼   回复  引用    

2007-07-23 16:10 by lhx[未注册用户]
谢谢小y
看看怎么调一下去~~

#25楼   回复  引用    

2007-07-27 15:09 by ztyz[未注册用户]
这个程序可以接受数据单片机送来的数据么?

#26楼   回复  引用    

2007-08-02 18:10 by 王波[未注册用户]
mycom 从那里下载?

#27楼   回复  引用    

2007-08-02 18:11 by 王波[未注册用户]
mycom 从那里下载? 急急 !!!!!!!!!!!

#28楼[楼主]   回复  引用  查看    

2007-08-06 19:47 by 小y      
to 王波

请看本篇第一行的超级链接 点击进去就有mycom的源码了

#29楼   回复  引用    

2007-08-13 11:50 by 寻[未注册用户]
public mycom mycom1=new mycom();
问题:
The type or namespace name 'mycom' could not be found (are you missing a using directive or an assembly reference?)

ls:这是怎么回事啊?

#30楼   回复  引用    

2007-08-13 15:17 by 寻[未注册用户]
看到了前面的说明,mycom类加进去了

#31楼   回复  引用    

2007-08-15 20:12 by 蓝[未注册用户]
关于可以传送string,这个到底怎样改,麻烦说得详细些,刚入门,很多不清楚,麻烦了

#32楼[楼主]   回复  引用  查看    

2007-08-16 09:34 by 小y      
to 蓝

其实串口只能传送byte类型的数据,所以要传string类型必须先把string转换成byte数组。

代码如:

string s = "abc asdfiasdof ";
byte[] bytes = System.Text.Encoding.Default.GetBytes(s);

#33楼   回复  引用    

2007-09-07 20:10 by bbh[未注册用户]
小Y您好:
我用了您给我的C#串口通信程序后,在编译时提示mycom类缺少引用,我该到哪里去找,请您给与指点.在此先谢谢!.
邮箱:bbh309@163.com

#34楼   回复  引用    

2007-09-27 13:22 by flag[未注册用户]
也不搞个连接,这mycom找不到,太遗憾了.

#35楼   回复  引用    

2007-10-07 06:48 by Findy[未注册用户]
楼主给我发一份
错误 1 类型“BusApp.Form1”已定义了一个名为“Dispose”的具有相同参数类型的成员
总出现这个错误.怎么改都不行.如果改掉就报错
我的邮箱是wangping_li@163.com
谢谢了
哪个成功了的也给我发一份哈..

#36楼   回复  引用    

2007-10-16 11:04 by Stone0616[未注册用户]
其实发送很容易,关键是接收,你这个好像是同步方式,请问有异步方式吗?如果加入线程又该怎么办呢?谢谢,如果可以的话,能不能把原码发到我邮箱:Toyan@tom.com,我学习一下呢?谢谢

#37楼   回复  引用  查看    

2007-10-18 19:40 by 外城      
这个类我用了,很好用,先行谢过!

有个怪现象请教小Y:

环境:win2000,.net2.0
COM2串口

开机后直接用‘你给的程序’,发送"F7"(按设备定义应该返回F7),却返回了7A;发送别的指令返回都不和预期返回值不符;

但是:用“串口调试助手2.0”打开COM2口,发送和接收正常!

再次用‘你给程序’发送"7A",返回“7A”,却正常了!

也就是说,在刚开机时,必须先用“串口调试助手2.0”和串口通讯后,‘你的程序’才能正常通讯!~

郁闷了一天,找不到解决办法,望指教!谢谢
比较着急,有更好的代码请发送至goga21cn@126.com
再次感谢!!

#38楼   回复  引用    

2007-11-15 09:31 by liang990[未注册用户]
@bbh

再后面有一个用C#写的mycom类 直接加到这个程序就好用了

#39楼   回复  引用    

2007-11-15 09:31 by liang990[未注册用户]
@ztyz
当然可以

#40楼   回复  引用    

2007-11-15 09:32 by liang990[未注册用户]
@寻
你没有添加mycom类

#41楼[楼主]   回复  引用  查看    

2007-11-16 21:47 by 小y      

TO 大家:

     该程序源码已经放到本blog第一篇提供下载。


#42楼   回复  引用    

2007-11-22 18:59 by eyes[未注册用户]
小y,你好:
我想问一下,
怎么用serialport类实现串口双机文件传输?用串口传送文件的流程是怎样的?

#43楼[楼主]   回复  引用  查看    

2007-11-22 23:10 by 小y      
为什么要用串口呢
双机用交叉线就可以通讯了啊

#44楼   回复  引用    

2007-11-23 18:51 by eyes[未注册用户]
哎...规定要用串口实现双机通信咯..
文件同步传输想了几天也没能解决..
我不明白同步文件传输是怎么工作的...
小y能解释下吗?

#45楼   回复  引用    

2007-12-05 22:46 by 程序人[未注册用户]
与37楼兄弟遇到一样的问题,请问楼主能有什么好的办法解决吗?
急盼!

liu_qion@163.com

开机时,必须先用“串口调试助手2.0”和串口通讯后,‘你的程序’才能正常通讯!~

#46楼[楼主]   回复  引用  查看    

2007-12-06 20:01 by 小y      
我以前遇到这个问题的时候用C++把那个类(mycom)重写就可以了

#47楼   回复  引用    

2007-12-28 17:27 by pansha[未注册用户]
错误 1 找不到类型或命名空间名称“mycom”(是否缺少 using 指令或程序集引用?) E:\Vision 2005\小串口程序\串口适用\串口适用\Form1.Designer.cs 38 16 串口适用

#48楼   回复  引用    

2007-12-28 18:13 by pansha[未注册用户]
请发一个mycom代码撒
谢谢:

#49楼   回复  引用    

2008-01-05 20:00 by 小B[未注册用户]
小Y你好问你点问题

我用的设备只能接收ASCII码

可我用VS2005自带的SerialPort的发送 只能发送字符串
这怎么办啊~~~

发送后设备给我返回的 ERR信息

#50楼[楼主]   回复  引用  查看    

2008-01-06 16:52 by 小y      
to 小B

你的设备只能接收ASCII码? 不可能
那个设备的驱动谁写的,设备可以接收byte数组,怎么解码要看驱动程序的
发送的永远是byte数组 不可能是字符串!!
字符串最后也编码成byte数组了

#51楼   回复  引用  查看    

2008-01-09 02:53 by 侯唯      
楼主,你好
我现在有个项目,设想是用一台电脑拖多个ID卡读卡器,因为一次刷卡动作是要刷两张卡才算完成,为防止冲突,需要知道设备机器号(或端口号),想请问楼主,在收到串口设备发来的数据,可否知道是哪个端口传来的?

#52楼   回复  引用    

2008-01-09 15:23 by Dardanelles[未注册用户]
大哥可否也发一份给小弟,谢谢!
huzhengkui@sina.com

#53楼   回复  引用    

2008-01-25 10:01 by QingTianWW[未注册用户]
做C#串口的朋友加这个Q群(54571396)C#串口群

#54楼   回复  引用  查看    

2008-02-29 11:13 by c.life      
文件中的类都不能进行设计,因此未能为该文件显示设计器。设计器检查出文件中有以下类: mycom --- 无法设计基类“System.Object”。 Form1 --- 无法设计基类“System.Object”。
----

原代码没无法加载?那个类(MYCOMM)也不能添加进去,我用的是VS2005

能否发一份,谢谢.
yuwy.sh@163.com


#55楼   回复  引用    

2008-03-01 11:08 by SNYG[未注册用户]
博主能否发送一份
snyg689@yahoo.com.cn

#56楼   回复  引用    

2008-03-18 10:13 by mousemr[未注册用户]
与37楼及45楼情况一样,博主是否有解决,能否赐教mousemr@sohu.com

#57楼   回复  引用    

2008-04-11 12:12 by usersh[未注册用户]
找不到下载地址,希望发一份给我:czglover@163.com

#58楼   回复  引用    

2008-04-14 10:22 by 付[未注册用户]
小Y你好
我是最近才开始做串口的,我想找些例子
我下了你的源码,报错:找不到类型或命名空间名称“mycom”(是否缺少 using 指令或程序集引用?),这个怎么解决
如果可以,希望发一份给我:fubo403@163.com
谢谢

#59楼   回复  引用    

2008-05-10 16:43 by mysterious[未注册用户]
博主你好
我用的是VS2005,运行是出现错误1:类型“BusApp.Form1”的声明上缺少 partial 修饰符;存在此类型的其他分部声明 C:\Documents and Settings\maomao\My Documents\Visual Studio 2005\Projects\BusApp\BusApp\Form1.cs 13 18 BusApp
请问这样的问题应该怎么解决?谢谢!

#60楼   回复  引用    

2008-05-17 18:28 by gaojing[未注册用户]
楼主你好。我运行了你那个程序,串口号选择 几都能运行 但实际上只有串口1。2。3 怎么在应用设置BUTTON 里写代码 判断 选择串口号 实际是否存在 如不存在则显示 设置失败 拜托楼主大哥了!我做毕业设计的 急。。。 看到请帮助我下好吗?

#61楼   回复  引用    

2008-05-17 18:30 by gaojing[未注册用户]
还有就是 串口调试助手那样的 能发送 字符 比如网址什么的 怎么写代码?

#62楼   回复  引用    

2008-05-27 15:40 by HBK[未注册用户]
串口号一般在UI中,由用户手动输入即可,程序负责读取用户输入的串口号名称,并初始化该串口,成功则提示可用,否则提示警告信息即可!判断串口是否可用,个人觉得没有别的办法,只能是实际使用中发送数据超时或无法得到对方应答信息,才能判断串口是否可用!串口需要底层驱动来支撑,只有你没有安装这个驱动程序,应用windows API来进行串口打开指令才会返回false,否则,永远都是可以打开,只要不影响应用就可以!串口发送出去的永远都是字节流,即使你认为是ASCII,其实还是转换成BYTE发送出去的,所以,发送字符也好还是传输文件也好,无外乎就是封装一个缓冲区对象,只是数据来源有可能是文件,也有可能是外部智能设备,你只需要在缓冲区对象中加入字符串到字节流或字节流到字符串转换的方法即可!

#63楼   回复  引用    

2008-05-30 11:17 by 五星[未注册用户]
你好,我想问一个问题,我最近在做一个从电子磅读取数据的程序,也是用的C#,我用了你的方法,能够读取的数据了,但是好像是16进制的数据。。怎么转换也转换不到10进制来。。麻烦告诉一下。。。我就是想让电子磅上显示的重量通过程序显示出来。。。

#64楼   回复  引用    

2008-06-02 17:52 by 五星[未注册用户]
@五星

#65楼   回复  引用    

2008-06-24 10:49 by 钟勇[未注册用户]
小Y你好,我是个毕业生,大学里面没怎么学习.现在找了个工作叫我做串口通讯程序.我很茫然.开发环境是VS2005用C#我觉得我大学就学了点点TC现在是什么都不懂就光说VS2005都使用不来还不说去熟悉C#.
就是想请教下我现在情况怎么去学习比较快.
我把你的程序下了也是那个问题
“BusApp.Form1”的声明上缺少 partial 修饰符;存在此类型的其他分部声明.
我你说你是2003我用2005就有问题那怎么解决呢?
谢谢

#66楼   回复  引用    

2008-06-25 10:08 by jungleboy[未注册用户]
哥们!谢谢了

#67楼   回复  引用    

2008-07-02 09:59 by c#(jacky)[未注册用户]
谢谢!不错能否发送完整的代码给我?yuanbincs@hotmail.com 谢谢!

#68楼   回复  引用    

2008-07-09 19:58 by denli[未注册用户]
想研究一下,

能不能发一下此程序的源代码给我一下咯?

我的邮箱:denli8@163.com

谢谢了!

#69楼   回复  引用    

2008-07-16 12:45 by Jackfather[未注册用户]
为何发送数据不能接收到数据呀?

#70楼   回复  引用    

2008-07-28 11:17 by 承诺[未注册用户]
小y兄,最近在做串口通信方面的,这方面不是很了解。下了你的例子,参考着自己做,有几个问题想请教:1.comm.dll通过添加引用添加不进去;2.运行不成功,总是提示Comm Can Not Be Opened,在mycomm.write附近出错;3.想弱智的问下,在B/S架构下能不能实现串口的通信。 很菜鸟,希望小y兄能解答

#71楼   回复  引用    

2008-08-14 10:17 by alex38[未注册用户]
--引用--------------------------------------------------
程序人: 与37楼兄弟遇到一样的问题,请问楼主能有什么好的办法解决吗?
<br>急盼!
<br>
<br>liu_qion@163.com
<br>
<br>开机时,必须先用“串口调试助手2.0”和串口通讯后,‘你的程序’才能正常通讯!~
--------------------------------------------------------
一样的现象,没有人知道么

#72楼   回复  引用    

2008-08-14 16:38 by alex38[未注册用户]
--引用--------------------------------------------------
程序人: 与37楼兄弟遇到一样的问题,请问楼主能有什么好的办法解决吗?
<br>急盼!
<br>
<br>liu_qion@163.com
<br>
<br>开机时,必须先用“串口调试助手2.0”和串口通讯后,‘你的程序’才能正常通讯!~
--------------------------------------------------------
应该是那个参数设置不正确,先运行串口调试助手,就把该参数设置好了,所以可以正确传输

#73楼   回复  引用    

2008-09-12 17:28 by 康海涛[未注册用户]
为什么设置成偶效验返回数据还试无效验数据

#74楼   回复  引用  查看    

2008-10-27 17:00 by barlong      
经学习后,发现你写的没有接收的处理.

#75楼   回复  引用    

2008-11-11 18:40 by 大慧[未注册用户]
小y 你好!
我在做GPRS模块(可以和串口调试助手正常使用)的串口通信,刚做了一个接收发送界面
使用的serialport控件 可以发送,接收却是一堆乱码,用的serialport.ReadExisting();发现就是一开始的ReadExisting()这读出的源数据乱掉,,还有与GPRS通信异步还是同步通信好点啊?
搞了几天,还请指点一下o。。。

#76楼   回复  引用    

2008-11-21 16:54 by Ethan[未注册用户]
我是要在WinCE上实现串口通信,用了你的例子,mycom类用了之后,在PDA
上调试之后 提示:无法找到PInvoke DLL "Comm.dll" 。
这个是什么问题啊,
我download了个comm.dll文件还不行?怎么回事啊?
我的Email:caishijin1@163.com
MSN: kingcai@hotmail.com

#77楼   回复  引用    

2008-11-27 21:32 by 穷光蛋[未注册用户]
@外城
这个问题我也发现了,害我浪费了不少开发时间,!!我想问题在于mycom类里,当你设置数据位和停止位时没有用,也就是说mycom类实例化时没有对串口初始化一样,我用了串口调试帮手一打开,再关掉,再用这个C#的就正常了,要是在串口调试帮手里把数据位和停止位改掉再用这个C#代码的就会出错,那个mycom像是外国人写的吧!!!

#78楼   回复  引用    

2008-11-28 14:55 by jasion841[未注册用户]
你好 我看了 你的串口通信测试中的 关于串口写方面的内容,你的 好像多调用了一个Comm.dll 中的 Init_Comm() ,我碰到的原因是 串口第一次初始化不成功,但如果用超级中端打开过后,再初始化 就好了 ,所以想看看你的Comm.dll中 写了什么 不知道可不可以发我 一份dll的源码,谢谢 邮箱:jackal841@163.com

#79楼   回复  引用    

2009-02-06 13:49 by keremery[未注册用户]
mycom1.BytesToRead的return值是0,这是为什么呀

#80楼   回复  引用    

2009-02-11 09:43 by 水乡一色[未注册用户]
我把环境建立好了。运行也可以了。但是我下发一个
7E 04 01 25 DB 01的指令下去硬件却没有资料反馈。

问题在下面:
我从网上下载了一个串口测试的工具,执行一次,然后再下
7E 04 01 25 DB 01
时就会有数据上传。
不知道是什么原因,所以想问一下楼主。是我的波特率设定问题还是有什么其它问题。比较急。先谢过!!

#81楼   回复  引用    

2009-02-19 11:40 by 水乡一色[未注册用户]
上述的串口通讯方式,只要采用普通的232-485转换器就可以正常的通讯了!
大家可以试一下!

#82楼   回复  引用    

2009-04-16 12:55 by 2World
想请问下 c++应该怎么样编译dll呢?
因为硬件提供的是c++ 头文件 自己是用c#做开发的 所以要引用头文件比较麻烦 劳烦各位帮忙下了 谢谢

#83楼   回复  引用    

2009-04-18 09:38 by cww4167[未注册用户]
您好,能发我一份vs2005做的一个整份的项目文件吗?要串口程序类,老师叫我在网上找个程序看懂,我一直都找不到,还有一天时间就要交差了,哎,帮帮忙啊,谢谢了邮箱cww4167@126.com
还有个问题为什么我在我的VS2005中编写串口程序时输入的一切关于serialPort运行时怎么出现错误,错误为当前上下文中不存在名称“serialPort1” D:\My Documents\Visual Studio 2005\Projects\WindowsApplication2\WindowsApplication2\Form1.cs 为什么啊,谢谢

#84楼   回复  引用    

2009-04-23 13:42 by abigail[未注册用户]
找不到你所说的提供下载地址的地方,能否将这篇源码发一份给我,谢谢。
abigail_yu@sina.com

#85楼   回复  引用    

2009-04-30 11:40 by heyjie123[未注册用户]
能否给发下mycom类,邮箱:heyjie123@163.com
先谢过!

#86楼   回复  引用    

2009-05-10 13:24 by #86楼[未注册用户]
小Y你好,我是个大四的学生,老师叫我做两台计算机间的串口通讯程序.开发环境是VS2008用C#,我都不会,你能给我发一份你做的相关程序吗
moon_19840815@126.com

#87楼   回复  引用    

2009-05-11 20:14 by weixiaohaifeng[未注册用户]
找不到你所说的提供下载地址的地方,能否将这篇源码发一份给我,谢谢。

#88楼   回复  引用    

2009-05-27 05:24 by Min20171[未注册用户]
能否将这篇源码发一份给我,谢谢。
min20171@hotmail.com
vs2008

#89楼   回复  引用    

2009-05-29 01:42 by fanxc
ding .

#90楼   回复  引用    

2009-06-09 14:24 by wmjcom[未注册用户]
找不到源代码,请发给我一份:wmjcom@163.com,谢谢。



发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 517882




相关文章:

相关链接: