一、因为我这笔记本没有串口,首先讲个软件,VSPM
介绍:[引用自:http://developer.51cto.com/art/200908/146713.htm]
——————————————————————————————————————————————
由于手上没有串口硬件,所以在网上找了一个 VSPM 虚拟串口软件 ,据说是免费的。下载地址:http://www.skycn.com/soft/30517.html
刚开始也不知道怎么用,摸索了一下,终于成功。
安装以后工作模式选择应该为:VSPM运行在Server模式,支持Client模式设备然后是使用设备探测器简历虚拟串口。(如果选择 建立默认的串口,软件就会自动帮你建立四个虚拟串口的)之后就是新增虚拟串口串口选COM2,选择监听的IP地址:192.168.1.111 ,
Client模式的设备尝试连接此端口:8099建立之后:选择此串口,再选择菜单栏:
检查此设备连接,输入刚才新增时候选择的IP地址:192.168.1.111,刚才选择的端口:
8099点击重新连接,这个窗口没有什么反映:
但是主窗口的串口最右边一栏:最后一次操作:
显示串口:COM2没有打开。
——————————————————————————————————————————————
新建工程,右击“添加引用”》.NET标签下“Microsoft.VisualBasic”
Form1.cs[设计]
Form1.cs
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;
namespace Windowstest
{
public partial class Form1 : Form
{
System.IO.Ports.SerialPort com;
public Form1()
{
InitializeComponent();
Myinit();
}
void Myinit()
{
Microsoft.VisualBasic.Devices.Computer pc = new Microsoft.VisualBasic.Devices.Computer();
com = new System.IO.Ports.SerialPort("COM2");
com.Open();
}
private void button1_Click(object sender, EventArgs e)
{
/* 向串口写入在文本框的数据 */
com.WriteLine(textBox1.Text);
}
}
}
保存调试运行
出现错误:Visual Studio 无法开始调试。请生成项目并重试,或者相应地设置 OutputPath 和 AssemblyName 属性,使其指向目标程序集的正确位置。
右击工程》“清理”》“重新生成”
出现错误:找不到“COM2”
别急,是虚拟串口没配好
PS:如果你是台式机有串口,直接设置将COM2改成COM1,就可以下一步了;
三、运行
设置VSPM
选中COM2端口,“虚拟串口及设备管理”》“跟踪此串口数据”
运行小程序,输入hello
点击“显示”
OK,串口发送数据就成功了。全文完,接收数据出了点问题,就先不写了>_^
参考文章:
http://cao416451347ming.blog.163.com/blog/static/1154556162011374393975/
http://developer.51cto.com/art/200908/146713.htm