110,536
社区成员
发帖
与我相关
我的任务
分享
public static string InitSerialPort()
{
port1.PortName = “com3”
port1.BaudRate = 19200;//波特率
port1.DataBits = 8; //数据位
port1.Parity = Parity.None;//无奇偶校验位
port1.StopBits = StopBits.One; //一个停止位
port1.ReadBufferSize = 40; //接收缓冲区大小
//port1.Handshake = Handshake.RequestToSend;//控制协议
port1.Encoding = Encoding.BigEndianUnicode;//设置编码格式
port1.ReceivedBytesThreshold = 1;//设置内部输入缓冲区的字节数
if (port1.IsOpen)
{
port1.Close();
}
try
{
port1.Open();
}
catch
{
return "请打开设备!";
}
return null;
}
public static Glud gludess
{
byte[] bytesData = new byte[]{};
if (port1.BytesToRead > 0)
{
byte firstByte = Convert.ToByte(port1.ReadByte());
int bytesRead = port1.BytesToRead;
bytesData = new byte[bytesRead + 1];
bytesData[0] = firstByte;
for (int i = 1; i <= bytesRead; i++)
{
bytesData[i] = Convert.ToByte(port1.ReadByte());
}
}
}
private void button3_Click(object sender, EventArgs e)
{
var str =GetBloodGlu.InitSerialPort();
System.Threading.Thread.Sleep(2000)
if (string.IsNullOrEmpty(str))
{
var glu = GetBloodGlu.gludess();
lblglu.Text = glu.value;
}
else
{
MessageBox.Show("请打开机器!");
}
}
}
这是点击得到数据的代码,最上面的是是里面的两个方法
glu是个model里面只有userno、value