|
Hi All.
I would like to ask how to read variable with period of time or if this variable has change.
|
|
|
|
|
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
And at the moment I have no idea what you are trying to work out!
Try expanding on what you have told us - because at the moment we can't help you.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Hi.
I'm sorry from my negligence.
I create Windows Forms application. In this application I would like to read data form PLC (Real Variable).
Now when I run this program I read value but only one time.
In my PLC I see that my variable has change but in C# application not.
In conclusion I would like to read my variable continuously or if it change.
My Code:
private void Form1_Load(object sender, EventArgs e)
{
SIS7.Net.Channel myChannel = new SIS7.Net.Channel();
SIS7.Device myDevice = new SIS7.Device("10.228.216.156,0,1", SIS7.Model.S7_1200);
SIS7.Group myGroup = new SIS7.Group(false, 500);
SIS7.Item myItem = new SIS7.Item("DB4.DBD68", 1, SIS7.DataType.Int32);
SIS7.Item myItem1 = new SIS7.Item("DB4.DBD100", 1, SIS7.DataType.Single);
myChannel.Devices.Add(myDevice);
myDevice.Groups.Add(myGroup);
myGroup.Items.Add(myItem);
myGroup.Items.Add(myItem1);
System.Single myFloat32;
try
{
myItem.Read();
myItem1.Read();
textBoxItemRead.Text = myItem.Values[0].ToString();
textBox1.Text = myItem1.Values[0].ToString();
}
catch (Exception ex)
{
}
myChannel.Dispose();
}
Regards
Pawel
|
|
|
|
|
Well...that code reads from the PLC only once (when the form loads) and then closes and Disposes your channel immediately.
So when the form is displayed, the PLC isn't communicating with the application at all - and couldn't if it wanted to because the connection isn't there any more.
Probably, you want to move that into a different thread, and make it loop to read the values, and then update the textboxes.
I'd suggest that you look at a BackgroundWorker[^] as that allows you to use the Progress to pass the messages back to the main thread for display (you can't change UI items from any thread except the main thread) via the BackgroundWorker.ProgressChanged Event (System.ComponentModel)[^] - the ProgressChangedEventArgs contains a UserState property you can transfer it in.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I need to transfer files to/from a server. I've done FTP in C# before but never used SCP.
From what I can find in Google, there isn't a lot available about using SCP in C#.
- Can anyone shed some light on SCP?
- Any reason not to use FTP assuming I encrypt the files before transfer?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
SCP[^] is an earlier and more primitive incarnation of the SFTP[^] protocol.
(Not to be confused with FTPS[^], which is just standard FTP over SSL.)
SCP, SFTP or FTPS would likely be a better choice than rolling your own encryption code. They've been written by experts and battle-tested by thousands of users over several decades.
The BCL has built-in support for FTPS via the FtpWebRequest.EnableSsl property[^]. For SFTP or SCP, you'll need a third-party library:
- SSH.NET[^] provides SCP and SFTP support.
- WinSCP[^] also has a .NET / COM wrapper.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Excellent info.. Thank you!
If it's not broken, fix it until it is
|
|
|
|
|
This kind of answer is what makes me scan every answer in this forum. Always nice to learn something new
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
The SCS TCP Server/ Client implementation, i would like to know if the Server side is also included and how to use it as part of implementation to a Server. Does SCS TCP Server take care of itself in this case. Thank you. http://codeproject.cachefly.net/script/Forums/Images/thumbs_up.gif
|
|
|
|
|
I'm working on making an app that scrapes my debit card account for my current balance and account activity. (I recently bought a Windows Phone, and they don't have an app for WP, and their website is not mobile friendly).
I'm running into an issue that has me a bit stumped. They use 2 steps for authentication. The first form submits the username and password, the 2nd asks for your date of birth as a secondary authentication.
I'm not sure where it's failing. I know the first request is good, as it brings up the DOB verify page, however after posting the DOB data, the 3rd response is back at the login screen.
The cookie headers are being filled, and all the response codes seem to be good. Does anyone see where the potential problem might be in this?
HttpWebRequest http = WebRequest.Create(LoginURL) as HttpWebRequest;
http.KeepAlive = true;
http.Method = "POST";
http.ContentType = "application/x-www-form-urlencoded";
string postData = "languageCode=&reqType=&j_username=myuser&j_password=mypass&sign-in.x=29&sign-in.y=7";
byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData);
http.ContentLength = postData.Length;
using (Stream postStream = http.GetRequestStream())
{
postStream.Write(dataBytes, 0, dataBytes.Length);
}
HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse;
using (Stream respStream = httpResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(respStream, Encoding.UTF8);
File.WriteAllText("loginresp.html", reader.ReadToEnd());
reader.Close();
}
httpResponse.Close();
HttpWebRequest http2 = WebRequest.Create(DOBVerifyURL) as HttpWebRequest;
http2.KeepAlive = true;
http2.Method = "POST";
http2.ContentType = "application/x-www-form-urlencoded";
http2.Headers["Cookie"] = httpResponse.Headers["Set-Cookie"];
http2.Referer = LoginURL;
http2.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36";
postData = "reqType=&cmd=checkSecu&languageCode=&additionalHolderAuthValue=urlencodeddob&continue.x=86&continue.y=13";
dataBytes = UTF8Encoding.UTF8.GetBytes(postData);
http2.ContentLength = postData.Length;
using (Stream postStream = http2.GetRequestStream())
{
postStream.Write(dataBytes, 0, dataBytes.Length);
}
HttpWebResponse httpResponse2 = http2.GetResponse() as HttpWebResponse;
using (Stream respStream2 = httpResponse2.GetResponseStream())
{
HttpWebRequest http3 = WebRequest.Create(AcctActivityURL) as HttpWebRequest;
http3.KeepAlive = true;
http3.ContentType = "text/html";
http3.Headers["Cookie"] = httpResponse2.Headers["Set-Cookie"];
http3.Referer = LoginURL;
http3.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36";
http3.AllowAutoRedirect = false;
HttpWebResponse httpResponse3 = http3.GetResponse() as HttpWebResponse;
using (Stream respStream3 = httpResponse3.GetResponseStream())
{
StreamReader reader = new StreamReader(respStream3, Encoding.UTF8);
File.WriteAllText("dobresp.html", reader.ReadToEnd());
reader.Close();
}
}
I copied the headers using Chrome's dev tools.
I can provide the working (from Chrome) headers, html, etc (edited of any personal info, of course) if needed.
(Side note: I don't plan on releasing or selling this app, this is purely for my own use. And it doesn't violate any terms or conditions so far as I can tell)
|
|
|
|
|
To be perfectly honest, I wouldn't do this.
Partly because it's going to fail in interesting ways - my bank throws in additional security questions at random intervals to throw keyloggers off and yours probably does the same. Partly becuase my bank changes its login page pretty often, again for the same reason. But mostly because you are planning on writing an application which automatically logs into your bank account and you want to put it on a phone. Tell me, which is the easiest piece of electronic kit you own to steal? Which is the most commonly lost or stolen? Yes - it's your phone. And if it's stolen, or found you have just handed the keys to your bank account to total strangers...
Are you really sure this is a good idea?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Normally, I'd agree with you 100%. However, the only thing you can do with this website is check your balance and the latest activity on your account. Neither the card number nor my social are listed on there. And in order to change anything on the account, you need to call customer service and provide both of those. It's not a bank account, per say, just a payroll deposit account with a debit card attached. If it was anything more, I'd not even consider doing this myself. And in the 4 years I've had the account, the front page hasn't changed, nor have I been asked for anything other than my dob
|
|
|
|
|
Steal a Windows Phone??
|
|
|
|
|
Did anyone say criminals are clever?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Rather than copying the cookie headers around manually, just create a new instance of the CookieContainer class[^] at the start, and assign it to the CookieContainer property[^] of each HttpWebRequest object.
If your bank is doing their job properly, there should be an anti-CRSF[^] token in the form, which you will need to scrape from each response and post to the next request.
You might also need to set the Referer property on the second and third request.
If you still can't make it work, try using Fiddler[^] to capture the requests from the browser and from your code to see what the difference is.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
In addition to the replies here. There are also many security schemes in place to avoid "machines" (programmes) to login or submit forms. Think of captchas eg. or like my bank requiering my bank card in a device and codes that are generated on the fly. (there are many others)
Steer away from this venture young padawan
|
|
|
|
|
I have a serial port class which looks something like this:
public static class OldHaspCommunication
{
private static SerialPort m_port;
private static string m_portName;
private static bool m_isOpen = false;
private static int m_baudRate;
private static Parity m_parity;
private static int m_dataBits;
private static StopBits m_stopBits;
private static bool m_xonxoff;
private static bool m_dtr;
private static bool m_rts;
public static void OpenPort(string portName,
int baudRate,
Parity parity,
int dataBits,
StopBits stopBits,
bool dtr,
bool rts,
bool xonxoff = false)
{
m_port = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
m_portName = portName;
m_baudRate = baudRate;
m_parity = parity;
m_dataBits = dataBits;
m_stopBits = stopBits;
m_xonxoff = xonxoff;
m_dtr = dtr;
m_rts = rts;
if (xonxoff)
m_port.Handshake = Handshake.XOnXOff;
m_port.DtrEnable = dtr;
m_port.RtsEnable = rts;
m_port.ReadTimeout = 500;
m_port.WriteTimeout = 500;
m_port.Open();
m_isOpen = true;
}
private static void ReadAll(byte[] data, uint length, int retryCount)
{
if (length > data.Length)
{
throw new Exception("Wrong data length in ReadAll");
}
int offset = 0;
int i = 0;
int remaining = checked((int)length);
while (remaining > 0)
{
if (i >= retryCount)
{
throw new Exception("Exceeded retry count parameter during reading in ReadAll");
}
int read = m_port.Read(data, offset, remaining);
if (read <= 0)
{
throw new EndOfStreamException(String.Format("ReadAll(OldHaspCommunication) - End of data reached with {0} bytes left to read", remaining));
}
remaining -= read;
offset += read;
i++;
}
}
private static void WriteAll(byte[] data, uint length)
{
int len = checked((int)length);
if (len > data.Length)
{
throw new Exception("Wrong data length in WriteAll");
}
m_port.Write(data, 0, len);
}
public static void ClosePort()
{
if (m_isOpen)
{
Reset();
m_port.Close();
m_isOpen = false;
}
}
private static void Reset()
{
m_port.DiscardInBuffer();
m_port.DiscardOutBuffer();
}
}
It seemed to be working fine, but I encountered problems in such a case.
When my device HASP was plugged into the computer everything went fine.
Then I unplugged the hasp and continued using the class, but I was getting error that I was calculating the data incorrectly (signature was wrong which I compute using write/read calls).
Then I plugged again my HASP and got such exception(with stack trace):
2016-02-10 13:01:14 - [LOG_ERROR] [THREAD ID: 11] General Exception: Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
at ProcessingLibrary.OldHaspCommunication.Reset() in c:\Users\g.\Documents\Visual Studio 2012\Projects\ProcessingLibrary\ProcessingLibrary\src\HASP\OldHaspCommunication.cs:line 270
at ProcessingLibrary.OldHaspCommunication.QueryParam(Byte[] in_buffer, UInt32 length, Byte command, Byte[] out_buffer, UInt32 szBuf, Byte& ret_code, Int32 l_entry, Int32 l_loop, Boolean quick_ser_no) in c:\Users\g.\Documents\Visual Studio 2012\Projects\ProcessingLibrary\ProcessingLibrary\src\HASP\OldHaspCommunication.cs:line 303
at ProcessingLibrary.OldHaspCommunication.Query(Byte[] in_buffer, UInt32 length, Byte command, Byte[] out_buffer, UInt32 szBuff, Byte& retcode) in c:\Users\g.\Documents\Visual Studio 2012\Projects\ProcessingLibrary\ProcessingLibrary\src\HASP\OldHaspCommunication.cs:line 277
at ProcessingLibrary.OldHaspCommunication.QueryCmd8(Byte[] in_buffer, Byte command, Byte[] out_buffer) in c:\Users\g.\Documents\Visual Studio 2012\Projects\ProcessingLibrary\ProcessingLibrary\src\HASP\OldHaspCommunication.cs:line 286
at ProcessingLibrary.CryptographyUtilities.SetSessionKey() in c:\Users\g.\Documents\Visual Studio 2012\Projects\ProcessingLibrary\ProcessingLibrary\src\Security\CryptographyUtilities.cs:line 94
at ProcessingLibrary.CryptographyUtilities.OldHASPSign(Byte[] buffer, UInt32 size, Byte[] outbuffer, Boolean session) in c:\Users\g.\Documents\Visual Studio 2012\Projects\ProcessingLibrary\ProcessingLibrary\src\Security\CryptographyUtilities.cs:line 137
at ProcessingLibrary.UpdateTerminalInfoMethods.TestConnection(ProcessingErrorInfo& errorInfo, String terminalID, UInt32 STAN) in c:\Users\g.\Documents\Visual Studio 2012\Projects\ProcessingLibrary\ProcessingLibrary\src\Processing Functions\UpdateTerminalInfoMethods.cs:line 70
Query functions (listed above) are also from HASP Class - I removed them for clarity they just call read/write methods in different ways and maybe Reset in some cases.
Can you please explain what problem happened and why? (Reset function got called by one of Query functions as it seems).
This must be related to the fact that I unplugged the HASP?
But why such exception?
How to modify my class such that if someone unplugs the HASP and then plugs it in, calls to the HASP are still successful?
Please note that initially my design was to call OpenPort once in application. And to call ClosePort also once in application.
|
|
|
|
|
Member 12061600 wrote: But why such exception? The exception identifies a bug in your code, some object reference is null when your code assumes it points to a real object. You need to use your debugger to catch the exception and find out which reference is in error and why.
|
|
|
|
|
What is the general good advice about using serial ports.
In my case I have only one function which talks to the HASP.
Is it good idea if I use it like this:
public booFoo()
{
try
{
OldHaspCommunication.OpenPort("COM6", 115200, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One, true, true);
.....
}
}
catch (Exception)
{ }
finally
{ ClosePort(); }
return false;
}
On each call to HASP I open and close it.
My issue is my class works fine but when I unplug HASP then come problems.
This way it seems there are no more issues, what do you think
|
|
|
|
|
Member 12061600 wrote: what do you think I think you should follow my suggestion and debug your code in order to find out how to prevent the exception.
|
|
|
|
|
This way I don't have the exception anymore, so why? I asked you about best practice to use Serial Port above.
|
|
|
|
|
There is no simple answer; best practice depends on what you are trying to do. And just because you don't have the exception any more does not mean the error has been fixed.
|
|
|
|
|
Ok so you don't have the answer, what is best way to talk with serial port - in such way that I am guarded against someone removing USB and then plugging in - code should still be able to talk with port.
Also the reason I posted code is someone may notice some design flaws or something.
I know what debugging is but issue seems not trivial.
|
|
|
|
|
Member 12061600 wrote: that I am guarded against someone removing USB How can you? If someone unplugs a physical device then the link is broken. The issue is how your program caters for, and recovers from, such a contingency. As with all programming you need to design for all situations.
Member 12061600 wrote: I know what debugging is but issue seems not trivial. That may be true, but it is still the best way to track down problems.
|
|
|
|
|
How can you? If someone unplugs a physical device then the link is broken. The issue is how your program caters for, and recovers from, such a contingency. As with all programming you need to design for all situations.
It seems I can check my first response to you.
When I open all the time port and then close in finally.
That way it seems to work always.
I am interested what are drawbacks of that.
|
|
|
|
|