16,555
社区成员
发帖
与我相关
我的任务
分享
public delegate void eventhandle(object sender, EventArgs e);
public bool Open()
{
if (this.m_IsOpen)
{
this.GetError(ERROR.EROPNNING);
return false;
}
this.m_handle = CreateFile("SCA1:", 0xc0000000, 0, 0, 3, 0, 0);
if (this.m_handle == -1)
{
this.m_IsOpen = false;
this.GetError(ERROR.ERCANTFINDSCA);
return false;
}
if (this.m_port == null)
{
this.m_port = new SerialPort();
}
this.m_port.BaudRate = 0x2580;
this.m_port.DataBits = 8;
this.m_port.PortName = "COM3:";
this.m_port.Open();
this.m_port.DataReceived += new SerialDataReceivedEventHandler(this.m_port_DataReceived);
if (this.m_port.IsOpen)
{
this.GetError(ERROR.EROK);
this.m_IsOpen = true;
return true;
}
this.GetError(ERROR.ERCANTOPENSERIAL);
this.m_IsOpen = false;
return false;
}
.......
private void DoWork()
{
uint[] lpOutBuffer = new uint[10];
int dwread = 0;
while (true)
{
while (!ReadFile(this.m_handle, lpOutBuffer, 40, out dwread, IntPtr.Zero) || (dwread <= 0))
{
}
this.m_strRead = string.Format("{0:D10} {1:D3},{2:D5}", ((lpOutBuffer[2] * 0x10000) + (lpOutBuffer[3] * 0x100)) + lpOutBuffer[4], lpOutBuffer[2], (lpOutBuffer[3] * 0x100) + lpOutBuffer[4]);
this.m_buf[0] = (byte)lpOutBuffer[0];
this.m_buf[1] = (byte)lpOutBuffer[1];
this.m_buf[2] = (byte)lpOutBuffer[2];
this.m_buf[3] = (byte)lpOutBuffer[3];
this.m_buf[4] = (byte)lpOutBuffer[4];
this.m_buf[5] = (byte)lpOutBuffer[5];
eventhandle dataReceive = this.DataReceive;
if (dataReceive != null)
{
dataReceive(null, null);
PlaySound(@"\Windows\asterisk.wav", 0, playMode.SND_FILENAME | playMode.SND_ASYNC);
Thread.Sleep(300);
}
}
}