c# - Reading FTDI TTL-232R to USB -
i’m trying read rs232 input via usb using ftdi rs232 usb 5v programmable cable. using ftd2xx_net library. connected smart meter sends telegram every 10 seconds.
all seems work fine. can , have set baudrate, databits, stopbits, etc. stops when reading data. below code snippet passes without data. i’m not experienced in electronics it’s bit out of comfortzone.
my questions.
- how flowcontrol influence read capability if device.
- how numbytesavailable influence the read capability if device.
- how numbytesread influence the read capability if device.
de setting of variables of above properties unknown me. not know how data receive. highly appreciated.
cable datasheet found here: http://www.ftdichip.com/support/documents/datasheets/cables/ds_ttl-232r_cables.pdf
smart meter datasheet found here: http://files.domoticaforum.eu/uploads/smartmetering/dsmr%20v4.0%20final%20p1.pdf
enter code here namespace p1_reader { class program { static void main(string[] args) { if (args.count() > 0) { executeoption(args[0]); } else { string option; { var menu = new stringbuilder(); menu.appendline("(1) listenfordata"); menu.appendline("(x) exit"); system.console.writeline(menu); option = system.console.readkey().keychar.tostring(); system.console.writeline(); executeoption(option); system.console.writeline(); } while (option.tolower() != "x"); } } private static void executeoption(string option) { switch (option.tolower()) { case "1": listenfordata(); break; case "x": break; default: system.console.writeline("that's not option!"); break; } } private static void listenfordata() { try { uint32 ftdidevicecount = 0; ftdi.ft_status ftstatus = ftdi.ft_status.ft_ok; ftdi myftdidevice = new ftdi(); ftstatus = myftdidevice.getnumberofdevices(ref ftdidevicecount); if (ftstatus == ftdi.ft_status.ft_ok) { console.writeline("number of ftdi devices: " + ftdidevicecount.tostring()); console.writeline(""); } else { console.writeline("failed number of devices (error " + ftstatus.tostring() + ")"); console.readkey(); return; } if (ftdidevicecount == 0) { console.writeline("failed number of devices (error " + ftstatus.tostring() + ")"); console.readkey(); return; } ftdi.ft_device_info_node[] ftdidevicelist = new ftdi.ft_device_info_node[ftdidevicecount]; ftstatus = myftdidevice.getdevicelist(ftdidevicelist); if (ftstatus == ftdi.ft_status.ft_ok) { (uint32 = 0; < ftdidevicecount; i++) { console.writeline("device index: " + i.tostring()); console.writeline("flags: " + string.format("{0:x}", ftdidevicelist[i].flags)); console.writeline("type: " + ftdidevicelist[i].type.tostring()); console.writeline("id: " + string.format("{0:x}", ftdidevicelist[i].id)); console.writeline("location id: " + string.format("{0:x}", ftdidevicelist[i].locid)); console.writeline("serial number: " + ftdidevicelist[i].serialnumber.tostring()); console.writeline("description: " + ftdidevicelist[i].description.tostring()); console.writeline(""); } } ftstatus = myftdidevice.openbyserialnumber(ftdidevicelist[0].serialnumber); if (ftstatus != ftdi.ft_status.ft_ok) { console.writeline("failed open device (error " + ftstatus.tostring() + ")"); console.readkey(); return; } ftstatus = myftdidevice.setbaudrate(115200); if (ftstatus != ftdi.ft_status.ft_ok) { console.writeline("failed set baud rate (error " + ftstatus.tostring() + ")"); console.readkey(); return; } ftstatus = myftdidevice.setdatacharacteristics(ftdi.ft_data_bits.ft_bits_8, ftdi.ft_stop_bits.ft_stop_bits_1, ftdi.ft_parity.ft_parity_none); if (ftstatus != ftdi.ft_status.ft_ok) { console.writeline("failed set data characteristics (error " + ftstatus.tostring() + ")"); console.readkey(); return; } ftstatus = myftdidevice.setflowcontrol(ftdi.ft_flow_control.ft_flow_rts_cts, 0x11, 0x13); if (ftstatus != ftdi.ft_status.ft_ok) { console.writeline("failed set flow control (error " + ftstatus.tostring() + ")"); console.readkey(); return; } // set read timeout 12 seconds, write timeout infinite ftstatus = myftdidevice.settimeouts(12000, 0); if (ftstatus != ftdi.ft_status.ft_ok) { console.writeline("failed set timeouts (error " + ftstatus.tostring() + ")"); console.readkey(); return; } uint32 numbytesavailable = 0; ftstatus = myftdidevice.getrxbytesavailable(ref numbytesavailable); if (ftstatus != ftdi.ft_status.ft_ok) { console.writeline("failed number of bytes available read (error " + ftstatus.tostring() + ")"); console.readkey(); return; } thread.sleep(10); string readdata = ""; uint32 numbytesread = 0; byte[] databuffer = new byte[1024]; ftstatus = myftdidevice.read(out readdata, numbytesavailable, ref numbytesread); while (readdata == "") { ftstatus = myftdidevice.read(out readdata, numbytesavailable, ref numbytesread); } if (ftstatus != ftdi.ft_status.ft_ok) { console.writeline("failed read data (error " + ftstatus.tostring() + ")"); console.readkey(); return; } console.writeline(readdata); ftstatus = myftdidevice.close(); console.writeline("press key continue."); console.readkey(); return; } catch { console.writeline("something realy bad happened.."); } } } }
the ftdi driver has internal buffer , automatically copies data read rx data line buffer. call getrxbytesavailable()
tell how data within internal buffer i.e. how data chip has read.
the call read()
blocking, wait until has number of bytes requested before returning. if remember correctly can set read timeout using api.
you want have getrxbytesavailable()
call within while loop e.g.
uint32 numbytesavailable = 0; while (true) { ftstatus = myftdidevice.getrxbytesavailable(ref numbytesavailable); if (ftstatus != ftdi.ft_status.ft_ok) { console.writeline("failed number of bytes available read (error " + ftstatus.tostring() + ")"); console.readkey(); break; } string readdata = ""; uint32 numbytesread = 0; byte[] databuffer = new byte[1024]; // todo: check don't on buffer. ftstatus = myftdidevice.read(out readdata, numbytesavailable, ref numbytesread); processdata(readdata); thread.sleep(10000); // sleep 10 seconds. }
Comments
Post a Comment