我正试图从Toledo 8510比例中获得重量,以便在文本框中显示,然后将其捕获并发送到SQL Server数据库。我已将来自几个来源的代码混合在一起,并且已经获得了在文本框中进行通信和显示输出的比例,但是在比例显示为5.65的情况下,文本框显示为56500000.当比例上没有重量时,它显示0。比例的输出是ASCII,当我使用Code Project或Putty的SerialPortListener时,输出如下所示:<' 00565 00000.我的解决方案中有代码删除<'和空格所以我确定最后五个零是ascii字符串的后半部分。
这是为了对当地慈善机构举办的钓鱼锦标赛进行评分,我们买不起专业人士,所以我正在学习编码。任何帮助将不胜感激。我的代码如下:
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)
{
}