我从串口接收数据有问题。每次发送12个字节,我想读取每个字节并将它们放入一个数组中,然后在richtextbox中显示它们(每个字节将显示在一个单独的行中)。但是有一个问题:当连续发送数据时,我会得到一些额外的0,它们根本不相关,而且我在终端程序中没有收到。 这是我的代码,如果你能帮助我,我感激不尽。
private void data_received(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
richTextBox1.Invoke(new EventHandler(ReadFromPort));
}
private void ReadFromPort(object sender, EventArgs e)
{
int bytes = 0;
label2.Text = "";
bytes = serialPort1.BytesToRead;
label2.Text = bytes.ToString();
byte[] buffer = new byte[12];
for (int k = 0; k < bytes; k++)
{
buffer[k] = 0;
}
serialPort1.Read(buffer, 0, bytes);
for (int i = 0; i < buffer.Length ; i++)
{
richTextBox1.AppendText(buffer[i].ToString() + Environment.NewLine);
}
}
答案 0 :(得分:2)
SerialPort.Read
int bytesRead = serialPort1.Read(buffer, 0, bytes);
for (int i = 0; i < bytesRead; i++)
{ … }
,显然:
如果count大于输入缓冲区中的字节数,则读取的字节数减少。
返回值表示从端口实际读取的数据量。然后只显示数组中的这个字节数:
var ua = 'Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
var casper = require('casper').create({
viewportSize: {width: 768, height: 1024},
userAgent: ua
});
var login = {email: 'fakeemail@gmail.com', pw: 'fakepw'}
var usrObj = {
succ: {},
err: []
};
//exit function
function exit() {
setTimeout(function() {
casper.exit();
casper.bypass(1);
}, 10);
}
//success & error message function
function message(dat) {
if (dat === 'credentials') {
console.log('Error: Login credentials are missing');
return exit();
}
else if (dat) {
console.log(dat);
return exit();
}
if (usrObj['err'].length > 0) {
console.log('Error not empty...');
console.log(usrObj.err);
return exit();
}
else if (usrObj['succ']) {
console.log('Success not empty...');
console.log(JSON.stringify(usrObj.succ));
return exit();
}
}
//trim login credentials
login.email = login.email.trim();
login.pw = login.pw.trim();
if (login.email && login.pw) {
casper.start('https://vimeo.com/log_in');
casper.waitForSelector('form#login_form',
function success() {
this.echo(this.getCurrentUrl());
this.sendKeys('form#login_form input[name="email"]', login.email);
this.sendKeys('form#login_form input[name="password"]', login.pw);
this.click('form#login_form input[type=submit][value="Log in with email"]');
},
function fail() {
message('Error with Vimeo [page not loading]')
}
);
casper.waitForSelector('#page_header>h1>a',
function success() {
this.echo(this.getCurrentUrl());
usrObj['succ']['uname'] = this.getHTML('span.topnav_user_name');
usrObj['succ']['profile'] = this.getElementAttribute('li.topnav_user_profile>a', 'href');
var test = [];
if (!usrObj.succ.uname) {test.push('Username not retrieved')}
if (!usrObj.succ.profile) {test.push('Profile link not retrieved')}
if (test.length > 0) {message(test.join('<br />'));}
//else {message();}
},
function fail() {
message('Login not successful');
}
);
casper.thenOpen('https://vimeo.com/staceydavidgearz',
function success() {
this.echo('Stacey David Profile: ' + this.getTitle());
this.echo(this.getCurrentUrl());
var finish = function() {
usrObj['succ']['foll'] = true;
message();
}
//var foll = this.getHTML('button[data-fatal-attraction="container:profile_page|component:follow"] > span');
var foll = this.getElementAttribute('button[data-fatal-attraction="container:profile_page|component:follow"] > svg', 'viewBox');
if (foll === '0 0 10 10') {
this.click('button[data-fatal-attraction="container:profile_page|component:follow"]');
setTimeout(function() {
foll = this.getElementAttribute('button[data-fatal-attraction="container:profile_page|component:follow"] > svg', 'viewBox');
if (foll === '0 0 10 10') {
message('Can\'t follow SD');
}
else {finish();}
}, 250);
}
else {
finish();
}
},
function fail() {
message('Not going to Stacey David profile page.');
}
);
casper.run();
}
else {message('credentials');}
答案 1 :(得分:0)
您正在将N个字节读入缓冲区,但显示整个缓冲区而不管您读入的字节数。有时你不会得到12个字节,但更少。但你仍然显示所有12个。
将for (int i = 0; i < buffer.Length ; i++)
更改为for (int i = 0; i < bytes ; i++)