如何将以下java代码转换为C#dotnet [英] How to convert below java code into C# dotnet

查看:107
本文介绍了如何将以下java代码转换为C#dotnet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.out.println("Trying to open port...");
           CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(comPort);
           port = (SerialPort)portId.open("SMS Transceiver", 10);
           port.setSerialPortParams(boardRates, dataBits, stopBits, 0);
           out = new OutputStreamWriter(port.getOutputStream(), "ISO-8859-1");
           in = new InputStreamReader(port.getInputStream(), "ISO-8859-1");
           System.out.println("Port is opened successfully..... \n");
           Thread.sleep(10000L);





我尝试过:





What I have tried:

Console.WriteLine("Trying to open port...");
Console.WriteLine(comPort);
port = new System.IO.Ports.SerialPort(comPort);
port.Open();
SerialPort Serial1 = new System.IO.Ports.SerialPort(comPort, boardRates, Parity.None, dataBits);
port.Encoding = Encoding.GetEncoding("iso-8859-1");
Console.WriteLine("Port is opened successfully..... \n");
Thread.Sleep(10000);

推荐答案

It尝试将代码从一种语言逐行转换为另一种语言通常不是一个好主意。

有时可行,但有时不行。



而是尝试分析您想要翻译的代码并理解它应该做什么,然后找出如何用另一种语言进行翻译。



In你的情况看起来好像你想要



1.打开一个特定的COM端口。

2.设置通讯参数

3.写入数据

4.读取数据



但不止这个我不知道,因为你不给那个信息。

所以你需要做这个分析。



例如你想做什么样的沟通?

*这是一个主从设置,一方总是发送数据而另一方只响应?

*谁是谁大师? PC或设备?

*连接的设备是否会随时发送数据,如条形码阅读器?





您通常需要在C#中通过串口进行通信?



1.设置通讯参数

It is usually not a good idea to just try to convert code from one language to another line by line.
Sometimes it works, but sometimes not.

Instead try to analyze the code you want to translate and understand what it is supposed to do and then find out how to do it in another language.

In your case it looks like you want to

1. Open a specific COM port.
2. Set communication parameters
3. Write data
4. Read data

But more than this I cannot know, because you don't give that information.
So you need to do this analyse.

For example what kind of communication do you want to do?
* Is it a master - slave setup where one side is always sending data and the other side only respond?
* Who is the master? The PC or the device?
* Will the connected device send data at any time, like a barcode reader?


What do you in general need to do in C# to communicate over a serial port?

1. Set up the communication parameters
string portName = "COM1";
int baudRate = 9600;
Parity parity = Parity.None;
int dataBits = 8;



2.创建类SerialPort的实例


2. Create an instance of the class SerialPort

SerialPort serial = new SerialPort(portName, baudRate, parity, dataBits);
serial.Encoding = Encoding.GetEncoding("iso-8859-1");



3.打开端口


3. Open the port

serial.Open();



4.写入数据


4. Write data

serial.WriteLine("Hello World");
or
serial.Write(new byte[] {0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64});



5.读取数据


5. Read data

string retVal = serial.ReadLine();  // Requires that the data is ended with a new line
or
byte[] buffer = new byte[serial.BytesToRead()];
int bytesRead = serial.Read(buffer, 0, buffer.Length);



你也可以使用 SerialPort.DataReceived事件(System.IO.Ports) [ ^ ]用于读取字节。例如,这对于可以随时从阅读器发送数据的条形码阅读器非常有用。



这是串行通信的基础知识。

使用MSDN并阅读文档: SerialPort类(System.IO.Ports) [ ^ ]



因为你没有解释你的目标是什么,它是很难提供比这更多的帮助。


You can also use the SerialPort.DataReceived Event (System.IO.Ports)[^] for reading bytes. This is useful for, for example, a barcode reader where data can be sent from the reader at any time.

That is the basics of serial communication.
Use MSDN and read the documentation: SerialPort Class (System.IO.Ports)[^]

As you don't explain what your goal is, it is difficult to give more help than this.


这篇关于如何将以下java代码转换为C#dotnet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆