.NET 프레임 워크를위한 좋은 직렬 포트 클래스?
-
20-08-2019 - |
문제
.NET Serialport 클래스를 대체 할 좋은 (희망적으로 무료) 클래스를 아는 사람이 있습니까? 그것은 나에게 문제를 일으키고 약간 더 유연한 문제가 필요합니다.
내 문제에 대한 다른 스레드를 참조하십시오, 그러나 본질적으로 포트를 열 때 IOCTL_SERIAL_SET_DTR 및 IOCTL_SERIAL_CLR_DTR 명령이 발행되지 않도록 억제해야하므로 .NET 프레임 워크에서 제공하는 클래스보다 유연한 것이 필요합니다.
해결책
몇 년 전에 나는 사용했습니다 OpenNetCf.io.serial 직렬 지원이 .NET에 추가되기 전에. 소형 프레임 워크를위한 것이지만 컴팩트 한 장치와 일반 Windows 앱 모두에 사용했습니다. 당신은 소스 코드를 얻으므로 내가 한 일을 직접 수정할 수 있습니다.
기본적으로 Kernel32.dll에서 가져온 직렬 기능 주위에 AC# 래퍼를 생성합니다.
당신은 또한보고 싶을 수도 있습니다 USB 케이블이 플러그를 뽑아서 사라지는 직렬 포트를 캡처하는 방법
다음은 내가 호출했던 코드입니다.
using OpenNETCF.IO.Serial;
public static Port port;
private DetailedPortSettings portSettings;
private Mutex UpdateBusy = new Mutex();
// create the port
try
{
// create the port settings
portSettings = new HandshakeNone();
portSettings.BasicSettings.BaudRate=BaudRates.CBR_9600;
// create a default port on COM3 with no handshaking
port = new Port("COM3:", portSettings);
// define an event handler
port.DataReceived +=new Port.CommEvent(port_DataReceived);
port.RThreshold = 1;
port.InputLen = 0;
port.SThreshold = 1;
try
{
port.Open();
}
catch
{
port.Close();
}
}
catch
{
port.Close();
}
private void port_DataReceived()
{
// since RThreshold = 1, we get an event for every character
byte[] inputData = port.Input;
// do something with the data
// note that this is called from a read thread so you should
// protect any data pass from here to the main thread using mutex
// don't forget the use the mutex in the main thread as well
UpdateBusy.WaitOne();
// copy data to another data structure
UpdateBusy.ReleaseMutex();
}
private void port_SendBuff()
{
byte[] outputData = new byte[esize];
crc=0xffff;
j=0;
outputData[j++]=FS;
// .. more code to fill up buff
outputData[j++]=FS;
// number of chars sent is determined by size of outputData
port.Output = outputData;
}
// code to close port
if (port.IsOpen)
{
port.Close();
}
port.Dispose();
다른 팁
나는 이것을 시도하지 않았지만 아마도 당신은 할 수 있습니다.
- 소스 코드를 가져옵니다
System.IO.Ports.SerialPort
사용하여 반사기 또는 유사합니다 - 원하는대로 해당 소스 코드를 변경하십시오
- 수정 된 사본을 다른 네임 스페이스로 재구성하고 사용하십시오.
표준 .NET 시리얼 포트는 봉인 된 클래스가 아닙니다. 서브 클래스에서 필요한 동작을 얻을 수있는 기회는 무엇입니까?
제휴하지 않습니다 StackOverflow