How to Get image from ComPort - java

I have some problems in my work..
I have stored TTL serial camera images to MicroSD card successfully using Arduino UNO with the help of Adafruit Tutorial (learn.adafruit.com/ttl-serial-camera/overview) but when i m transferring that images through Zigbee transmitter, At the comport (Zigbee receiver) i m receiving random words. And i think its ASCII.
I want to save images receiving from comport to the folder of my PC.
Is it possible?
I have seen in some forums that use the java or python code, but i can't understand how to use it?
Read image data from COM7 port in Java

I guess this is what you are looking for:
import serial
ser = serial.Serial('/dev/tty.usbserial', 9600)
image = ser.read()
with open('/tmp/image', 'wb') as file:
file.write(image)
Works only in Python 3, in Python 2 you need to use io.open. You may need to install serial-modul first if you don't already have it. I'm not familiar with the Arduino-C-dialect you need to send the image over the com-port...

Arduino IDE's Serial Monitor is using the Serial class to communicate over a serial comm. port.
// Receive & send methods from the SerialMonitor class.
private void send(String s) {
..
serial.write(s);
}
public void message(final String s) {
..
textArea.append(s);
}
My suggestion is to reuse that (Java) code, but since the Serial class is designed for text-only communication you would need to encode the image bytes into e.g. Base64 encoding with this library and decode it on the PC.
If the transfer speed is important, and there is an Arduino binary-based serial communication library, you should use that.
UPDATE
You can read raw bytes from the serial port via the mentioned Serial class like this:
...
Serial port = ...;
byte[] buffer = new byte[1024]; // 1KB buffer
OutputStream imageOutput = new FileOutputStream(...);
// Wait for the image.
while (port.available() > 0) {
int numBytes = port.readBytes(buffer);
if (numBytes > 0) {
imageOutput.write(buffer, numBytes);
}
}
imageOutput.flush();
imageOutput.close();
...

Related

Sending a String via Serial COMM

I'm developing a system with Gemalto BG5ST (a java modem).
I need to send a string sent via http GET request to the Serial Port.
This string is stored, but the problem is that I need the data to be int or byte in order to write in the Outputstream.
Is there anyway to go around this?
OutputStream outStream = null;
String strCOM = "comm:COM0;blocking=off;baudrate=115200";//autocts=off;autorts=off
CommConnection commConn = (CommConnection)Connector.open(strCOM)
inStream = commConn.openInputStream();
outStream = commConn.openOutputStream();
Working with IDE 1.3 due to modem restrictions.
Thanks!
str.getBytes() - 1.1 version, used default charset
str.getBytes("UTF-8") - 1.1 version
I did this to solve the issue.
byte[] data = v1.getBytes();
int j;
for (j=0;j<data.length;j++)
{
outStream.write(data[j]);
System.out.println(data[j]);
}
Thanks guys.

Communication mismatch from Android to Arduino through RN42 bluetooth module

after this problem
Bluetooth connection in Android > 4.1.2
that I fixed by forcing connection by repeatedly try to connect with a while iteration, now I'm having an other issue with Android/Arduino communication.
My HW is Nexus 7 MY2012 with Android 4.3, Arduino UNO R3 and a BT module RN42.
From Android I'm sending an array of bytes to Arduino got from a string.
At the beginning, with Baudrate 115200 and no parity, only the first byte arrives correct and the rest was apparently mismatched (mostly in a repetitive manner).
After setting parity EVEN in the RN42 module, I see that at least first 3 bytes arrive correctly, and the rest is messed up.
Here below the core part of comunication from Android (initialization of BT basically follows the SDK example). It is located inside a class estended from AsyncTask used to manage connection work:
public void write(String message) {
Log.d(TAG, "...Data to send: " + message + "...");
byte[] msgBuffer = message.getBytes();
try {
mmOutStream.write(msgBuffer);
} catch (IOException e) {
mHardwareToServiceHdlr.obtainMessage(MSG_ERR_BT_WRITE).sendToTarget();
Log.d(TAG, "...Error data send: " + e.getMessage() + "...");
}
}
and Arduino sketch
#include <SoftwareSerial.h>
#include <Streaming.h>
const int bluetoothTx = 2;
const int bluetoothRx = 3;
byte incomingByte;
String incomingString;
SoftwareSerial bluetooth(bluetoothTx,bluetoothRx);
void setup() {
Serial.begin(115200);
bluetooth.begin(115200);
bluetooth.print("$$$");
delay(100);
bluetooth.println("U,115K,E");
bluetooth.begin(115200);
delay(100);
}
void loop(){
if (bluetooth.available() > 0) { // if the data came
incomingByte = bluetooth.read(); // read byte
Serial.println((char)incomingByte);
}
}
If I send to Arduino a string such as "hello horld" this is what I get in serial monitor in a serie of transmissions:
hel,o wo2ld
hel<o wo2ld
hel,o7orld
hel,o wo2ld
hel,o wo2ld
hel<o7or6d
hel,o wo2ld
hel,o wo2ld
hel,o wo2ld
hel<o wo2ld
hel,o7orld
hel<o wo2ld
This is just example, result depends also on how much often I send the string to Arduino.
The most of the times, the fourth and the nineth byte (but not always in the same manner and not always only them) are mismatched.
Transmitting data from Arduino to Android seems to work fine with any particular trouble.
Any help would be greatly apreciated, this thing is making me crazy as far as I need to transmit data longer than 3 bytes.
Thanks
There could be multiple issues with your setup:
Don't use SoftwareSerial's begin() multiple times. If you need to empty the buffer then use bluetooth.flush() after the println() and before the delays.
Taken from the SoftwareSerial docs:
If using multiple software serial ports, only one can receive data at a time.
Try using UART as the serial port instead of a bit-banged serial port.
The Arduino UNO supports interrupts only for pins 2 and 3, and they are frequently used for something else, check that they are really free in your design.
Try using a slower baud rate, there could even be RF issues with your layout at 115200 bps.
If the previous approaches don't work, try storing the characters in a buffer before doing the Serial.print in the main loop. After you are done, finally print it, like this (mind the boundary checks):
char buffer[MAX_LEN];
unsigned int count = 0;
void loop(){
if (bluetooth.available() > 0) { // if the data came
buffer[count++] = bluetooth.read(); // read byte
}
buffer[count] = '\0';
Serial.print(buffer);
count = 0;
}
Kindly ensure pairing the device explicitly from your Android phone’s settings. Usually the pair code is 1234. The Hardware Tx,Rx pin are preferred if free on the Arduino. 96200 baud rate is generally better

String serial communication

I'm just learning Java for about a couple weeks now. My end goal is to have my Arduino measure some sensors and send the results to my android via USB cable. However I'm just trying to get my Arduino to communicate with the Java console on my computer first, and then I figure it's pretty much copy and paste from there.
I can get my Arduino and Java console to communicate with each other as long as it's a simple byte. Anything passed that is giving me a result of the quadratic equation exploding.
In my Arduino serial Monitor I get this:
Engine Temp:100
Air Temp:95
Engine Speed:10000 RPM
Wheel Speed:30 MPH
and in my Java console I get this:
Normal println
[B#6f5f6479
String
€˜3Àf`Ì󀘀
UTF
??3?f`??
Here is my reader code:
#Override
public void serialEvent(SerialPortEvent arg0) {
byte[] readBuffer = new byte[1000];
try {
int availableBytes = input.available();
if (availableBytes > 0) {
// Read the serial port
input.read(readBuffer, 0, availableBytes);
// Print it out
System.out.println("Normal println");
System.out.println(readBuffer);
System.out.println("\nString");
System.out.println(new String(readBuffer, 0, availableBytes));
System.out.println("\nUTF");
System.out.println(new String(readBuffer,"UTF-8"));
System.out.println("\nDone");
}}
catch (IOException e) {
}
}
input is my InputStream.
Ensure that the Arduino and the serial port on the android are at the same speed. The provided code does NOT show how the serial ports are defined and opened/started so its impossible to know if this is the problem.
The characters displayed at the android appear like the typical garbage seen when the two end-points of a serial line are not similarly configured.
It looks like the Arduino is correctly sensing the engine's dynamics?

jPcap - send packet to selected MAC (not the selected interface)

I'm trying to send ethernet packet to choosed destination MAC address using jPcap:
public void sendPacket(Packet packet, byte[] srcMac, byte[] dstMac, Interface i) throws IOException
{
JpcapSender sender = JpcapSender.openDevice(i.netInterface);
EthernetPacket ether = new EthernetPacket();
ether.frametype = EthernetPacket.ETHERTYPE_IP;
ether.src_mac = srcMac; // MAC address of selected interface
ether.dst_mac = dstMac; // MAC addr. choosed somwhere on form
packet.datalink = ether;
sender.sendPacket(packet);
sender.close();
}
It works, but it's always sent to the selected interface not to the dst_mac!
So I don't understand the relation between selected interface and scr_mac:
why I have to choose both (interface and scr_mac)?
why I have to add dst_mac even if it's not used?
how to send packet out of my computer then?
why I have to choose both (interface and scr_mac)?
The interface is what the software is using to communicate (to send or receive packets). This is usually your ethernet card. You need to specify it so that Jpcap knows how to send the information. The src_mac address is part of the packet header. It is intended to be used dynamically so that as the packets are being sent they are updated with the appropriate information. The src_mac does not necessarily play a role in how the packet is sent.
why I have to add dst_mac even if it's not used?
It is used. Make sure that you have the other device with the specified mac address linked to your source by a direct ethernet connection, and also make sure that it is ready to receive the data. Right now, what I suspect is happening, is you're trying to read back through your same interface on the host computer.
Jpcap's website has some tutorials and samples I found useful. I've worked quite a bit with the Jpcap library, and I would be happy to help you if you have any more questions.

How to control the handset using AT commands in java

I know that by using AT commands we can control the handset.As example unlocking screen we can give a specific AT command or moving right to the menu or left or bottom or up we can give specific AT commands. What all are the AT commands for doing this kind of control.
Thank you.
From what I understand, the AT commands are more used for phone-type functions (making calls, or sending SMS, etc), rather than menu navigation, etc.
I'm not entirely sure if that was your end-goal after menu navigation, but you can find more details here: http://en.wikipedia.org/wiki/Hayes_command_set (the original +AT command set)
If you wanted to send SMS from a handset connected to your computer you might want to take a peek at this page: http://www.developershome.com/sms/atCommandsIntro.asp
If you wanted more control when performing functions, like sending SMS, etc, you might want to investigate "PDU Mode."
It is entirely possible that some handset manufacturers may have implemented additional +AT commands to allow other functions to be performed, so you might do better by specifically searching for the commands related to the handset you are using.
(Of course, if you're having issues connecting to the handset hardware itself, you need to ensure you have either the javax.comm extension or some favoured Java USB API installed)
If post doesn't help, perhaps you could provide more details in your question? (eg. what you are ultimately trying to do, if you think it would help)
List of AT commands
sample java code to use AT command
public void servicesDiscovered(int transID, ServiceRecord serviceRecord[])
{
String url = serviceRecord[0].getConnectionURL(1, false);
try
{
//ClientSession conn= (ClientSession)Connector.open(url);
StreamConnection meineVerbindung = (StreamConnection) Connector.open(url);
if(conn== null)
System.out.println("Kann Service URL nicht oeffnen\n");
else
{
OutputStream out = conn.openOutputStream();
InputStream in = conn.openInputStream();
String message = "AT+CGMI\r\n";
// send AT-command
System.out.println("send AT Comand request: "+message);
out.write(message.getBytes());
out.flush();
out.close();
byte buffer[] = new byte[10000];
// read the response from mobile phone
in.read(buffer);
System.out.println("AT Comand response: "+buffer.toString());}
}
catch(IOException e)
{
System.out.println("Service Error(3): "+e.getMessage());
}
}

Categories

Resources