.net - C# and SerialPort Class truncating data -
.net - C# and SerialPort Class truncating data -
i have c# application uses .net serialport class. code utilize pick info serial port nil special. key parts are
//open port comport.baudrate = myport.baudrate; comport.stopbits = stopbits.one; comport.databits = 8; comport.parity = parity.none; comport.readtimeout = 20000; comport.portname = myport.portsystemname; comport.handshake = handshake.none; comport.rtsenable = true; comport.datareceived += new serialdatareceivedeventhandler(port_datareceived); comport.datareceived += new serialdatareceivedeventhandler(port_datareceived); private void port_datareceived(object sender, serialdatareceivedeventargs e) { string msg = ""; seek { msg = comport.readexisting(); if (comport.isopen) comport.close(); }
this code works perfectly fine in windows xp. however, on windows 7 runs issues no matter info sent, picks first 4 characters. in string "123456", msg "1234". device collecting info rfideas pcprox , have verified info fine. have verified info looks ok in hyperterminal. there has odd way i'm picking info in code. help!
that exclusively consistent api. not, , never has been, guaranteed read everything:
this method returns contents of stream , internal buffer of serialport object string. method not utilize time-out. note method can leave trailing lead bytes in internal buffer, makes bytestoread value greater zero.
additionally, need handle "not yet in internal buffer" - don't read while bytestoread positive. involves looping , buffering until entire message received.
it job read right amount of data, either using markers such line-ends, or using length-prefix header.
if works fine on xp, fortune (and maybe timing and/or efficiency tweaks).
everything above applies as inputs, not serial ports; example, file io , network io works along pretty much same principles.
c# .net serial-port
Comments
Post a Comment