i bought a NP-05B powerUSB device and am trying to interface the device in c#. I was able to connect to the device using YAT.
YAT interface response
In the c# form i imported a serial device and i am able to connect and send commands to the device. But when the device answers to the command it only returns: pshow. The serial read seems to not be able to catch "CR NUL LF" and anything afterward.
Here is my serialRead implementation
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int Count;
Byte[] Rx_Byte;
Count = 0;
Rx_Byte = new Byte[serialPort1.BytesToRead];
serialPort1.Read(Rx_Byte, 0, Rx_Byte.Length);
while (Count < Rx_Byte.Length)
{
if (Rx_Byte[Count] != 0x00)
{
Console.WriteLine("receiving " + Rx_Byte[Count].ToString());
}
Count++;
}
}
and here is the response i get from my console:
Console Output
Thank you for your time.
With a serial sniffer i was able to figure out that i needed to send a return carriage and that C# doesn't send one by default when writing to serial port
thank you.
Related
I made this Source code to implement lwip tcp server
If you try to remove the st-link wire and do it without debugging, the server itself doesn't work.
I try to find a way, but I don't know where the problem is.
If the LED on the board blinks, it means it's down.
Is there a problem with why the server is not working?
If you turn it to debugging, you receive it without any problems, generate a message, and then send it.
Then isn't there a problem with the Source code?
If the connection itself is not working when there is no debugging, I don't think the server is created
But if it's a circuit problem, I think it's weird to be a server when you debug it
I don't know where to look.
When debugging, the client sent and received properly when it connected to the client from hercules or Raspberry Pi
void Tcp_Task(void const * argument)
{
/* USER CODE BEGIN Tcp_Task */
struct netconn *conn, *newconn;
err_t err, accept_err;
struct netbuf *buf;
void *data;
u16_t len;
MX_LWIP_Init();
LWIP_UNUSED_ARG(argument);
conn = netconn_new(NETCONN_TCP);
if (conn!=NULL)
{
// netconn_bind(conn, NULL, port 번호)
err = netconn_bind(conn, NULL, 5001);
if (err == ERR_OK)
{
netconn_listen(conn);
while (1)
{
accept_err = netconn_accept(conn, &newconn);
if (accept_err == ERR_OK)
{
while (netconn_recv(newconn, &buf) == ERR_OK)
{
do
{
netbuf_data(buf, &data, &len);
memcpy(receivemsg, data, len);
transmitmsg = procPacket();
msg_len = getsendPackSize();
netconn_write(newconn, transmitmsg, msg_len, NETCONN_COPY);
}
while (netbuf_next(buf) >= 0);
netbuf_delete(buf);
}
netconn_close(newconn);
netconn_delete(newconn);
}
osDelay(100);
}
}
else
{
netconn_delete(newconn);
}
}
/* Infinite loop */
for(;;)
{
osDelay(100);
}
/* USER CODE END Tcp_Task */
}
lwip tcp server Source code site
[https://blog.naver.com/eziya76/221867311729](lwip tcp server)
I don't think I can implement the server if I turn the board off and on or without debugging, but I don't know where to find the problem
Please let me know if the Source code is the problem, and if the other place is the problem, please tell me how to find it
I'm writing this using a translator, so please understand
We use the Windows IoT to connect to another application in network with TCPClient. All works now, except one point which confuse us. When we unplug the network cable from our raspberry Pi (I guess version 2) the Startup application with a GUI is immediately stopped. When we plug in the cable back, after a little time the Startup app is started again.
We use VS2017 to build arm UWP app.
Any idea how to prevent stop of application?
As without your source code,I am not sure what cause your problem.I have tested following pieces codes in raspberry Pi 3, when i unplug the network cable, the application with UI does not stop.In foreground task,asynchronous method is recommend, please reference Asynchronous programming.
Server:
TcpListener tcpListener = new TcpListener(IPAddress.Any, 6550);
tcpListener.Start();
byte[] buffer = new byte[1024];
String data = string.Empty;
while (true)
{
var client = await tcpListener.AcceptTcpClientAsync();
var stream = client.GetStream();
int i = 0;
// Loop to receive all the data sent by the client.
do
{
i = await stream.ReadAsync(buffer, 0, buffer.Length);
if(i > 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(buffer, 0, i);
//Display received message
txtRServerContent.Text = data;
}
} while (i > 0);
}
Client:
TcpClient tcpClient = new TcpClient(AddressFamily.InterNetwork);
await tcpClient.ConnectAsync(IPAddress.Parse("10.168.197.66"), 14400);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
NetworkStream stream = tcpClient.GetStream();
// Buffer to store the response bytes.
byte[] data = new byte[1024];
// String to store the response ASCII representation.
String responseData = String.Empty;
int count = 0;
while (true)
{
// Read the first batch of the TcpServer response bytes.
count = await stream.ReadAsync(data, 0, data.Length);
if (count > 0)
{
responseData = System.Text.Encoding.ASCII.GetString(data, 0, count);
//Dispaly responsed message
txtClientContent.Text = responseData;
}
}
It seems that this issue only occur if debugger is attached. For now this topic is closed.
I have searched a lot for this but could not find any specific answer that applies to my case. I have made device using GPS module and GSM/GPRS module with Arduino Mega 2560, and it sends me the location through SMS. Now I want to get the location parameters using GPRS. I have in mind to use TCP. I will send data through AT Commands from GPRS module, but I am confused on how to make a server on C#. I know that I would be needing a static/public IP for this. But I don't know how to get the public IP, and start receiving data which I send from GPRS module. Please please I need help because I am a beginner in Client/Server programming, and I am working on my final year project. Many thanks in advance!
please take a look at this TCP server and client example.
You will need a public static IP address. That is something you have to ask your broadband provider, and they will explain you the available options they have, probably you will have to pay extra money. You can use your current public IP address, that will be probably dynamic, but they don't use to change way to often, so whenever you are unable to connect, you will have to check if the IP changed or not, and set the new one.
This video series maybe a good introduction: https://vimeo.com/38103518
Here is the Server Code:
class Server
{
TcpListener server = null;
public Server(string ip, int port)
{
IPAddress localAddr = IPAddress.Parse(ip);
server = new TcpListener(localAddr, port);
server.Start();
StartListener();
}
public void StartListener()
{
try
{
while (true)
{
Console.WriteLine("Waiting for a connection... ");
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
Thread t = new Thread(new ParameterizedThreadStart(HandleDeivce));
t.Start(client);
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
}
public void HandleDeivce(Object obj)
{
TcpClient client = (TcpClient)obj;
NetworkStream stream = client.GetStream();
string data = null;
Byte[] bytes = new Byte[256];
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("{1}: Received: {0}", data, Thread.CurrentThread.ManagedThreadId);
if (data.StartsWith("##"))
{
data = "LOAD";
}
else
{
data = "ON";
}
byte[] msg = Encoding.ASCII.GetBytes(data);
stream.Write(msg, 0, msg.Length);
Console.WriteLine("{1}: Sent: {0}", data, Thread.CurrentThread.ManagedThreadId);
}
client.Close();
}
}
Is there any RIM API available which will help to get the list of available network service or only Wi-Fi networks for a device and set selected network access point for any network communications?
Is it possible for my application to disable the mobile networks like GPRS, WAP, etc.?
Example:
When the application is started it should scan for Wi-Fi connections, even if there are no previous Wi-Fi network access points set on the device, and list the available Wi-Fi connections. Then the user will select the appropriate Wi-Fi connection to connect for any network communication. Outside the application any Internet communication, like the browser or any other application, should be done through the same selected Wi-Fi connection.
The scanning for Wi-Fi and setting the connection is almost similar to BlackBerry Wi-Fi Setup.
I am looking to do this for BlackBerry OS 4.5, 4.7 and 5.0.
Update
The thing is I'm looking for Wi-Fi scanning through application. It's like through the application I am able to scan available Wi-Fi access points or hotspots and set one of access point by selecting it to device, then connect to it for communication.
Basically it's like, how do we set the Wi-Fi connection in "Manage connetion" of BlackBerry? I have to do a similar thing through the applicaiton.
From some BlackBerry forums I got to know there is package in OS v5.0, that is, a net.rim.device.api.wlan.hotspot package to get the Wi-Fi hotspots. But after a long search I didn't find any sample example or much explanation on it. As I am trying to implement by looking into its API documentation, but I did not succeded.
If you have any idea related to this or any sample code it will be very helpful.
Well, to scan for all available networks for the application you can use the NetworkDiagnostic tool from RIM.
Anther piece of code to scan for your phone connectivity and get the best connection string can be found in How to reliably establish a network connection on any BlackBerry device,
/**
* Determines what connection type to use and returns the necessary string to use it.
* #return A string with the connection info
*/
private static String getConnectionString()
{
// This code is based on the connection code developed by Mike Nelson of AccelGolf.
// http://blog.accelgolf.com/2009/05/22/blackberry-cross-carrier-and-cross-network-http-connection
String connectionString = null;
// Simulator behavior is controlled by the USE_MDS_IN_SIMULATOR variable.
if (DeviceInfo.isSimulator())
{
if (UploaderThread.USE_MDS_IN_SIMULATOR)
{
logMessage("Device is a simulator and USE_MDS_IN_SIMULATOR is true");
connectionString = ";deviceside=false";
}
else
{
logMessage("Device is a simulator and USE_MDS_IN_SIMULATOR is false");
connectionString = ";deviceside=true";
}
}
// Wi-Fi is the preferred transmission method.
else if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
{
logMessage("Device is connected via Wifi.");
connectionString = ";interface=wifi";
}
// Is the carrier network the only way to connect?
else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT)
{
logMessage("Carrier coverage.");
String carrierUid = getCarrierBIBSUid();
if (carrierUid == null)
{
// Has carrier coverage, but not BIBS. So use the carrier's TCP network
logMessage("No Uid");
connectionString = ";deviceside=true";
}
else
{
// otherwise, use the Uid to construct a valid carrier BIBS request
logMessage("uid is: " + carrierUid);
connectionString = ";deviceside=false;connectionUID="+carrierUid + ";ConnectionType=mds-public";
}
}
// Check for an MDS connection instead (BlackBerry Enterprise Server).
else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS)
{
logMessage("MDS coverage found");
connectionString = ";deviceside=false";
}
// If there is no connection available abort to avoid bugging the user unnecssarily.
else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE)
{
logMessage("There is no available connection.");
}
// In theory, all bases are covered so this shouldn't be reachable.
else
{
logMessage("no other options found, assuming device.");
connectionString = ";deviceside=true";
}
return connectionString;
}
/**
* Looks through the phone's service book for a carrier provided BIBS network
* #return The uid used to connect to that network.
*/
private static String getCarrierBIBSUid()
{
ServiceRecord[] records = ServiceBook.getSB().getRecords();
int currentRecord;
for (currentRecord = 0; currentRecord < records.length; currentRecord++)
{
if (records[currentRecord].getCid().toLowerCase().equals("ippp"))
{
if (records[currentRecord].getName().toLowerCase().indexOf("bibs") >= 0)
{
return records[currentRecord].getUid();
}
}
}
return null;
}
I am trying to send data using UDP (datagram). I am not able to test application on simulator. I tried running MDS first and then simulator,but it did not work. The error is displayed as Port 8080 already in use on BlackBerry simulator console. How do I change port in simulator? The UDP port to which I am connecting is localhost:5014
I am using simulator for BlackBerry Pearl 8100.
On the Blackberry forum there are comments about issues with datagram under 4.5.0.x up to 4.5.0.83. No wonder there are no UDP samples in sdk.
You always can download 8100 with 4.5.0.108 simulator from http://na.blackberry.com/eng/developers/
Another thing is to use ip, although hostname is allowed in api reference, but when you use MDS simulator it grabs localhost alias.
In the following code you have simple server which is listening to port 135, and bb client which is sending data packet to 127.0.0.1 on port 135.
Desktop server code:
public static void main(String[] args) {
byte[] inBuff = new byte[32];
DatagramSocket socket;
try {
socket = new DatagramSocket(137);
DatagramPacket pckt = new DatagramPacket(inBuff, inBuff.length);
while (true) {
socket.receive(pckt);
System.out.println(new Date() + " " + pckt.getAddress()
+ ":" + pckt.getPort());
socket.send(pckt);
}
} catch (Exception e) {
System.out.println(e.getMessage()+":");
System.out.println(e.getClass().getName());
}
}
BlackBerry client code (tested with Bold 8900 under 4.6.1):
UDPDatagramConnection connection = null;
byte[] outBuff = "Hello!".getBytes();
Datagram outDatagram = null;
try {
connection = (UDPDatagramConnection) Connector
.open("datagram://127.0.0.1:137");
outDatagram = connection.newDatagram(outBuff, outBuff.length);
connection.send(outDatagram);
System.out.println("Datagram packet was sent");
} catch (Exception e) {
System.out.println(e.getMessage()+":");
System.out.println(e.getClass().getName());
}