c# - SerialPort not writing data with long strings - FIXED -


i writing program writes channel urls , channel names device. i'm trying write channel urls length of 254. shorter urls (i haven't found threshold yet) work. makes more strange channel names 16 characters long, , these write when channel urls shorter.

i have following code works:

            (int = 0; < numberofchannels; i++)             {                 string hexstring = string.format("{0:x1}", i);                 serialport.writeline("channel 2 " + hexstring + " " + channelurls[i]);                 system.threading.thread.sleep(1000);                 serialport.writeline("channel 1 " + hexstring + " " + channelnames[i]);                 system.threading.thread.sleep(1000);             } 

however, not want have code sleep 2seconds writing 1 channel. question: how long strings written through serialport without waiting 2 seconds?

this not work:

            (int = 0; < numberofchannels; i++)             {                 string hexstring = string.format("{0:x1}", i);                 if (serialport.writeline("channel 2 " + hexstring + " " + channelurls[i]))                 {                     debug.writeline("this should happen before string written");                 }                 if(serialport.writeline("channel 1 " + hexstring + " " + channelnames[i]))                 {                     debug.writeline("this should happen before string written");                 }             } 

the debug lines written, , string not written through comport.

my serialport.writeline function looks this:

    public static bool writeline(string data)     {         if (serport.isopen)         {             debug.writeline("port open, writing "+ data);             serport.writeline(data);             return true;         }         else return false;      } 

update: found when disconnect , reconnect, first url written. apparently has buffer?

update2: how set serialport:

                    serport.baudrate = 115200;                     serport.databits = 8;                     serport.stopbits = stopbits.one;                     serport.portname = comnr;                     serport.handshake = handshake.none;                     serialport.serport.readtimeout = 1000;                     serialport.serport.writetimeout = 1000;                     serport.datareceived += serport_datareceived;                     serport.open();                     serport.discardinbuffer();                     serport.discardoutbuffer(); 

update 3: fixed issue i've found issue was. before sending commands shown here, sent command took time process. if device recieved data during process stop receiving data. updated code timer check ok respond device before sending next commands.

this depends on device working on. if send many bytes it, there's chance cannot process them all.

you can try decrease waiting time 1000 ms to, example, 200 ms.

you can try send packages of data... example, send long string in packages of 50 characters, waiting amount of time between them (example, 100 ms).

update 1:

also try decrease baudrate 115200 9600.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -