我在load的时候进行了:
ThreadPool.QueueUserWorkItem(new WaitCallback(SerialReceive), 0); 其中的方法:
public void SerialReceive(Object a)//通过串口取得扫描枪的数据。
{
if (isLoad)
{
Thread.Sleep(800);
}
byte[] buf;
buf = new byte[1];
int bytesRead = 0;
int i;
this.code = "";
while (Receive)
{
if (Serial.Opened)
{
bytesRead = Serial.ReadPort(1, buf);
if (bytesRead > 0)
{
for (i = 0; i < bytesRead; i++)
{
GetDate(Convert.ToChar(buf[i]).ToString());
}
}
}
}
this.isLoad = false;
}
private void GetDate(string a)//通过串口取得扫描枪的数据。进行保存
{
if (this.InvokeRequired)
{
this.Invoke(new GetDateDelegate(GetDate), new object[] { a });
return;
}
this.code += a;
if (a == "\n")
{
this.data = this.code.Substring(0, this.code.Length-2);
//Thread.CurrentThread.Abort();
this.SaveData(this.code);
//Thread.CurrentThread.Start();
}
textBoxReceive.Text += a; }
private void textBoxSend_KeyPress(object sender, KeyPressEventArgs e)//通过usb接口直接输入进行保存
{
if (e.KeyChar == 13)
{
SaveData(this.textBoxSend.Text.Trim());
textBoxReceive.Text += this.textBoxSend.Text.Trim() + "\r\n";
this.textBoxSend.Text = "";
this.textBoxSend.Focus();
}
}
public void SaveData(string msSql)..保存数据
{}
现在问题是我没法取得串口的数据,请问该如何调用呢?串口的端口都正常打开!
ThreadPool.QueueUserWorkItem(new WaitCallback(SerialReceive), 0); 其中的方法:
public void SerialReceive(Object a)//通过串口取得扫描枪的数据。
{
if (isLoad)
{
Thread.Sleep(800);
}
byte[] buf;
buf = new byte[1];
int bytesRead = 0;
int i;
this.code = "";
while (Receive)
{
if (Serial.Opened)
{
bytesRead = Serial.ReadPort(1, buf);
if (bytesRead > 0)
{
for (i = 0; i < bytesRead; i++)
{
GetDate(Convert.ToChar(buf[i]).ToString());
}
}
}
}
this.isLoad = false;
}
private void GetDate(string a)//通过串口取得扫描枪的数据。进行保存
{
if (this.InvokeRequired)
{
this.Invoke(new GetDateDelegate(GetDate), new object[] { a });
return;
}
this.code += a;
if (a == "\n")
{
this.data = this.code.Substring(0, this.code.Length-2);
//Thread.CurrentThread.Abort();
this.SaveData(this.code);
//Thread.CurrentThread.Start();
}
textBoxReceive.Text += a; }
private void textBoxSend_KeyPress(object sender, KeyPressEventArgs e)//通过usb接口直接输入进行保存
{
if (e.KeyChar == 13)
{
SaveData(this.textBoxSend.Text.Trim());
textBoxReceive.Text += this.textBoxSend.Text.Trim() + "\r\n";
this.textBoxSend.Text = "";
this.textBoxSend.Focus();
}
}
public void SaveData(string msSql)..保存数据
{}
现在问题是我没法取得串口的数据,请问该如何调用呢?串口的端口都正常打开!
然后对端口进行数据读取,一般情况下硬件厂商都会提供API接口,但是不同的硬件版本适用的API有可能是不同的,你最好能问一下别人厂家的人,给你提供下技术支持
看看传进去的数据是否正确
我老是犯这个错误