C# 在C中格式化串行输入到文本框#
我试图从Toledo 8510电子秤中获取重量,并将其显示在文本框中,在文本框中它将被捕获并发送到SQL Server数据库。我已经把几个源代码混合在一起,得到了在文本框中进行通信和显示输出的比例,但是比例显示为5.65,文本框显示56500000。当秤上没有重量时,显示0。量表的输出是ASCII格式的,当我使用Code项目的SerialPortListener或Putty时,输出是这样的:您有量表的文档吗?您应该阅读。小数点的位置是可变的,因此需要读取“状态字A”;此外,电子秤可以报告磅或千克;你可能想检查状态字B来考虑这一点。显然不是你眼前的问题,但你是否需要考虑SerialDataEventArgs可能有一个或多个读数的一部分?您可能需要在重新组装的流中查找STX。感谢链接手册。这本书比我那本18岁的硬拷贝容易读!我会告诉你事情的进展。C# 在C中格式化串行输入到文本框#,c#,visual-studio,formatting,serial-port,ascii,C#,Visual Studio,Formatting,Serial Port,Ascii,我试图从Toledo 8510电子秤中获取重量,并将其显示在文本框中,在文本框中它将被捕获并发送到SQL Server数据库。我已经把几个源代码混合在一起,得到了在文本框中进行通信和显示输出的比例,但是比例显示为5.65,文本框显示56500000。当秤上没有重量时,显示0。量表的输出是ASCII格式的,当我使用Code项目的SerialPortListener或Putty时,输出是这样的:您有量表的文档吗?您应该阅读。小数点的位置是可变的,因此需要读取“状态字A”;此外,电子秤可以报告磅或千克
private void UserInitialization()
{
_spManager = new SerialPortManager();
SerialSettings mySerialSettings = _spManager.CurrentSerialSettings;
serialSettingsBindingSource.DataSource = mySerialSettings;
ComPrtDrpDnBx.DataSource = mySerialSettings.PortNameCollection;
BaudRtDrpdnBx.DataSource = mySerialSettings.BaudRateCollection;
DataBtsDrpDnBx.DataSource = mySerialSettings.DataBitsCollection;
ParityDrpDnBx.DataSource = Enum.GetValues(typeof(System.IO.Ports.Parity));
StpBtsDrpDnBx.DataSource = Enum.GetValues(typeof(System.IO.Ports.StopBits));
_spManager.NewSerialDataRecieved += new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved);
//this.FormClosing += new FormClosingEventHandler(WeighInFrm_FormClosing);
}
private void WeighInForm_FormClosing(object sender, FormClosingEventArgs e)
{
_spManager.Dispose();
}
void _spManager_NewSerialDataRecieved(object sender, SerialDataEventArgs e)
{
if (this.InvokeRequired)
{
// Using this.Invoke causes deadlock when closing serial port, and BeginInvoke is good practice anyway.
this.BeginInvoke(new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved), new object[] { sender, e });
return;
}
int maxTextLength = 9; // maximum text length in text box
if (CapWeghtTxtBx.TextLength > maxTextLength)
CapWeghtTxtBx.Text = CapWeghtTxtBx.Text.Remove(0, CapWeghtTxtBx.TextLength - maxTextLength);
// This application is connected to a Scale sending ASCII characters, so data is converted to text
string str = Encoding.ASCII.GetString(e.Data);
string newString = Regex.Replace(str, "[^.0-9]", ""); //Remove the Caret symbol and any spaces
CapWeghtTxtBx.AppendText(newString);
CapWeghtTxtBx.Select(CapWeghtTxtBx.TextLength, 0);
CapWeghtTxtBx.ScrollToCaret();
CapWeghtTxtBx.Text = string.Format("{0:0}", double.Parse(CapWeghtTxtBx.Text)); //formats the text
}
private void CapWeghtTxtBx_TextChanged(object sender, EventArgs e)
{
}