Как решить проблему при преобразовании строковых данных, полученных от arduino, в целочисленные или плавающие
я использую arduino для последовательной отправки данных на компьютер в приложении c# windows form, которое я использовал для чтения с помощью инструмента Gauge. для этого датчика ему нужно дать значение, поэтому я попытался преобразовать строковые данные в целое число, но это дает мне ошибку при преобразовании этих данных в целое число. пожалуйста, дайте мне решение.......
ошибка заключается в следующем ----
System.FormatException was unhandled HResult=-2146233033 Message=Input string was not in a correct format. Source=mscorlib StackTrace: at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.Convert.ToInt32(String value) at WindowsFormsApplication3.Form1.button1_Click(Object sender, EventArgs e) in H:\Users\kulde\documents\visual studio 2015\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Form1.cs:line 36 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at WindowsFormsApplication3.Program.Main() in H:\Users\kulde\documents\visual studio 2015\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Program.cs:line 19 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:
Что я уже пробовал:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO.Ports; namespace WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void aGauge1_ValueInRangeChanged(object sender, ValueInRangeChangedEventArgs e) { } private void button1_Click(object sender, EventArgs e) { SerialPort port = new SerialPort(); port.BaudRate = 9600; port.PortName = "COM3"; port.Open(); while (true) { string kd =port.ReadLine(); textBox1.Text = kd; int kk = Convert.ToInt32(kd); this.aGauge1.Value = kk; } } } }
Dave Kreskowiak
Ошибка говорит вам, что то, что вы пытались разобрать, на самом деле не было числом. Поскольку мы не знаем, что это была за строка, которую вы пытались преобразовать, мы действительно не могли сказать вам, что с ней не так.
[no name]
Зачем вы вообще посылаете строку? Вы должны отправить целое число как целое число по последовательному соединению. Вы только что нашли новый способ создать проблему без веской причины.