| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1154 人关注过本帖
标题:VB里的MSComm和C#的serialPort
只看楼主 加入收藏
黄昏の腕轮
Rank: 2
等 级:论坛游民
帖 子:64
专家分:12
注 册:2009-6-13
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:2 
VB里的MSComm和C#的serialPort
下面这段VB中的程序,换成C#中的serialPort该怎么写才好呢?我下面自己也写了个,不过效果好像不太一样。。
VB:
Dim Out(100) As Byte
Dim var As Variant
Dim ii As Integer
Public Function Comm()
    With MSC
        .CommPort = 5
        .Settings = "9600,n,8,1"
        .InBufferSize = 40
        .InputLen = 1
        .RThreshold = 1
        .InputMode = comInputModeBinary
        .InBufferCount = 0
        If Not .PortOpen Then
            .PortOpen = True
        End If
        .Output = "~HS"
        var = Null
        var = .Input
        DelayTime   '延时
        For ii = 2 To 81
            var = Null
            var = .Input
            Out(ii) = var(0)
            Tcm.Text = Tcm.Text & Chr(Out(ii))
        Next
        .RThreshold = 1
        .PortOpen = False
    End With
    num_s = right(Tcm.Text, 27)
    num_s = left(num_s, 1)
    Tcm.Text = ""
End Function

C#:
private void Comm()
{
    byte[] DataRead;
    int bytData;
    int bytnum;
    //serialPort.BaudRate = 9600;
    //serialPort.DataBits =8;
    //serialPort.PortName = "COM5";
    //serialPort.ReadBufferSize = 1024;
    //serialPort.WriteBufferSize = 1024;
    //serialPort.WriteTimeout = 200;
    serialPort.Close();
    serialPort.Open();
    serialPort.ReceivedBytesThreshold = 1;
    serialPort.DiscardInBuffer();
    serialPort.DiscardOutBuffer();
    string h = "~HS";
    Byte[] buff = System.Text.Encoding.Default.GetBytes(h);
    serialPort.Write(buff, 0, 3);
    Thread.Sleep(500);
    bytnum = serialPort.BytesToRead;
    DataRead = new byte[bytnum];
    for (int i = 2; i < 82; i++)
    {
        bytData = serialPort.ReadByte();
        DataRead[i] = (byte)bytData;
        Tcm.Text += DataRead[i];
    }
    serialPort.Close();
    int = Tcm.Text.Length;
    num_s = num_s.Substring(t - 27, 27);
    num_s = num_s.Substring(0, 1);
    Tcm.Text = "";
}
大家帮我看看,应该怎么改?

[ 本帖最后由 黄昏の腕轮 于 2010-7-17 11:15 编辑 ]
搜索更多相关主题的帖子: MSComm serialPort 
2010-07-16 21:55
黄昏の腕轮
Rank: 2
等 级:论坛游民
帖 子:64
专家分:12
注 册:2009-6-13
收藏
得分:0 
没有人帮我看看吗
2010-07-17 21:00
hainandgm
Rank: 6Rank: 6
等 级:侠之大者
威 望:3
帖 子:111
专家分:475
注 册:2007-6-21
收藏
得分:10 
采用SerialPort控件的DataReceived事件吧
private List<byte> _ReceiveData = new List<byte>();
private void serialPort_DataReceived(object sender, EventArgs e)
{
ReadAgain:
   while (serialPort.BytesToRead > 0)
       _ReceiveData.Add((byte)serialPort.ReadByte());
   
   Thread.Sleep(10);

   if (serialPort.IsOpen && serialPort.BytesToRead > 0)
      goto ReadAgain;

   Tcm.Invoke(new EventHandler(delegate {
                for(int i=0;i<_ReceiveData.Count;i++)
                     Tcm.Text += _ReceiveData[i];
   }));

   _ReceiveData.Clear;
}

我只写了串口控件的接收数据事件,控件的初始化和数据的处理你来完成吧
2010-07-20 09:01
快速回复:VB里的MSComm和C#的serialPort
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.041757 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved