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 编辑 ]