transferring data from PIC to Arduino via RS232

Hello masters:
I'm using a microchip transferring some data through RS232 to Arduino mega2560 which Arduino mega2560 is connect to my computer through USB(Com3).
But do i need to write any code to transfer the data from RS232 to USB in order to represent the data on the serial monitor?
Or can i just use the code such as:
(there is a problem : incompatible types in assignment of 'int' to 'unsigned int [9]' how to fix it?)

unsigned int data[9];
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
data=Serial.read();
Serial.write(data);
}
}

data=Serial.read();

data is an array. Which element of the array would you like to store the value in ?

I'm using a microchip transferring some data through RS232 to Arduino mega2560

Can we assume that you have an RS232 converter between the device and the Arduino TTL input

tony_lai:
I'm using a microchip transferring some data through RS232 to Arduino mega2560 which Arduino mega2560 is connect to my computer through USB(Com3).

Which of the Mega's serial ports are you connecting your RS232 signal to? Your code implies that it is connected to pins 0 and 1 which would be wrong.

Use (for example) Serial1 on pins 19 and 18.

And, as mentioned by @UKHelibob, you should not connect RS232 voltage levels directly to an Arduino - they may damage the Arduino and they won't be intelligible to it.

...R

Hello Tony,

Here is the code I use to do something similar to what you want. You will have to modify it for your needs, in particular setting the correct bard rates and using the correct serial ports. Offered 'as is' for you to play around with, no support.

// Receives data on serial port 1 and sends to the serial monitor with an index.
// Each character is on a new line.
// Receiving 0x00 resets the count to 0.

char fileName[] = __FILE__;

void setup() {
  Serial.begin(115200);
  delay(2000);
  Serial.println("Serial monitor started");
  Serial1.begin(9600);
  Serial.println("Serial 1 started");
  Serial.println(" ");
  Serial.println(sizeof(fileName));
  Serial.println(fileName);  
}

void loop() {
  serialMonitor();
}

void serialMonitor() {
  char RxTemp;
  static uint8_t charCount;
  while (Serial1.available() > 0) {
    RxTemp = Serial1.read();
    Serial.print(charCount);
    Serial.print(" byte ");
    Serial.print((byte)RxTemp, HEX);
    Serial.print(" char ");
    Serial.println((char)RxTemp);
    ++charCount;
    if ((uint8_t)RxTemp == 0) {
      charCount = 0;
      Serial.println("charCount = 0");
    }
  }
}

EDIT
Never mind Shakespeare, set the baud rates to what you need.

bard rates

:slight_smile:

Dear@UKHeliBob:
I do use a TTL to RS232 converter
So it should be

data[1] = Serial.raed();

if i would like to read the first element of this array,right?
but what if i would like to read all the elements of this array?
Should i use
data[1] = Serial.raed();
data[2] = Serial.raed();
data[3] = Serial.raed();
.
.
.
till
data[9] = Serial.raed();

Or is there any faster ways?

UKHeliBob:

data=Serial.read();

data is an array. Which element of the array would you like to store the value in ?
Can we assume that you have an RS232 converter between the device and the Arduino TTL input

Dear @PerryBebbington:
Thank you for your code!!
I will try it with the correct ~~bard ~~ baud rate!
Thanks again!

PerryBebbington:
Hello Tony,

Here is the code I use to do something similar to what you want. You will have to modify it for your needs, in particular setting the correct bard rates and using the correct serial ports. Offered ‘as is’ for you to play around with, no support.

// Receives data on serial port 1 and sends to the serial monitor with an index.

// Each character is on a new line.
// Receiving 0x00 resets the count to 0.

char fileName = FILE;

void setup() {
 Serial.begin(115200);
 delay(2000);
 Serial.println(“Serial monitor started”);
 Serial1.begin(9600);
 Serial.println(“Serial 1 started”);
 Serial.println(" ");
 Serial.println(sizeof(fileName));
 Serial.println(fileName);  
}

void loop() {
 serialMonitor();
}

void serialMonitor() {
 char RxTemp;
 static uint8_t charCount;
 while (Serial1.available() > 0) {
   RxTemp = Serial1.read();
   Serial.print(charCount);
   Serial.print(" byte “);
   Serial.print((byte)RxTemp, HEX);
   Serial.print(” char ");
   Serial.println((char)RxTemp);
   ++charCount;
   if ((uint8_t)RxTemp == 0) {
     charCount = 0;
     Serial.println(“charCount = 0”);
   }
 }
}




EDIT
Never mind Shakespeare, set the baud rates to what you need.

data[1] = Serial.raed();

if i would like to read the first element of this array,right?

No. That puts the byte read from Serial in the second element of they array as they are indexed from zero.

but what if i would like to read all the elements of this array?
Should i use
data[1] = Serial.raed();
data[2] = Serial.raed();
data[3] = Serial.raed();

Note my comment above, but in any case, before reading from Serial you must make sure that something is available to read using Serial.available()

Hello guys! I am using Arduino mega2560 for data transfer, and my process is :
1.PIC will create some data and the type of the data is unsigned int data[9];
2.data will transfer to RS232(Serial 3 TX3 RX3) first.
3.data will through TTL and go to Arduino Mega 2560(The voltage has been convert)
4.data will go from TTL to USB port and represent the data[9] on the serial monitor

data[0]=0xFE;
data[1]=(RefSpeed >> 8 ); //0x00~0xFE
data[2]=(RefSpeed&(256-1));//0x00~0xFE
data[3]=(Speed >> 8 );//0x00~0xFE
data[4]=(Speed&(256-1));//0x00~0xFE
data[5]=(ControlOutput >> 8 );//0x00~0xFE
data[6]=(ControlOutput&(256-1));//0x00~0xFE
data[7]=0xFE;
data[8]=0xFE:

Arduino code:
#define RS232PORT Serial3
#define USBPORT Serial

#define RS232BAUDRATE 250000
#define USBBAUDRATE 250000

void setup() {
RS232PORT.begin(RS232BAUDRATE);
USBPORT.begin(USBBAUDRATE);
}

void loop() {
char data[9];
unsigned int nBytesAvail = 0;
unsigned int nBytes = 0;

//check for data from RS232 PORT
if((nBytesAvail = RS232PORT.available())>0)
{
//read data into receive buffer
nBytes = RS232PORT.readBytes(data,nBytesAvail);
//write the data to other port
USBPORT.write(data,nBytes);
delay(50);
}

//check for data from USB PORT
if((nBytesAvail = USBPORT.available())>0)
{
//read data into receive buffer
nBytes = USBPORT.readBytes(data,nBytesAvail);
//write the data to other port
USBPORT.write(data,nBytes);
delay(50);
}
USBPORT.write("start");
USBPORT.println(data[0]);
USBPORT.print(data[1]);
USBPORT.println(data[2]);
USBPORT.print(data[3]);
USBPORT.println(data[4]);
USBPORT.print(data[5]);
USBPORT.println(data[6]);
delay(50);
}

I'm sure the data has been transfer by PIC, but the data i receive from Arduino is
PIC Arduino
start
0xFE 0
0x15 27
0x80 56

which is all wrong, can anyone help me please~

0xFE as 0 indicates too much RS-232 conversion of polarity and most probably also voltage. Check the voltage level generated by the PIC and drop the RS-232 converter if possible.

Hi,

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

I think you are not taking account of the fact that an int uses 2 bytes.

You may also need to check the endian-ness of the PIC - is it the same as the Mega. IIRC the Mega is little-endian.

It looks as if your data uses 0xFE as an end marker. If so this second example in Serial Input Basics may be useful. However you will need to change it to use bytes rather chars.

...R

   if((nBytesAvail = RS232PORT.available())>0)

That very definitely doesn't do what you think for at least 2 different reasons.

Have a look at C Operators for how to use =, == and other operators.

Difficult to help without your PIC code.

You already have my test code.

Topics on the same subject merged

DO NOT START NEW TOPICS ON THE SAME SUBJECT