此帖出自嵌入式系统论坛
最新回复
我已经自己解决了:
private void button3_Click(object sender, EventArgs e) //写入某页内容
{
try
{
string strtxt = textBox2.Text.ToString();
if (strtxt.Length < 32) //写入的byte数据长度必须为32字节,否则责要做处理!
{
if (strtxt.Length == 0)
{
textBox1.Text = "您没有输入任何数据!";
return;
}
else
{
strtxt = strtxt.PadLeft(32, '0');
}
}
else if (strtxt.Length > 32)
{
strtxt = strtxt.Remove(32, strtxt.Length - 32);
}
string f = "K1";
string d = "M" + comboBox1.Text.ToString()+strtxt;
byte[] mbytx = new byte[f.Length];
byte[] mbyrx = new byte[d.Length];
ASCIIEncoding objEncoder = new ASCIIEncoding();
mbytx = objEncoder.GetBytes(f);
mbyrx = objEncoder.GetBytes(d);
byte[] function = new byte[10];
byte[] data = new byte[100];
if (CommSend(mbytx, mbyrx) == 1)
{
if (CommRece(function, data) == 1)
{
string str = System.Text.Encoding.Default.GetString(data, 0, 99);
textBox1.Text = "写入成功!";
}
else
{
textBox1.Text = "写入失败!";
}
}
else
{
textBox1.Text = "写入超时!";
}
}
catch
{
textBox1.Text = "异常发生!";
}
}
private void button4_Click(object sender, EventArgs e) //读取某页内容
{
try
{
string f = "K0";
string d = "M"+comboBox1.Text.ToString();
byte[] mbytx = new byte[f.Length];
byte[] mbyrx = new byte[d.Length];
ASCIIEncoding objEncoder = new ASCIIEncoding();
mbytx = objEncoder.GetBytes(f);
mbyrx = objEncoder.GetBytes(d);
byte[] function = new byte[10];
byte[] data = new byte[100];
if (CommSend(mbytx, mbyrx) == 1)
{
if (CommRece(function, data) == 1)
{
string str = System.Text.Encoding.Default.GetString(data, 0, 99);
textBox2.Text = str.Remove(0, 4);
textBox1.Text = "读取成功!";
}
else
{
textBox1.Text = "读取失败!";
}
}
else
{
textBox1.Text = "读取超时!";
}
}
catch
{
textBox1.Text = "异常发生!";
}
}
详情
回复
发表于 2008-1-30 23:59
| ||
|
||
此帖出自嵌入式系统论坛
| ||
|
||
| |
|
|
| |
|
|
此帖出自嵌入式系统论坛
| ||
|
||
EEWorld Datasheet 技术支持