using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace ReadData
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//MSComm1.PortOpen = false;
}
public string WeightStr;
public byte[] bytReceiveByte; //接收到的字节
public int intReceiveLen; //接收到的字节数
public int intHexWidth; //显示列数(电子秤的输出位数小循环)
public string strAscii;
//电子秤输出的ASCII码 private void btn_start_Click(object sender, EventArgs e)
{
readweighta9();
} private void readweighta9()
{
//串口参数初始化
try
{
MSComm1.InputLen = 0;
MSComm1.CommPort = 1;//1;
MSComm1.Settings = "4800,n,8,1"; //";
MSComm1.InputMode = MSCommLib.InputModeConstants.comInputModeBinary; MSComm1.RThreshold = 480;
MSComm1.SThreshold = 1;
MSComm1.InBufferCount = 0; MSComm1.InBufferSize = 1024;
MSComm1.OutBufferSize = 512;
intHexWidth = 9; //电子秤输出为9位的循环
if (MSComm1.PortOpen == false)
{
MSComm1.PortOpen = true;
}
}catch(Exception e){
throw e;
MessageBox.Show("ddddddddd");
}
}
public string GetWeightA9(string RedData)
{
int FFirst, FSecond;
string OriStr1, OriStr2; if (RedData == "")
{
return ("0.000");
}
FFirst = RedData.IndexOf("+", 1);
FSecond = RedData.IndexOf("+", FFirst + 1);
// Fthird = RedData.IndexOf("+", FSecond + 1); if (FFirst < FSecond)
{ OriStr1 = RedData.Substring(FFirst + 1, 6);
OriStr2 = RedData.Substring(FSecond + 1, 6);
if (OriStr1.Equals(OriStr2))
{
if (OriStr1.Equals("000000") && OriStr2.Equals("000000"))
{
return ("0.000");
}
else
{
return ((Convert.ToString(Convert.ToDouble(OriStr1) / 1000)));
}
} else
{
return ("0.000");
}
}
else
{
return ("0.000");
}
}
private void btn_end_Click(object sender, EventArgs e)
{
if (MSComm1.PortOpen == true) {
MSComm1.PortOpen = false;
lbl_state.Text = "停止";
}
} private void MSComm1_OnComm(object sender, EventArgs e)
{
byte[] bytInput;
object objInput;
int intInputLen; if (MSComm1.PortOpen == false)
{
MSComm1.PortOpen = true;
}
if (MSComm1.CommEvent == 2)
{
lbl_state.Text = "...正 常...";
}
else
{
lbl_state.Text = "...测试中...";
} //此处添加处理接收的代码-
//以二进制方式读取串口值!
MSComm1.InputMode = MSCommLib.InputModeConstants.comInputModeBinary;
intInputLen = MSComm1.InBufferCount;
bytInput = new byte[intInputLen];
objInput = MSComm1.Input;//这里注意axMSComm1.Input返回的是一个object的
bytInput = (byte[])objInput;//类型,所以必须使用显式的类型转换,这点和VB不同
InputManage(bytInput, intInputLen);
GetDisplayText();
} //'**********************************
//'输入处理
//'处理接收到的字节流,并保存在全局变量
//'bytReceiveRyte()
//'**********************************
public void InputManage(byte[] bytInput, int intInputLenth)
{
int n; //定义变量及初始化 // ReDim Preserve bytReceiveByte(intReceiveLen + intInputLenth)
bytReceiveByte = new byte[intReceiveLen + intInputLenth];
for (n = 0; n <= intInputLenth - 1; n++)
{
bytReceiveByte[intReceiveLen + n] = bytInput[n];
}
intReceiveLen = intReceiveLen + intInputLenth;
} //'***********************************
//'为输出准备文本
//'保存在全局变量
//'strText
//'总行数保存在
//'intLine
//'***********************************
public void GetDisplayText()
{
int n, intValue, intHighHex, intLowHex;
string strSingleChr;
strAscii = ""; //设置初值 //'*****************************************
//'获得16进制码和ASCII码的字符串
//'*****************************************
for (n = 0; n <= intReceiveLen - 1; n++)
{
intValue = Convert.ToInt32(bytReceiveByte[n]); if (intValue < 32 || intValue > 128)
{//处理非法字符
strSingleChr = Convert.ToString((char)46);
}//对于不能显示的ASCII码,
else
{//用"."表示
strSingleChr = Convert.ToString((char)intValue);
}
strAscii = strAscii + strSingleChr; intHighHex = intValue / 16;
intLowHex = intValue - intHighHex * 16; if (intHighHex < 10)
{
intHighHex = intHighHex + 48;
}
else
{
intHighHex = intHighHex + 55;
}
if (intLowHex < 10)
{
intLowHex = intLowHex + 48;
}
else
{
intLowHex = intLowHex + 55;
} if ((n % intHexWidth) == 0)
{
if ((strAscii.Length) >= 60)
{
WeightStr = strAscii.Substring(strAscii.Length - 30, 30);
lbl_data.Text = GetWeightA9(WeightStr); }
}
}
if (intReceiveLen > 524)
{
ClearWeight();
}
}
public void ClearWeight()
{
byte[] bytTemp;
bytTemp = new byte[0];
bytReceiveByte = new byte[0];
intReceiveLen = 0;
InputManage(bytTemp, 0); }从仪表上读取数据正确就是过一会会死机
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace ReadData
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//MSComm1.PortOpen = false;
}
public string WeightStr;
public byte[] bytReceiveByte; //接收到的字节
public int intReceiveLen; //接收到的字节数
public int intHexWidth; //显示列数(电子秤的输出位数小循环)
public string strAscii;
//电子秤输出的ASCII码 private void btn_start_Click(object sender, EventArgs e)
{
readweighta9();
} private void readweighta9()
{
//串口参数初始化
try
{
MSComm1.InputLen = 0;
MSComm1.CommPort = 1;//1;
MSComm1.Settings = "4800,n,8,1"; //";
MSComm1.InputMode = MSCommLib.InputModeConstants.comInputModeBinary; MSComm1.RThreshold = 480;
MSComm1.SThreshold = 1;
MSComm1.InBufferCount = 0; MSComm1.InBufferSize = 1024;
MSComm1.OutBufferSize = 512;
intHexWidth = 9; //电子秤输出为9位的循环
if (MSComm1.PortOpen == false)
{
MSComm1.PortOpen = true;
}
}catch(Exception e){
throw e;
MessageBox.Show("ddddddddd");
}
}
public string GetWeightA9(string RedData)
{
int FFirst, FSecond;
string OriStr1, OriStr2; if (RedData == "")
{
return ("0.000");
}
FFirst = RedData.IndexOf("+", 1);
FSecond = RedData.IndexOf("+", FFirst + 1);
// Fthird = RedData.IndexOf("+", FSecond + 1); if (FFirst < FSecond)
{ OriStr1 = RedData.Substring(FFirst + 1, 6);
OriStr2 = RedData.Substring(FSecond + 1, 6);
if (OriStr1.Equals(OriStr2))
{
if (OriStr1.Equals("000000") && OriStr2.Equals("000000"))
{
return ("0.000");
}
else
{
return ((Convert.ToString(Convert.ToDouble(OriStr1) / 1000)));
}
} else
{
return ("0.000");
}
}
else
{
return ("0.000");
}
}
private void btn_end_Click(object sender, EventArgs e)
{
if (MSComm1.PortOpen == true) {
MSComm1.PortOpen = false;
lbl_state.Text = "停止";
}
} private void MSComm1_OnComm(object sender, EventArgs e)
{
byte[] bytInput;
object objInput;
int intInputLen; if (MSComm1.PortOpen == false)
{
MSComm1.PortOpen = true;
}
if (MSComm1.CommEvent == 2)
{
lbl_state.Text = "...正 常...";
}
else
{
lbl_state.Text = "...测试中...";
} //此处添加处理接收的代码-
//以二进制方式读取串口值!
MSComm1.InputMode = MSCommLib.InputModeConstants.comInputModeBinary;
intInputLen = MSComm1.InBufferCount;
bytInput = new byte[intInputLen];
objInput = MSComm1.Input;//这里注意axMSComm1.Input返回的是一个object的
bytInput = (byte[])objInput;//类型,所以必须使用显式的类型转换,这点和VB不同
InputManage(bytInput, intInputLen);
GetDisplayText();
} //'**********************************
//'输入处理
//'处理接收到的字节流,并保存在全局变量
//'bytReceiveRyte()
//'**********************************
public void InputManage(byte[] bytInput, int intInputLenth)
{
int n; //定义变量及初始化 // ReDim Preserve bytReceiveByte(intReceiveLen + intInputLenth)
bytReceiveByte = new byte[intReceiveLen + intInputLenth];
for (n = 0; n <= intInputLenth - 1; n++)
{
bytReceiveByte[intReceiveLen + n] = bytInput[n];
}
intReceiveLen = intReceiveLen + intInputLenth;
} //'***********************************
//'为输出准备文本
//'保存在全局变量
//'strText
//'总行数保存在
//'intLine
//'***********************************
public void GetDisplayText()
{
int n, intValue, intHighHex, intLowHex;
string strSingleChr;
strAscii = ""; //设置初值 //'*****************************************
//'获得16进制码和ASCII码的字符串
//'*****************************************
for (n = 0; n <= intReceiveLen - 1; n++)
{
intValue = Convert.ToInt32(bytReceiveByte[n]); if (intValue < 32 || intValue > 128)
{//处理非法字符
strSingleChr = Convert.ToString((char)46);
}//对于不能显示的ASCII码,
else
{//用"."表示
strSingleChr = Convert.ToString((char)intValue);
}
strAscii = strAscii + strSingleChr; intHighHex = intValue / 16;
intLowHex = intValue - intHighHex * 16; if (intHighHex < 10)
{
intHighHex = intHighHex + 48;
}
else
{
intHighHex = intHighHex + 55;
}
if (intLowHex < 10)
{
intLowHex = intLowHex + 48;
}
else
{
intLowHex = intLowHex + 55;
} if ((n % intHexWidth) == 0)
{
if ((strAscii.Length) >= 60)
{
WeightStr = strAscii.Substring(strAscii.Length - 30, 30);
lbl_data.Text = GetWeightA9(WeightStr); }
}
}
if (intReceiveLen > 524)
{
ClearWeight();
}
}
public void ClearWeight()
{
byte[] bytTemp;
bytTemp = new byte[0];
bytReceiveByte = new byte[0];
intReceiveLen = 0;
InputManage(bytTemp, 0); }从仪表上读取数据正确就是过一会会死机
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货