2,543
社区成员
发帖
与我相关
我的任务
分享
void SerialPortInit(string com)
{
string portName;
if (com == null)
portName = "COM0";
else
portName = com;
int baudRate = 115200;
Parity parity = Parity.None;
int dataBits = 8;
StopBits stopBits = StopBits.One;
int ReadTimeOut = 400;
SerialPort sp = null;
sp = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
sp.ReadTimeout = ReadTimeOut;
try
{
if (sp.IsOpen)
{
sp.Close();
sp.Open();
}
else
{
sp.Open();
}
}
catch (Exception ex)
{
Debug.Log("错误:" + ex.Message);
}
}