[英]Serial Port Communication with electric meter and serial port in c#
I have an Electric meter connected over USB on COM5. 我有一个通过COM5上的USB连接的电表。
I want to read data from the meter but first check whether it's working or not. 我想从仪表读取数据,但首先要检查它是否正常工作。 Means if I write something over the port I will send and receive again.
意味着如果我在端口上写一些东西,我将再次发送和接收。
So, I am using the SerialPort
class and the DataReceived
event handler. 因此,我正在使用
SerialPort
类和DataReceived
事件处理程序。
My code is below. 我的代码如下。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace Communication
{
class Program
{
static void Main(string[] args)
{
const int bufSize = 2048;
Byte[] but = new Byte[bufSize]; // to save receive data
SerialPort sp = new SerialPort("COM5");
sp.BaudRate = 9600;
sp.Parity = Parity.None;
sp.StopBits = StopBits.One;
sp.DataBits = 8;
sp.Handshake = Handshake.None;
sp.DtrEnable = true;
sp.RtsEnable = true;
sp.Open(); //open the port
sp.DataReceived += port_OnReceiveDatazz; // event handler
sp.WriteLine("$"); //start data stream
Console.ReadLine();
sp.WriteLine("!"); //stop data stream
sp.Close(); //close the port
}
//event handler method
public static void SerialDataReceivedEventHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort srlport = (SerialPort)sender;
const int bufSize = 12;
Byte[] buf = new Byte[bufSize];
Console.WriteLine("Data Received!!!");
Console.WriteLine(srlport.Read(buf,0,bufSize));
}
}
}
When compiling I get this error: 编译时出现此错误:
port_OnReceivedDatazz does not exist in the current context
当前上下文中不存在port_OnReceivedDatazz
Please give some suggestion. 请给一些建议。
There is error port_OnReceivedDatazz does not exist in the current context
当前上下文中不存在错误port_OnReceivedDatazz
The name of your event handler and your event handler method have to correspond! 事件处理程序的名称和事件处理程序的方法必须对应!
You have basically 2 Options either rename this line: 您基本上有2个选项可以重命名此行:
sp.DataReceived += port_OnReceiveDatazz; // event handler
to : 至 :
sp.DataReceived += SerialDataReceivedEventHandler;
OR rename the method 或重命名方法
public static void port_OnReceiveDatazz(object sender, SerialDataReceivedEventArgs e)
{
EDIT: 编辑:
if you still don't see the desired output it might be that Console.ReadLine()
blocks the console and prevents it from printing. 如果仍然看不到所需的输出,则可能是
Console.ReadLine()
阻止了控制台并阻止其打印。
In the MSDN Example they use 在MSDN示例中,他们使用
Console.ReadKey();
For Reference see this answer . 作为参考,请参见此答案 。
Just as last remark, you never save your received data permanently since you use a local variable for storing the input: 就像最后一句话一样,您永远不会永久保存收到的数据,因为您使用本地变量来存储输入:
Byte[] buf = new Byte[bufSize];
srlport.Read(buf,0,bufSize);
You should use the array from this line: 您应该在此行中使用数组:
Byte[] but = new Byte[bufSize]; // to save receive data
When you read your data take the but
array: 当您读取数据时,使用
but
数组:
srlport.Read(but,0,bufSize);
EDIT 2: 编辑2:
If you want to print out what you have received you need to print out the content of the array that you fill with the Read
method: 如果要打印收到的内容,则需要打印使用
Read
方法填充的数组的内容:
//event handler method
public static void SerialDataReceivedEventHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort srlport = (SerialPort)sender;
const int bufSize = 12;
Byte[] buf = new Byte[bufSize];
Console.WriteLine("Data Received!!!");
int bytes_read = srlport.Read(buf,0,bufSize)
Console.WriteLine("Bytes read: " + bytes_read);
// you can use String.Join to print out the entire array without a loop
Console.WriteLine("Content:\n" + String.Join(" ", bud));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.