请教怎么添加两个串口收到数据后的触发事件函数

请教怎么添加两个串口收到数据后的触发事件函数,第1张

如果是同步一直读数据直到ReadFile返回或超时,如果你指的是异步 *** 作,调用GetOverlappedResult函数,该函数返回 *** 作的结果,用来判断异步 *** 作是否完成,去找异步串口通讯的文章看看

这个不用数据库的触发事件的。

你添加数据执行SQL的时候是有返回值的,比如你添加一条数据,那么返回的时候如果成功就会返回插入数据库的数据数量 即为1,判断一下是否是0,如果不为0就d出窗口啊

以下是一段 我自己用的接收处理代码其中的sp是已经声明好的SERIPORT。

sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived)

//接收串口数据字符串

string watcher = null,send=null

//串口数据接收事件

void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)

{

watcher += sp.ReadExisting()//这句就是接收内容代码,如果只需要接收后面就都不需要了。

{

if (watcher.Length % 8 == 0)

{

send = watcher

switch (watcher)

{

case "I(001,1)":

//dt1.Tag = "0"

if(dt1.Tag=="0")

{

sign = "d_*_auto"

dt1.IsEnabled = true

dt_streamwrite.IsEnabled = true

}

//sign = "d_*_auto"

//dt1.IsEnabled = true

break

case "I(001,0)":

sign = "d_*_auto"

sp.WriteLine("O(01,000,0)")

dt_streamwrite.IsEnabled = true

dt1.IsEnabled = false

break

case "I(002,1)":

//dt2.Tag = "0"

if (dt2.Tag == "0")

{

sign = "d_*_auto"

dt2.IsEnabled = true

dt_streamwrite.IsEnabled = true

}

break

case "I(002,0)":

sign = "d_*_auto"

sp.WriteLine("O(02,000,0)")

dt2.IsEnabled = false

dt_streamwrite.IsEnabled = true

break

case "I(003,1)":

//dt3.Tag = "0"

if (dt3.Tag == "0")

{

sign = "d_*_auto"

dt3.IsEnabled = true

dt_streamwrite.IsEnabled = true

}

break

case "I(003,0)":

sign = "d_*_auto"

sp.WriteLine("O(03,000,0)")

dt3.IsEnabled = false

dt_streamwrite.IsEnabled = true

break

case "I(004,1)":

//dt4.Tag = "0"

if (dt4.Tag == "0")

{

sign = "d_*_auto"

dt4.IsEnabled = true

dt_streamwrite.IsEnabled = true

}

break

case "I(004,0)":

sign = "d_*_auto"

sp.WriteLine("O(04,000,0)")

dt4.IsEnabled = false

dt_streamwrite.IsEnabled = true

break

case "I(005,1)":

//dt5.Tag = "0"

if (dt5.Tag == "0")

{

sign = "d_*_auto"

dt5.IsEnabled = true

dt_streamwrite.IsEnabled = true

}

break

case "I(005,0)":

sign = "d_*_auto"

sp.WriteLine("O(05,000,0)")

dt5.IsEnabled = false

dt_streamwrite.IsEnabled = true

break

case "I(006,1)":

sign = "d_*_auto"

dt1.Tag = "1"

dt1.IsEnabled = false

dt_streamwrite.IsEnabled = true

break

case "I(006,0)":

sign = "d_*_auto"

dt1.Tag = "0"

dt_streamwrite.IsEnabled = true

break

case "I(007,1)":

sign = "d_*_auto"

dt2.IsEnabled = false

dt_streamwrite.IsEnabled = true

dt2.Tag = "1"

break

case "I(007,0)":

sign = "d_*_auto"

dt2.Tag = "0"

dt_streamwrite.IsEnabled = true

break

case "I(008,1)":

sign = "d_*_auto"

dt3.IsEnabled = false

dt_streamwrite.IsEnabled = true

dt3.Tag = "1"

break

case "I(008,0)":

sign = "d_*_auto"

dt3.Tag = "0"

dt_streamwrite.IsEnabled = true

break

case "I(009,1)":

sign = "d_*_auto"

dt_streamwrite.IsEnabled = true

dt4.IsEnabled = false

dt4.Tag = "1"

break

case "I(009,0)":

sign = "d_*_auto"

dt4.Tag = "0"

dt_streamwrite.IsEnabled = true

break

case "I(010,1)":

sign = "d_*_auto"

dt5.IsEnabled = false

dt_streamwrite.IsEnabled = true

dt5.Tag = "1"

break

case "I(010,0)":

sign = "d_*_auto"

dt5.Tag = "0"

dt_streamwrite.IsEnabled = true

break

}

watcher = null

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11947988.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存