using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; using System.Drawing.Printing; using System.IO; using System.Threading; using CRs232; namespace barcode.operate { public class Hywlhybq : System.Windows.Forms.Form { ......... CRs232.Rs232 rs232 ; Thread com1 = null ; ......... #region Windows Form Designer generated code {} #endregion private void Hywlhybq_Load(object sender, System.EventArgs e) {
有人是这样回答的:
当然是厂家提供接口啊,要不你怎么知道数据格式!
(当然自己慢慢试,也可以试出来,不过那就比较麻烦咯,我们曾经搞过,很费时)
“一般是磅连到你的com,你读数据,c#也是一样.”你的意思是磅自动把获得的重量发向com口,我们只要从com口取值就我们所要的重量对吗?
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.IO;
using System.Threading;
using CRs232;
namespace barcode.operate
{
public class Hywlhybq : System.Windows.Forms.Form
{
.........
CRs232.Rs232 rs232 ;
Thread com1 = null ;
......... #region Windows Form Designer generated code
{}
#endregion
private void Hywlhybq_Load(object sender, System.EventArgs e)
{
try
{
dzc();;
}
catch
{}
}
private void dzc()
{
try
{
if (rs232.IsOpen){ rs232.Close();com1.Abort();}
}
catch
{}
rs232 = new CRs232.Rs232();
rs232.Port = 1;
rs232.BaudRate = 9600;
rs232.DataBit = 8;
rs232.StopBit = Rs232.DataStopBit.StopBit_1;
rs232.Parity = Rs232.DataParity.Parity_None;
//rs232.Timeout = 500;
rs232.WorkingMode = Rs232.Mode.NonOverlapped; try
{
rs232.Open();
if (rs232.IsOpen)
{
com1 = new Thread(new ThreadStart(this.GetWeight));
//com1.IsBackground = true;
com1.Start();
}
}
catch
{
MessageBox.Show("串口:COM1 已被占用!");
}
} private void GetWeight()
{
int i=0;
while(true)
{
Thread.Sleep(10);
try
{
rs232.Read(16);
if(i<3)
{
ff=ff+rs232.InputStreamString.ToString();i++;
}
else
{
i=0;
Jsmz(ff);ff="";
}
}
catch(Exception err)
{
string x=err.Message;
}
}
}
private void Jsmz(string ff)
{
char [] zhongliang=ff.ToCharArray();
for(int j=0;j<ff.Length;j++)
{
if(zhongliang[j]=='N'&&a1=="")
{
j++;
bool pd=false;
for(int i=0;i<7;i++)
{
if(zhongliang[j]!='0'||pd==true)
if(zhongliang[j]!='.')
{
a1=a1+zhongliang[j];pd=true;
}
j++;
}
}
}
YYY=a1;a1="";
ff="";
}
private void Hywlhybq_Closed(object sender, System.EventArgs e)
{
try
{
if (rs232.IsOpen){ rs232.Close();com1.Abort();}
}
catch
{}
}
}
}希望对你有帮助。GetWeight()是获取数据的。
myEmail:[email protected]