硬件 485服务器 开发
485 我理解为时候把硬件给网络化,485就是一个ip端口,一头连接硬件一头连接网线
我们只需要连接485 的网络地址 对485进行数据传输交互就是间接对硬件交互(其中的一种)还有串口连接(曾经的)
这是就可实现基础的代码控制硬件
话不多说,上代码
连接485
class WeighingInstrument
{
Socket socketClient;
public WeighingInstrument(string ip,int port )//ip与端口号
{
socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipaddress = IPAddress.Parse(ip);
IPEndPoint endpoint = new IPEndPoint(ipaddress, port);
socketClient.Connect(endpoint);
}
/// <summary>
/// 接收信息
/// </summary>
/// <returns></returns>
public int Weigherweight()
{
byte[] arrRecMsg = new byte[11];
int length = socketClient.Receive(arrRecMsg);
Console.WriteLine("地磅值 &&&&&& "+Encoding.ASCII.GetString(arrRecMsg, 0, length));
string q = Encoding.ASCII.GetString(arrRecMsg, 0, length) .Replace("\u0002", "").Replace("\r", "");
double weight = Convert.ToDouble(q);
int zhi =(int) Math.Round(weight, 0);
Home.Meter = zhi;
//重量效验
return zhi;
}
//发送数据
public int fasong()
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//初始 socket.Send(Encoding.UTF8.GetBytes("1234567890"));//发送数据
}
}