#region Receiving
public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int bytes = serial.BytesToRead;
byte[] buffer = new byte[bytes];
serial.Read(buffer, 0, bytes);
int length = buffer.Length;
for (int i = 0; i < length; i += 8)
{
if (length - i >= 8)
{
definition.buffering = BitConverter.ToInt64(buffer, i);
Console.Write(definition.buffering.ToString());
//Int64 val = BitConverter.ToInt64(buffer, i);
foreach (var item in buffer)
{
SQLiteConnection sqliteCon = new SQLiteConnection(dBConnectionString);
//open connection to database
try
{
string itemcode = item.ToString();
sqliteCon.Open();
string Query = "insert into EmployeeList (empID,Name,surname,Age) values('" + itemcode + "','" + itemcode + "','" + itemcode + "','" + itemcode + "')";
SQLiteCommand createCommand = new SQLiteCommand(Query, sqliteCon);
createCommand.ExecuteNonQuery();
MessageBox.Show("Saved");
sqliteCon.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Console.Write(item.ToString());
}
}
else
{
// invalid (last) part.
}
}
//definition.buffering = BitConverter.ToInt64(buffer, 0);
Console.WriteLine();
Console.WriteLine(definition.buffering);
Console.WriteLine();
Thread thread = new Thread(new ThreadStart(() => ThreadExample.ThreadJob(this)));
thread.Start();
thread.Join();
}
#endregion
编辑一个。我试着发送0xFF,但是这个代码在控制台中的显示是0。纠正这段代码的错误
嗨。我有这样的代码并执行后,我有一个问题,关于这一点:
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Destination array is not long enough to copy all the items in the collection. Check array index and length.
,它是在这条线一点:
definition.buffering = BitConverter.ToInt64(buffer, 0);
我该如何解决这个问题?
错误听起来很翔实给我。 '缓冲区'的长度是多少? –
因此,员工的名字只是字节缓冲区中的一个字节?你确定要迭代通过串口收到的所有字节吗? –