查看: 1577|回复: 0
打印 上一主题 下一主题

串口通信简单程式

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-2-1 14:08:44 |只看该作者 |倒序浏览
串口通信简单程式 使用JH.CommBase实现 SCOMM.cs文件 //说明:PackInfo是我定义的一个协议包;把CommBase.cs文件中的open函数加了保存配置文件"configscomm.xml" //CRingBuff是自定义的一个缓冲池 //超过50000字节了,只能作为附件上传 注意:这只是简单例子 -------------------------------------------------------------------------------- 补充说明:如果你使用了BaseTerm***工具生成了jhc配置文件,请删除 false

false
NULL 0 类CommBaseSettings中没有这几个成员,load Xml会失败 -------------------------------------------------------------------------------- using UnityEngine; using System; using System.Collections; using System.IO; using System.IO.Ports; using System.Threading; using JH.CommBase; namespace nsCOMM { //by gotoxy public class CRingBuff { private int MaxSize = 4 * 1024; private byte[] RingBuff; private int in_ptr; private int out_ptr; public CRingBuff() { RingBuff = new byte[MaxSize]; in_ptr = 0; out_ptr = 0; } public CRingBuff(int size) { MaxSize=size; RingBuff = new byte[MaxSize]; in_ptr = 0; out_ptr = 0; } public bool InQueue(byte b) { if ((in_ptr + 1) % MaxSize == out_ptr) return false;//满 RingBuff[in_ptr] = b; in_ptr++; if (in_ptr >= MaxSize) in_ptr = 0; return ***e; } public bool OutQueue(out byte outbyte) { outbyte = 0; if (in_ptr == out_ptr) return false;//空 outbyte = RingBuff[out_ptr]; out_ptr++; if (out_ptr >= MaxSize) out_ptr = 0; return ***e; } public bool GetTop(out byte outbyte) { outbyte = 0; if (in_ptr == out_ptr) return false;//空 outbyte = RingBuff[in_ptr]; return ***e; } public bool IsFull() { if ((in_ptr + 1) % MaxSize == out_ptr) return ***e;//满 else return false; } public bool IsEmpty() { if (in_ptr == out_ptr) return ***e;//空 else return false; } public void ClearRing() { in_ptr = 0; out_ptr = 0; } public int RingSize() { return (in_ptr + MaxSize - out_ptr) % MaxSize; } public int RingDefaultSize() { return MaxSize; } } public class SCOMMBASE : CommBase { public int HeadCouter = 0; public int TailCouter = 0; public CRingBuff RevRingBuff = new CRingBuff(); protected override CommBaseSettings CommSettings() { //string AppPath = Application.dataPath + "/configscomm.xml"; string AppPath = "configscomm.xml"; FileInfo f = new FileInfo(AppPath); if (f.Exists) { FileStream fs = f.OpenRead(); CommBaseSettings cs = CommBaseSettings.LoadFromXML(fs); fs.Close(); return cs; } else { CommBaseSettings cs = new CommBaseSettings(); cs.SetStandard("COM1", 9600, Handshake.none); return cs; } } public bool IsOpen { get { return Online; } } protected override void OnRxChar(byte b) { if (RevRingBuff.InQueue(b))//入缓冲池 { if (b == PackInfo.SYNC_HEAD) HeadCouter++;//PackInfo:自定义的协议包,忽略 else if (b == PackInfo.SYNC_TAIL) TailCouter++; } } public bool Write(byte[] Buff, int offset, int size) { byte[] temp = new byte[size]; for (int i = 0; i < size; i++) { temp = Buff[offset + i]; } Send(temp); return ***e; } } public class SCOMM { public bool ready = false; public SCOMMBASE sp; public SCOMM() { sp = new SCOMMBASE(); sp.Open(); if (!sp.IsOpen) { throw (new ApplicationException("打开串口失败")); } ready = ***e; } public bool IsOpen { get { return sp.IsOpen; } } public void Open() { if (!sp.IsOpen) { sp.Open(); } } public void Close() { if (sp.IsOpen) { sp.Close(); } } public void Exit() { //其他... if (sp.IsOpen) { sp.Close(); } } } }





转载 unity3d中文学习社区
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-5 20:02 , Processed in 0.083128 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部