System.IO.Ports.SerialPort 应用-没完成
来源:互联网 发布:手机开淘宝店流程步骤 编辑:程序博客网 时间:2024/05/17 01:27
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
namespace SerialTest
{
public class NM4BPort
{
private SerialPort sp;
public NM4BPort(string portName)
{
sp = new SerialPort(portName, 115200, Parity.Space, 8);
sp.ReadTimeout = 5000;
sp.WriteTimeout = 5000;
}
public void Open()
{
if(sp != null)
sp.Open();
}
public void Close()
{
if (sp != null)
sp.Close();
}
private void SendCommandID(byte chCMD)
{
WriteByte(0x3F);
WriteByte(chCMD);
ReadByte();
ReadByte();
}
private byte ReadByte()
{
byte ret = (byte)sp.ReadByte();
byte[] buf = { 0x2E };
sp.Write(buf,0, 1);
return ret;
}
private bool WriteByte(byte dat)
{
byte[] buf = { dat };
sp.Write(buf, 0, 1);
byte ret = (byte)sp.ReadByte();
return (ret == (byte)0x2E);
}
private int ReadInt()
{
int ret = 0;
ReadByte();
for(int i=0; i<4; i++)
ret = (ret << 8) + ReadByte();
return ret;
}
private string ReadString(int nMax)
{
byte[] buf = new byte[nMax];
ReadByte();
for(int i=0; i<nMax; i++)
buf[i] = ReadByte();
ASCIIEncoding ascii = new ASCIIEncoding();
string decoded = ascii.GetString(buf);
return decoded;
}
public string GetDriverNames()
{
SendCommandID(0x03);
int nLen = ReadInt();
return ReadString(nLen);
}
}
class Program
{
static void Main(string[] args)
{
NM4BPort port = new NM4BPort("COM2");
try
{
port.Open();
string driverNames = port.GetDriverNames();
Console.WriteLine(driverNames);
port.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
port.Close();
Console.ReadKey();
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
namespace SerialTest
{
public class NM4BPort
{
private SerialPort sp;
public NM4BPort(string portName)
{
sp = new SerialPort(portName, 115200, Parity.Space, 8);
sp.ReadTimeout = 5000;
sp.WriteTimeout = 5000;
}
public void Open()
{
if(sp != null)
sp.Open();
}
public void Close()
{
if (sp != null)
sp.Close();
}
private void SendCommandID(byte chCMD)
{
WriteByte(0x3F);
WriteByte(chCMD);
ReadByte();
ReadByte();
}
private byte ReadByte()
{
byte ret = (byte)sp.ReadByte();
byte[] buf = { 0x2E };
sp.Write(buf,0, 1);
return ret;
}
private bool WriteByte(byte dat)
{
byte[] buf = { dat };
sp.Write(buf, 0, 1);
byte ret = (byte)sp.ReadByte();
return (ret == (byte)0x2E);
}
private int ReadInt()
{
int ret = 0;
ReadByte();
for(int i=0; i<4; i++)
ret = (ret << 8) + ReadByte();
return ret;
}
private string ReadString(int nMax)
{
byte[] buf = new byte[nMax];
ReadByte();
for(int i=0; i<nMax; i++)
buf[i] = ReadByte();
ASCIIEncoding ascii = new ASCIIEncoding();
string decoded = ascii.GetString(buf);
return decoded;
}
public string GetDriverNames()
{
SendCommandID(0x03);
int nLen = ReadInt();
return ReadString(nLen);
}
}
class Program
{
static void Main(string[] args)
{
NM4BPort port = new NM4BPort("COM2");
try
{
port.Open();
string driverNames = port.GetDriverNames();
Console.WriteLine(driverNames);
port.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
port.Close();
Console.ReadKey();
}
}
}
}
- System.IO.Ports.SerialPort 应用-没完成
- System.IO.Ports.SerialPort
- VB.net学习笔记之串口通讯:System.IO.Ports.SerialPort
- 操作串口通信类(IO.Ports)-SerialPort
- VS_WinForm_IO.Ports.SerialPort串口通信初步程序代码
- 想在Unity中写串口程序,发现找不到System.IO.Ports
- Unity3d——报错 The type or namespace name `Ports' does not exist in the namespace `System.IO'
- 关于IO ports和IO memory
- libxml2 没安装导致php5无法ports
- I/O Completion Ports(完成端口)
- system.io
- serialPort
- SerialPort
- SerialPort
- 没完成的东西
- I/O Completion Ports-I/O完成端口
- I/O Completion Ports-I/O完成端口
- VS2010 + QT5 SerialPort 串口应用实例总结
- Memory Hacking Software MemHack 3.0.1.3 Pro by L. Spiro
- 电脑报编程工具调查爬行榜
- 视口与映射
- 唯一让盖茨晕倒的中国人
- Java Web应用中任务调度解析
- System.IO.Ports.SerialPort 应用-没完成
- C#压缩Access数据库
- 取第一块硬盘序列号
- 使用 WebFlowEditor 开发SWF应用[摘]
- 分区域滚动窗口
- 类加载的三种方式
- oracle学习大全(good)
- oracle存储过程加密如何解密?
- getURL函数的使用方法指南